**Original question:**

You is a political prisoner in jail. Things is looking grim, but fortunately, your jailmate have come up with an escape plan. He has found a-a-both of you-get out of the cell and run through the city to the train station, where you'll le Ave the country. Your friend would escape first and run along the streets of the city to the train station. He'll then call the there on your cellphone (which somebody smuggled in to you inside a cake), and you'll start T o Run to the same train station. When you meet your friend there, you'll both board a train and is on the Your to freedom. Your friend'll is running along the streets during the day, wearing he jail clothes, so people would notice. This is what you can do follow any of the same streets, your friend follows-the authorities may be waiting for you t Here. You had to pick a completely different path (although the run across the same intersections as your friend). What's the earliest time at which YOur friend can board a train?

Problem, in short

Given a weighed, undirected graph, find the shortest path from S to T and back without using the same edge twice.

Input

The input would contain several test cases. Each test case would begin with an integer n (2≤n≤100)

-the number of nodes (intersections). The jail is at node number 1, and the train station was at node number n. The next line would contain an integer m-the number of streets. The next m lines would describe the M streets. Each line would contain 3 integers-the, nodes connected by the street and the time it takes to run the length of the s Treet (in seconds). No Street would be longer than or shorter than 1. Each street would connect to the different nodes. No pair of nodes'll be directly connected by more than one street. The last test case would be followed to a line containing zero.

Output

For each test case, output a single integer on a line by itself-the number of seconds and your friend need between t He time he leaves the jail cell and the time both of you board the train. (Assume-do not need-to-wait for the Train-they leave every second.) If There is no solution, print ' back to jail '.

Sample Input

2

1

1 2 999

3

3

1 3 10

2 1 20

3 2 50

9

12

1 2 10

1 3 10

1 4 10

2 5 10

3 5 10

4 5 10

5 7 10

6 7 10

7 8 10

6 9 10

7 9 10

8 9 10

0

Sample Output

Back to jail

80

Back to jail

**English:**

Give you a weighted graph that allows you to find the minimum cost to go from Node 1 to node N and then back from node N to Node 1.

#include<bits/stdc++.h>
using namespace std;
const int maxn=10001;
int mark;
struct Edge
{
int from,to,cap,flow,cost;
Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w){}
};
struct MCMF
{
int n,m;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn];
void init(int n)
{
this->n=n;
for(int i=1;i<=n;i++)
G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap,int cost)
{
edges.push_back(Edge(from,to,cap,0,cost));
edges.push_back(Edge(to,from,0,0,-cost));
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BellmanFord(int s,int t,int& flow,long long& cost)
{
for(int i=1;i<=n;i++)
d[i]=INT_MAX;
memset(inq,0,sizeof(inq));
d[s]=0;
inq[s]=1;
p[s]=0;
a[s]=INT_MAX;
queue<int> Q;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
inq[u]=0;
for(int i=0;i<G[u].size();i++)
{
Edge& e=edges[G[u][i]];
if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)
{
d[e.to]=d[u]+e.cost;
p[e.to]=G[u][i];
a[e.to]=min(a[u],e.cap-e.flow);
if(!inq[e.to])
{
Q.push(e.to);
inq[e.to]=1;
}
}
}
}
if(d[t]==INT_MAX)
return false;
flow+=a[t];
cost+=(long long )d[t]*(long long)a[t];
for(int u=t;u!=s;u=edges[p[u]].from)
{
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
}
return true;
}
int MincostMaxflow(int s,int t,long long& cost)
{
int flow = 0;
cost=0;
int road=0;
while(BellmanFord(s,t,flow,cost))
{
road++;
if(road==2)
break;
}
if(road==2)
mark=true;
return flow;
}
};
MCMF M;
int main()
{
ios::sync_with_stdio(false);
int n,m;
while(cin>>n,n)
{
M.init(n);
mark=false;
cin>>m;
for(int i=1;i<=m;i++)
{
int f,t,c;
cin>>f>>t>>c;
M.AddEdge(f,t,1,c);
M.AddEdge(t,f,1,c);
}
long long cost=0;
M.MincostMaxflow(1,n,cost);
if(!mark)
cout<<"Back to jail"<<endl;
else
cout<<cost<<endl;
}
return 0;
}

**Answer:**

This is my first minimum cost of the maximum flow of the topic, just beginning to be the name of the problem to be fooled, has always been thought to be the shortest-circuit problems, but think for a long time can not think of how to solve (seemingly can use a short path to resolve). Internet on a search, the results of a look is the problem of network flow-_-.

Here the traffic on all sides of the graph is set to 1, because there is no graph, so we need to establish two identical edges between the two nodes.

The title of the request from the beginning to the end, and then from the end point back to the beginning, here to use the network flow processing characteristics, if passing through a certain edge, then this change to increase the corresponding flow, while the reverse side to reduce the corresponding traffic. This is also the essence and core of the network flow, but also why the network flow algorithm can solve the problem of the fundamental.

Now the starting point is the S endpoint is T, if the first path is s->a->b->c->t, then the traffic on the edge will change, the second search to the path may be s->b->a->d->t, Then the two path weights and up are the S->c->t plus the s->d->t value. Therefore, using the minimum cost maximum flow algorithm solves this problem, which is equivalent to the augmented path of the minimum cost of finding two to the endpoint from the starting point, and two times the cost is the answer.