UVa 10806 Dijkstra, Dijkstra (最小费用流)
Dijkstra, Dijkstra
Dexter: “You don’t understand. I can’t walk...
they’ve tied my shoelaces together.”
Topper Harley: “A knot. Bastards!” Jim Abrahams and Pat Proft,
"Hot Shots! Part Deu
Description
you are a political prisoner in jail. Things are looking grim, but fortunately, your jailmate has come up with an escape plan. He has found a way for both of you to get out of the cell and run through the city to the train station, where you will leave the country. Your friend will escape first and run along the streets of the city to the train station. He will then call you from there on your cellphone (which somebody smuggled in to you inside a cake), and you will start to run to the same train station. When you meet your friend there, you will both board a train and be on your way to freedom. Your friend will be running along the streets during the day, wearing his jail clothes, so people will notice. This is why you can not follow any of the same streets that your friend follows - the authorities may be waiting for you there. You have to pick a completely different path (although you may run across the same intersections as your friend). What is the earliest time at which you and 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 will contain several test cases. Each test case will begin with an integer n (2 ≤ n ≤ 100) — the number of nodes (intersections). The jail is at node number 1, and the train station is at node number n. The next line will contain an integer m — the number of streets. The next m lines will describe the m streets. Each line will contain 3 integers — the two nodes connected by the street and the time it takes to run the length of the street (in seconds). No street will be longer than 1000 or shorter than 1. Each street will connect two different nodes. No pair of nodes will be directly connected by more than one street. The last test case will be followed by a line containing zero.
Output
For each test case, output a single integer on a line by itself — the number of seconds you and your friend need between the time he leaves the jail cell and the time both of you board the train. (Assume that you 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
/* * UVa 10806 Dijkstra, Dijkstra * 求两条从S到T的路径,没有重复的边,并且边权和最小 * * 最小费用流,流量为2时的费用就是答案,否则无解 */ #include <bits/stdc++.h> using namespace std; struct MinCostMaxFlow { const static int MAXN = 10000; const static int MAXE = 100000; const static int INF = 0x3f3f3f3f; struct Edge { int from,to,next,cap,flow,cost; Edge(){} Edge(int u,int v,int c,int f,int _c,int nxt):from(u),to(v),cap(c),flow(f),cost(_c),next(nxt) {} }edge[MAXE]; int head[MAXN],tol,N; int pre[MAXN],dis[MAXN]; bool vis[MAXN]; //Function void init() { N=MAXN; tol=0; memset(head,-1,sizeof(head)); } void link(int u,int v,int cap,int cost)//s->t,cap,cost { edge[tol]=Edge(u,v,cap,0,cost,head[u]);head[u]=tol++; edge[tol]=Edge(v,u,0,0,-cost,head[v]);head[v]=tol++; } bool spfa(int S,int T) { queue<int>Q; for(int i=0;i<N;i++) dis[i]=INF; for(int i=0;i<N;i++) vis[i]=false; for(int i=0;i<N;i++) pre[i]=-1; dis[S] = 0; vis[S] = true; Q.push(S); while(!Q.empty()) { int u=Q.front(); Q.pop(); vis[u] = false; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost) { dis[v]=dis[u]+edge[i].cost; pre[v]=i; if(!vis[v]) { vis[v] = true; Q.push(v); } } } } if(pre[T] == -1) return false; else return true; } int MCMF(int S,int T,int &cost) { cost=0; int maxflow=0; while(spfa(S,T)&&maxflow<=2) { int Min=INF; for(int i=pre[T];i!=-1;i=pre[edge[i^1].to]) Min=min(Min,edge[i].cap-edge[i].flow); for(int i=pre[T];i!=-1;i=pre[edge[i^1].to]) { edge[i].flow+=Min; edge[i^1].flow-=Min; cost+=edge[i].cost*Min; } maxflow+=Min; } return maxflow; } }MCMF; int main() { int n,m; int u,v,w; while(scanf("%d",&n)==1&&n) { MCMF.init(); scanf("%d",&m); while(m--) { scanf("%d%d%d",&u,&v,&w); MCMF.link(u,v,1,w); MCMF.link(v,u,1,w); } MCMF.link(0,1,2,0); MCMF.link(n,n+1,2,0); int ans; int f=MCMF.MCMF(0,n+1,ans); if(f<=1) printf("Back to jail\n"); else printf("%d\n",ans); } return 0; }