Title Link: http://poj.org/problem?id=1459
Test instructions
A total of n points, M edge, there is a NP point is a gas station is the source point, there are NC points is a point of consumption, that is, meeting point.
Gives the capacity of the M-bar, the maximum outflow per source point, and the maximum inflow per sink point.
Can consume as much as you please.
Ideas:
A multi-source multi-sink network stream is the addition of a super-source point and a super-sink point.
An edge is attached to each source point by the source point, and the capacity is the maximum amount of the source point outflow.
Connect each meeting point to a super meeting point with a capacity of the maximum inflow specified for that meeting point.
Then the maximum flow is calculated.
1 //#include <bits/stdc++.h>2#include <iostream>3#include <cstring>4#include <cstdio>5#include <vector>6#include <queue>7 using namespacestd;8 intN, m, S, T;9 #defineMAXN 110Ten #defineINF 0x3f3f3f3f One structEdge A { - int from, to, cap, flow; -Edge (intFintTintCintFL) the { - from= f; to = t; Cap = C; Flow =FL; - } - }; +Vector <Edge>edges; -Vector <int>G[MAXN]; + intD[MAXN], VIS[MAXN], CUR[MAXN]; A voidAddedge (int from,intTo,intcap) at { -Edges.push_back (Edge ( from, to, Cap,0)); -Edges.push_back (Edge to, from,0,0)); -m =edges.size (); -g[ from].push_back (M-2); -G[to].push_back (M-1); in } - BOOLBFS () to { +memset (Vis,0,sizeof(Vis)); -D[s] =0; theVis[s] =1; *Queue <int>Q; $ Q.push (s);Panax Notoginseng while(!q.empty ()) - { the intx =Q.front (); Q.pop (); + for(inti =0; I < g[x].size (); i++) A { theEdge &e =Edges[g[x][i]]; + if(!vis[e.to] && e.cap >E.flow) - { $D[e.to] = D[x] +1; $Vis[e.to] =1; - Q.push (e.to); - } the } - }Wuyi returnVis[t]; the } - intDfsintXinta) Wu { - if(x = = T | | a = =0)returnA; About intFlow =0, F; $ for(int&i = Cur[x]; I < g[x].size (); i++) - { -Edge &e =Edges[g[x][i]]; - if(d[e.to] = = D[x] +1&& (f = dfs (e.to, Min (A, e.cap-e.flow))) >0) A { +E.flow + =F; theedges[g[x][i]^1].flow-=F; -Flow + =F; $A-=F; the if(A = =0) Break; the } the } the returnflow; - } in intMaxflow () the { the intFlow =0; About while(BFS ()) the { thememset (cur,0,sizeof(cur)); theFlow + =DFS (s, INF); + } - returnflow; the }Bayi intN, NP, NC, M; the intMain () the { - //freopen ("In.txt", "R", stdin); - //freopen ("OUT.txt", "w", stdout); the while(~SCANF ("%d%d%d%d", &n, &NP, &NC, &M)) the { the edges.clear (); the for(inti =0; I <= n+1; i++) g[i].clear (); - intA, B, C;Charop; the for(inti =1; I <= M; i++) the { thecin>>op; scanf"%d", &a); 94cin>>op; scanf"%d", &b); thecin>>op; scanf"%d", &c); the Addedge (A, B, c); the }98s = N; t = n+1; About for(inti =1; I <= NP; i++) - {101cin>>op; scanf"%d", &a);102cin>>op; scanf"%d", &c);103 Addedge (S, a, c);104 } the for(inti =1; I <= NC; i++)106 {107cin>>op; scanf"%d", &a);108cin>>op; scanf"%d", &c);109 Addedge (A, t, c); the }111n = n+2; the intFlow =Maxflow ();113printf"%d\n", flow); the } the return 0; the}
POJ 1459 Power Network Multi-source multi-stream networks