UVALive-2957 Bring them there (maximum flow graph theory modeling)

Source: Internet
Author: User

The main problem: There are n planets, your task is to use the shortest time to the K supercomputer from Planet S to the planet T. Each supercomputer needs a spaceship to transport it, there are m two-way tunnels between the planets, each tunnel takes a day to pass, and no two ships can use the same tunnel at the same time, and the tunnel does not connect two identical planets, and there is at most one tunnel between each pair of planets.

The idea of solving problems: according to Great White books, the idea is to dismantle
For example, the delivery time is T, then each point u split into T + 1, respectively, u0, U1 ... uT, respectively, corresponds to the first day of the U Planet
For each given edge (U,V), assuming that I day, even the edge ui–>vi+1 and vi–>ui+1, two sides of the capacity of 1
You also have to add a point ui->ui+1, which is INF, which means that the ship is not moving forward.
The calculation of the process, the gradual expansion of the graph, day-by-day enumeration, the day-to-day expansion until the flow of K
Note here, at some point, a–>bi+1 and bi–>ai+1 at the same time the flow is not allowed, here, if there is a flow in two directions, then we are equivalent to the two spacecraft to stay on planet A and Planet B each day, so it will not be contradictory

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace STD;#define M 1000010#define N 10010#define INF 0x3f3f3f3fstructedge{intU, V, cap, flow, next;} E[M];structdinic{intHead[n], d[n];intTot, sink, source;voidInit () {memset(Head,-1,sizeof(head)); tot =0; }inline voidAddedge (intUintVintCAP) {e[tot].u = u; E[TOT].V = v; E[tot].cap = cap; E[tot].flow =0; E[tot].next = Head[u];        Head[u] = tot++; u = u ^ v; v = u ^ v;        u = u ^ v; e[tot].u = u; E[TOT].V = v; E[tot].cap =0; E[tot].flow =0; E[tot].next = Head[u];    Head[u] = tot++; }inline BOOLBFsints) {intU, v;memset(d,0,sizeof(d)); Queue<int>Q;        Q.push (s); D[s] =1; while(! Q.empty ()) {u = Q.front (); Q.pop ();if(U = = sink)return true; for(inti = Head[u]; ~i; i = e[i].next) {v = e[i].v;if(!d[v] && e[i].cap-e[i].flow >0) {D[v] = D[u] +1;                Q.push (v); }            }        }return false; }intDfsintXintA) {if(x = = Sink | | a = =0)returnAintF, Flow =0; for(inti = head[x]; ~i; i = e[i].next) {intv = e[i].v;if(D[v] = = D[x] +1&& E[i].cap-e[i].flow >0) {f = Dfs (V, min (A, e[i].cap-e[i].flow));                E[i].flow + = f; e[i^1].flow-= f;                Flow + + F; A-= f;if(!a) Break; }        }if(Flow = =0) D[x] =0;returnFlow }intMaxflow (intSourceintSinkintNeed) {intFlow =0; This->sink = sink; while(BFS (source)) {Flow + = DFS (source, need-flow);if(Flow >= need)returnFlow }returnFlow }};D inic dinic;structNode {intx, y;} Node[n];intN, M, K, S, t;intPos[n];BOOLVis[n];voidPrintintCNT) { for(inti =1; I <= K; i++) Pos[i] = s;printf("%d\n", CNT); for(inti =1; I <= CNT; i++) { vector<int>U, v;memset(Vis,0,sizeof(VIS)); for(intj =0; J <4* m; J + =4) {intp = j + (I-1) *4* m + n * I *2;//positive flow, reverse flow, i.e. U-->v            if(E[p].flow &&!) e[p+2].flow) {U.push_back (e[p ^1].V-(I-1) * n);            V.push_back (E[P].V-I * n); }//Reverse flow, positive no flow, that is, v-->u, so that the two sides have to exclude the flow of the situation, the equivalent of two points of exchange, that is, both ships stay in situ            Else if(! E[p].flow && E[p +2].flow) {u.push_back (e[(p +2) ^1].V-(I-1) * n); V.push_back (E[p +2].v-i * N); }        }printf("%d", U.size ()); for(intp =0; P < u.size (); p++) for(intj =1; J <= K; J + +) {if(!vis[j] && pos[j] = = U[p]) {Vis[j] =1;printf("%d%d", J, V[p]); POS[J] = v[p]; Break; }            }printf("\ n"); }}voidSolve () { for(inti =0; I < m; i++)scanf("%d%d", &node[i].x, &AMP;NODE[I].Y);intCNT =0, Maxflow =0, sink = t; Dinic.init (); while(Maxflow < k) {++cnt;//stay where you are         for(inti =1; I <= N; i++) Dinic. Addedge (i + (CNT-1) * N, i + CNT * n, INF);//Side         for(inti =0; I < m; i++) {dinic. Addedge (node[i].x + (CNT-1) * N, NODE[I].Y + cnt * N,1); Dinic. Addedge (Node[i].y + (CNT-1) * N, node[i].x + cnt * N,1);        } sink + = n; Maxflow + = Dinic.    Maxflow (S, sink, k-maxflow); } print (CNT);}intMain () { while(scanf("%d%d%d%d%d", &n, &m, &k, &s, &t)! = EOF) solve ();return 0;}

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

UVALive-2957 Bring them there (maximum flow graph theory modeling)

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.