Power Network
| Time Limit: 2000MS |
|
Memory Limit: 32768K |
| Total Submissions: 25108 |
|
Accepted: 13077 |
Description
A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node U is supplied with an amount s (U) >= 0 of the power, may produce an amount 0 <= P (u) <= pmax (U) of power, May consume a amount 0 <= C (u) <= min (S (u), Cmax (U)) of power, and may deliver an amount D (u) =s (u) +p (U)-C (U) of Powe R. The following restrictions Apply:c (U) =0 for any power station, p (U) =0 for any consumer, and P (u) =c (u) =0 for any Dispat Cher. There is at most one Power transport line (U,V) from a node u to a node V in the net; It transports an amount 0 <= L (u,v) <= Lmax (u,v) of the Power delivered by U v. Let Con=σuc (U) is the power consumed in the net. The problem is to compute the maximum value of Con.
An example was in Figure 1. The label X/y of power station U shows that P (u) =x and Pmax (U) =y. The label x/y of consumer U shows that C (U) =x and Cmax (U) =y. The label X/y of Power Transport Line (U,V) shows that L (u,v) =x and Lmax (u,v) =y. The power consumed is con=6. Notice that there is other possible states of the network but the value of Con cannot exceed 6.
Input
There is several data sets in the input. Each data set encodes a power network. It starts with four integers:0 <= n <= (nodes), 0 <= NP <= N (power stations), 0 <= NC <= N (consum ERS), and 0 <= m <= n^2 (Power transport Lines). Follow M data triplets (u,v) z, where u and v are node identifiers (starting from 0) and 0 <= z <= of Lmax (U,V). Follow NP doublets (U) z, where U is the identifier of a power station and 0 <= Z <= 10000 is the value of Pmax (U). The data set ends with NC doublets (u) z, where u are the identifier of a consumer and 0 <= Z <= 10000 is the value of Cmax (U). All input numbers is integers. Except the (u,v) z Triplets and the (U) z doublets, which do not contain white spaces, white spaces can occur freely in Inpu T. Input data terminate with an end of file and is correct.
Output
For each data set from the input, the program prints on the standard output the maximum amount of power that can is consum Ed in the corresponding network. Each result have an integral value and are printed from the beginning of a separate line.
Sample Input
2 1 1 2 (0,1) (1,0) ten (0) (1) 207 2 3 (0,0) 1 (0,1) 2 (0,2) 5 (1,0) 1 (8) (2,3) 1 (2,4) 7 (3,5) 2 (3,6) 5 (4,2) 7 (4,3 ) 5 (4,5) 1 (6,0) 5 (0) 5 (1) 2 (3) 2 (4) 1 (5) 4
Sample Output
156
Hint
The sample input contains the data sets. The first data set encodes a network with 2 nodes, power station 0 with Pmax (0) =15 and Consumer 1 with Cmax (1) =20, and 2 p Ower Transport lines with Lmax (0,1) =20 and Lmax (1,0) =10. The maximum value of Con is 15. The second data set encodes the network from Figure 1.
The topic gives a lot of nonsense, especially the sign S (u), D (U), con and that formula are all to ignore, confuse
The difficulty lies in the composition
Power Plant P (U) are source points, User C (U) are meeting points, transit stations when the ordinary point processing
Both the node and the edge have X/Y (flow and capacity), which can easily cause contradiction (because the maximum flow problem is that only the edges have traffic and capacity.) However, it is not difficult to find that the example given in the graph has multiple source points, multiple sinks, multiple ordinary points, only the source and meeting points are marked with X/y, the normal point is not marked X/y, and all the edges given are X/Y. This is undoubtedly prompting us to make a variant of the diagram: suggest a super source S, a super sink T, so that s point to all source points, and the source point capacity y as the capacity of these edges, so that all sinks point to T, and the sink point capacity y respectively as the capacity of these edges, and then the source and sink point is the point, all become ordinary points. In this way, "multi-source multi-sinks maximum flow" is transformed into "single-source single-sink maximum flow" problem.
To learn the maximum flow problem, you will find that the traffic value on the edge is given the initial value, but the input of this problem is only capacity, no traffic, many people immediately feel the impossible to start. In fact, the amount of traffic on the edge of the initial value is not so-called, the solution of the maximum flow needs only the capacity. But generally, for the sake of convenience, the flow of all sides is initialized to0. One of the biggest benefits of doing this is to avoid Reverse Arc 's existence,
The above analysis comes from http://www.cnblogs.com/lyy289065406/archive/2011/07/30/2122116.html
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < queue> #define MAXN 300#define maxm 100000#define INF 0x3f3f3f3fusing namespace Std;int HEAD[MAXN], CUR[MAXN], Cnt;int D IST[MAXN], Vis[maxn];int N, NP, NC, m;struct node{int u, V, cap, flow, next;}; Node Edge[maxm];void init () {cnt = 0; Memset (Head,-1, sizeof (head));} void Add (int u, int v, int w) {edge[cnt] = {u, V, W, 0, Head[u]}; Head[u] = cnt++; EDGE[CNT] = {V, u, 0, 0, Head[v]}; HEAD[V] = cnt++;} void Getmap () {int u, V, W; while (m--) {scanf ("(%d,%d)%d", &u, &v, &w);//Note there is a space add (U, V, W); } while (np--) {scanf ("(%d)%d", &u, &w); Add (N, u, W);//n is the source point, source Point and plant Connection} while (nc--) {scanf ("(%d)%d", &u, &w); Add (u, n + 1, W); n + 1 for meeting point, Consumer and meeting point connection}}bool BFS (int st, int ed) {queue<int>q; memset (Vis, 0, sizeof (VIS)); memset (Dist,-1, sizeof (Dist)); Vis[St] = 1; DIST[ST] = 0; Q.push (ST); while (!q.empty ()) {int u = q.front (); Q.pop (); for (int i = head[u]; i =-1; i = Edge[i].next) {node E = Edge[i]; if (!VIS[E.V] && e.cap > E.flow) {vis[e.v] = 1; DIST[E.V] = Dist[u] + 1; if (e.v = = ed) return true; Q.push (E.V); }}} return false;} int DFS (int x, int ed, int a) {if (x = = Ed | | a = = 0) return A; int flow = 0, F; for (int &i = cur[x]; I! =-1; i = Edge[i].next) {node &e = Edge[i]; if (dist[e.v] = = Dist[x] + 1 && (f = DFS (e.v, ed, Min (A, e.cap-e.flow))) > 0) {e.flow + = f; edge[i ^ 1].flow-= f; A-= f; Flow + + F; if (a = = 0) break; }} return flow;} int Maxflow (int st, int ed) {int flowsum = 0; while (BFS (st,ed)) {memcpy (cur, head, sizeof(head)); Flowsum + = DFS (St, Ed, INF); } return flowsum;} int main () {while (scanf ("%d%d%d%d", &n, &NP, &NC, &m)! = EOF) {init (); Getmap (); printf ("%d\n", Maxflow (n, n + 1)); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1459 && ZOJ 1734--power Network "Maximum Flow Dinic"