UVA 10594 Data Flow topic: Give a picture, and D, K,d represents the amount of data to be transmitted, K represents the amount of data that each edge can transmit (that is, the capacity), asking the minimum time required to transmit all the data. Problem solving ideas: Build a super source point to the source point 1, the capacity is D, and then the minimum cost of the maximum flow of the graph. Finally, the maximum flow is compared with D, which is smaller than D output inpossible, otherwise the output minimum fee.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;typedef Long LongllConst intN =5005;Constll INF =1e18;intN, M, S, T;ll K, D;structrec{ll U, V,Cos;} Rec[n];intPre[n], inq[n]; ll A[n], d[n];structedge{intFrom, to; ll cap, flow; llCos;}; vector<Edge>Edges vector<int>G[n];voidInit () { for(inti =0; i < N; i++) g[i].clear (); Edges.clear ();}voidAddedge (intFromintTo, ll cap, ll flow, 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 () { for(inti =0; I < m; i++) {scanf("%lld%lld%lld", &rec[i].u, &REC[I].V, &rec[i].Cos); }scanf("%lld%lld", &d, &k); for(inti =0; I < m; i++) {Addedge (rec[i].u, REC[I].V, K,0, Rec[i].Cos); Addedge (REC[I].V, rec[i].u, K,0, Rec[i].Cos); } Addedge (0,1D0,0);}intBF (intSintT, ll& Flow, ll& cost) { Queue<int>Q;memset(INQ,0,sizeof(INQ));memsetA0,sizeof(a));memset(Pre,0,sizeof(pre)); for(inti =0; I <= N; 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) {ll flow =0; Cost =0; while(BF (S, t, flow, cost));returnFlow;}intMain () { while(scanf("%d%d", &n, &m) = =2) {init (); Input (); s =0, t = N; ll cost;intAns = MCMF (s, t, cost);if(Ans < D)printf("impossible.\n");Else printf("%lld\n", cost); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA 10594 Data flow (minimum fee max Stream + problem with wrong)