UVa 10806 Dijkstra, Dijkstra (minimum charge flow)

Source: Internet
Author: User

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 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 ED GE 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

/** UVa 10806 Dijkstra, Dijkstra * Seek two paths from S to T, no duplicate edges, and edge right and min * * Minimum cost flow, the cost of traffic is 2 o'clock is the answer, otherwise no solution*/#include<bits/stdc++.h>using namespacestd;structmincostmaxflow{Const Static intMAXN =10000; Const Static intMaxe =100000; Const Static intINF =0x3f3f3f3f; structEdge {int  from, To,next,cap,flow,cost; Edge () {} Edge (intUintVintCintFint_c,intNXT): from(U), to (v), Cap (c), Flow (f), Cost (_c), next (NXT) {}}edge[maxe]; intHead[maxn],tol,n; intPRE[MAXN],DIS[MAXN]; BOOLVIS[MAXN]; //Function    voidinit () {N=MAXN; Tol=0; memset (Head,-1,sizeof(head)); }    voidLinkintUintVintCapintCost//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++; }    BOOLSPFA (intSintT) {Queue<int>p;  for(intI=0; i<n;i++) dis[i]=INF;  for(intI=0; i<n;i++) vis[i]=false;  for(intI=0; i<n;i++) pre[i]=-1; Dis[s]=0; Vis[s]=true;        Q.push (S);  while(!Q.empty ()) {            intu=Q.front ();            Q.pop (); Vis[u]=false;  for(inti=head[u];i!=-1; i=Edge[i].next) {                intv=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; }    intMCMF (intSintTint&Cost ) { Cost=0; intmaxflow=0;  while(SPFA (s,t) &&maxflow<=2)        {            intmin=INF;  for(inti=pre[t];i!=-1; i=pre[edge[i^1].to]) Min=min (min,edge[i].cap-Edge[i].flow);  for(inti=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; }        returnMaxflow; }}MCMF;intMain () {intn,m; intu,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); intans; intF=MCMF. MCMF (0, n+1, ans); if(f<=1) printf ("Back to jail\n"); Elseprintf"%d\n", ans); }    return 0;}

UVa 10806 Dijkstra, Dijkstra (minimum charge flow)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.