UVA 10806 Dijkstra, Dijkstra.

Source: Internet
Author: User

Test instructions

From the beginning to the end, and then from the end to the beginning, which can not go through the same side at the same time, ask you the minimum path length. Enter the end point N, start at 1, and then enter M, which represents the M edge. Each edge consists of a starting point, an end point, and a length.

Analysis:

The minimum length is also limited to one time per road, so you can use network flow to connect. Length is the side cost, each side of the traffic is 1, so that each side can only walk once. From the starting point to the end point and then from the end to the beginning, equivalent to the beginning of an outside starting point, the flow of 2,.

Code:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<queue>UsingNamespace Std;Constint MAXN=110;Constint INF=1e9;int nM, ST;int INQ[MAXN];int D[MAXN],p[MAXN],a[MAXN];int flow, cost;struct Edge{int from, to, Cap, Flow, cost;}; vector<edge>edges; vector<Int>g[MAXN];voidInit(){Flow=cost=s=0; T=n;For(int I=0; I<t+1; I+ +) G[I].Clear(); Edges.Clear();}voidAdd(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});int NC=edges.Size(); G[From].Push_back(NC-2); G[To].Push_back(NC-1);}boolBell(Int& Flow,Int& Cost){For(int I=0; I<=t; I+ +) d[I]=inf;Memset(Inq,0,sizeof(Inq)); D[s]=0; Inq[s]=1; P[s]=0; A[s]=inf; 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, T]=Min(A[u],e. cap-E. Flow);If(!inq[E, T]){Q.Push(E, T); Inq[E, T]=1;}}}}If(d[t]==inf)ReturnFalse; Flow+=a[t]; Cost+=d[t]*a[t];int U=t;While(U!=s){cout<<3<<endl; Edges[P[u]].flow+=a[t]; Edges[P[u]^1].flow-=a[t]; U=edges[P[u]].from;}ReturnTrue;}IntMain(){While(scanf("%d", &n), n){scanf("%d", &m);Init();int aBC;Add(0,1,2,0);While(M--){scanf("%d%d%d", &a, &b, &c);Add(AB,1C);Add(bA,1C);}cout<<1<<endl;While(flow, cost); //cout<<2<<endl; if(flow<2) printf("back to jail\ n"); Else printf("%d\ n", cost); }}< /c8> 

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

Output:

Back to jail

80

Back to jail

UVA 10806 Dijkstra, Dijkstra.

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.