Dijkstra, Dijkstra.
Time Limit: 10 seconds
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 Deux."
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 | Sample Output |
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 | Back to jail 80 Back to jail |
题意:给你一个图,问从1到n两种完全不同的走法的最小费用。
思路:用费用流跑出流量为2就结束。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
int v,cost,cap,next;
}Edge[40010];
int n,m,h[110],tot,C,F,dis[110],INF=1e9,pre[110],f[110];
bool vis[110];
queue<int> qu;
void AddEdge(int u,int v,int cost,int cap)
{
Edge[++tot].v=v;
Edge[tot].cost=cost;
Edge[tot].cap=cap;
Edge[tot].next=h[u];
h[u]=tot;
}
int _spfa()
{
int i,j,k,u,v,p;
for(i=1;i<=n;i++)
dis[i]=INF;
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
memset(f,0,sizeof(f));
dis[1]=0;
vis[1]=1;
qu.push(1);
f[1]=INF;
pre[1]=-1;
while(!qu.empty())
{
u=qu.front();
qu.pop();
vis[u]=0;
for(p=h[u];p>=0;p=Edge[p].next)
{
v=Edge[p].v;
if(Edge[p].cap && dis[u]+Edge[p].cost<dis[v])
{
dis[v]=dis[u]+Edge[p].cost;
f[v]=min(f[u],Edge[p].cap);
pre[v]=p;
if(!vis[v])
{
qu.push(v);
vis[v]=1;
}
}
}
}
if(dis[n]==INF)
return 0;
C+=f[n]*dis[n];
F+=f[n];
if(F==2)
return 0;
for(p=pre[n];p>=0;p=pre[Edge[p^1].v])
{
Edge[p].cap-=f[n];
Edge[p^1].cap+=f[n];
}
return 1;
}
int main()
{
int i,j,k,u,v,cost;
while(~scanf("%d",&n) && n>0)
{
scanf("%d",&m);
memset(h,-1,sizeof(h));
tot=-1;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&cost);
AddEdge(u,v,cost,1);
AddEdge(v,u,-cost,0);
AddEdge(v,u,cost,1);
AddEdge(u,v,-cost,0);
}
C=F=0;
while(_spfa())
{
//printf("F %d %d\n",F,C);
};
if(F!=2)
printf("Back to jail\n");
else
printf("%d\n",C);
}
}