Solution report
Ideas:
From S-> T and then from T-> S is equivalent to s-> T. Each route must be taken only once, and the minimum cost is required to set the size of each edge to 1, running expense Flow
As long as the traffic is 2, it will end.
#include <iostream>#include <cstring>#include <cstdio>#include <queue>#define inf 0x3f3f3f3f#define N 5000#define M 50000using namespace std;int n,m,s,t,cnt,head[N],pre[N],vis[N],dis[N],f[N],cost,flow;struct node { int v,cost,cap,next;} edge[M];void add(int u,int v,int cost,int cap) { edge[cnt].v=v,edge[cnt].cost=cost,edge[cnt].cap=cap; edge[cnt].next=head[u],head[u]=cnt++; edge[cnt].v=u,edge[cnt].cost=-cost,edge[cnt].cap=0; edge[cnt].next=head[v],head[v]=cnt++;}int _spfa() { for(int i=1; i<=n; i++) { dis[i]=inf; vis[i]=pre[i]=f[i]=0; } dis[1]=0; vis[1]=1; f[1]=inf; pre[1]=-1; queue<int >Q; Q.push(1); while(!Q.empty()) { int u=Q.front(); Q.pop(); vis[u]=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(edge[i].cap&&dis[v]>dis[u]+edge[i].cost) { dis[v]=dis[u]+edge[i].cost; f[v]=min(f[u],edge[i].cap); pre[v]=i; if(!vis[v]) { vis[v]=1; Q.push(v); } } } } if(dis[n]==inf)return 0; cost+=f[n]*dis[n]; flow+=f[n]; if(flow==2)return 0; for(int i=pre[n]; i!=-1; i=pre[edge[i^1].v]) { edge[i].cap-=f[n]; edge[i^1].cap+=f[n]; } return 1;}void mcmf() { cost=flow=0; while(_spfa());}int main() { int i,j,u,v,w; while(~scanf("%d",&n)) { if(!n)break; memset(head,-1,sizeof(head)); cnt=0; scanf("%d",&m); while(m--) { scanf("%d%d%d",&u,&v,&w); add(u,v,w,1); add(v,u,w,1); } mcmf(); if(flow!=2||cost==inf) printf("Back to jail\n"); else printf("%d\n",cost); } return 0;}
Problem?
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 your SS 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 fromSToTAnd back without using the same edge twice.
Input
The input will contain several test cases. Each test case will begin with an integerN(2 <=N<= 100)-the number of nodes (intersections). The jail is at node number 1, and the train station is at node numberN. The next line will contain an integerM-The number of streets. The nextMLines will describeMStreets. 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 |
211 2 999331 3 102 1 203 2 509121 2 101 3 101 4 102 5 103 5 104 5 105 7 106 7 107 8 106 9 107 9 108 9 100 |
Back to jail80Back to jail |
Problemsetter: Igor naverniouk
Uva10806_dijkstra, Dijkstra. (network stream/fee stream) (graph theory topic of the book)