UVA 10806 Dijkstra, Dijkstra. (Minimum charge maximum flow)

Source: Internet
Author: User
Tags cos

UVA 10806 Dijkstra, Dijkstra. The main topic: you and your partner want to escape. Your partner goes to the scout first, and when your partner comes to the station, he will call you (the phone is hidden in the cake), and then you can run to the station where someone will pick you up. But because your partner was wearing a prison suit when he was running to the station, the streets he passed were under martial law and you had to run from the other streets. If you can get to the train station, please output the shortest time you and your partner will spend on the road, if not please "back to jail". Problem solving idea: minimum fee maximum flow. Set up a super source point to the prison (start 1), a capacity of 2 (two people), set a super meeting point, make the train station (end N) connect to him, the capacity is 2 (two people), the rest of the streets are non-direction (that is, forward to reverse all to consider), and capacity of 1 (each street can only Finally, the maximum flow, if the maximum flow is 2 of the output of the minimum fee, or go back to prison for the next jailbreak.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;typedef Long LongllConst intN = the;Const intINF =0x3f3f3f3f;intN, M, S, t;intA[n], Pre[n], d[n], inq[n];structedge{intFrom, to, cap, flow; llCos;}; vector<Edge>Edges vector<int>g[3* N];voidInit () { for(inti =0; I <3N    i++) g[i].clear (); Edges.clear ();}voidAddedge (intFromintTo,intCapintFlow, LLCos{Edges.push_back (Edge) {from, to, Cap,0,Cos}); Edges.push_back (Edge) {To, from,0,0, -Cos});intm = Edges.size (); G[from].push_back (M-2); G[to].push_back (M-1);}voidInput () {intFrom, to; ll cost; for(inti =0; I < m; i++) {scanf("%d%d%lld", &from, &to, &cost); Addedge (from, to,1,0, cost); Addedge (To, from,1,0, cost); } Addedge (0,1,2,0,0); Addedge (n, n +1,2,0,0);}intBF (intSintTint& Flow, ll& cost) { Queue<int>Q;memset(INQ,0,sizeof(INQ));memsetA0,sizeof(a));memset(Pre,0,sizeof(pre)); for(inti =0; I <=2* n +1;    i++) D[i] = INF; D[s] =0;    A[s] = INF; Inq[s] =1;intFlag =1; Pre[s] =0; Q.push (s); while(! Q.empty ()) {intU = Q.front ();        Q.pop (); Inq[u] =0; for(inti =0; I < g[u].size (); i++) {Edge &e = edges[g[u][i]];if(E.cap > E.flow && d[e.to] > D[u] + E.Cos) {d[e.to] = D[u] + E.Cos;                A[e.to] = min (A[u], e.cap-e.flow); Pre[e.to] = G[u][i];if(!inq[e.to]) {Inq[e.to] =1;                Q.push (e.to); }}} flag =0; }if(D[t] = = INF)return 0;    Flow + = A[t]; Cost + = (LL) d[t] * (LL) a[t]; for(intu = t; U! = S;        U = edges[pre[u]].from) {Edges[pre[u]].flow + = a[t]; edges[pre[u]^1].flow-= a[t]; }return 1;}intMCMF (intSintT, ll& cost) {intFlow =0; Cost =0; while(BF (S, t, flow, cost));returnFlow;}intMain () { while(scanf("%d", &n)! = EOF) {if(n = =0) Break;scanf("%d", &m); s =0, T = n +1;        Init ();        Input (); ll cost =0;intAns = MCMF (s, t, cost);if(ans = =1)printf("Back to Jail\n");Else printf("%lld\n", cost); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVA 10806 Dijkstra, Dijkstra. (Minimum charge maximum 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.