Poj_1459_power Network_ Network Flow problem

Source: Internet
Author: User
POJ 1459
Power Network
Time limit:2000ms Memory limit:32768k
Total submissions:26441 accepted:13745

Description
A power network consists of nodes (power stations, consumers and dispatchers) connected by
Power transport Lines. A node u May is supplied with A amount s (u) >= 0,
May produce a amount 0 <= P (u) <= pmax (U) power, may consume a amount 0 <= C (u) <= min
(S (u), Cmax (U)) of power, and could deliver an amount D (u) =s (u) +p (U)-C (U) of power.
The following restrictions Apply:c (U) =0 for all power station, P (U) =0 for any consumer,
and P (u) =c (u) =0 for any dispatcher. There is at most one Power transport line (U,V) from
A node u to a node V in the net; It transports a amount 0 <= L (u,v) <= Lmax (u,v) of power
Delivered by U to 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 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 of that L (u,v) =x and Lmax (u,v) =y. The power consumed is con=6. Notice that
There are other possible states of the network but the value of Con cannot exceed 6.

Input
There are 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
(consumers), 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 <= 1000 is the value
of Lmax (U,V). Follow np doublets (u) z, where U is the identifier's a power station and
0 <= Z <= 10000 is the value of Pmax (U). The data set ends with NC doublets (u) z, where
U is the identifier of a consumer and 0 <= Z <= 10000 is the value of Cmax (U). All
Input numbers are integers. Except the (u,v) z Triplets and the (U) z doublets, which
Don't contain white spaces, white spaces can occur freely in input. Input data Terminate
With a end of file and are correct.

Output
For each data set from the input, the program prints on the standard output the maximum
Amount of power that can is consumed in the corresponding network. Each result has an
Integral value and is printed to the beginning of a separate line.

Sample Input

2 1 1 2 (0,1) 20 (1,0) 10 (0) 15 (1) 20
7 2 3 13 (0,0) 1 (0,1) 2 (0,2) 5 (1,0) 1 (1,2) 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

15
6
*/
/*
The first four numbers represent respectively, M Point, NP power Station, NC user, n Edge
Next is N edge------(U,V) w,w represents the maximum flow of the edge (U,V);
The NP power station---(u) w,w represents the maximum flow rate the Power station u can provide;
NC User Information (v) W,W represents the maximum amount of traffic that each user v can accept.

The network flow problem adds the source point meeting point, transforms into the general maximum network flow problem

*/

#include <stdio.h> #include <string.h> #include <queue> using namespace std;
const int N = 105;
const int inf = 0X3F3F3F3F;
int g[n][n];
int flow[n][n];
int p[n];
int x[n];
int s,e;
	int Edmonds_karp () {memset (flow,0,sizeof (flow));
	Queue<int> Q;
	int f = 0,v,u;
		while (1) {memset (x,0,sizeof (x));
		X[s] = inf;
		Q.push (s); while (!
			Q.empty ()) {u = Q.front ();
			Q.pop ();
					for (v=s;v<=e;v++) {if (!x[v] && g[u][v]>flow[u][v]) {p[v] = u;
					Q.push (v);
				X[v] = min (x[u],g[u][v]-flow[u][v]);
		}} if (x[e]==0) {return F;
			for (int i=e;i!=s;i=p[i]) {Flow[p[i]][i] + = x[e];
		Flow[i][p[i]] = x[e];
	} f + + x[e];
	int main () {int n,a,b,m,i;
		while (scanf ("%d%d%d%d", &n,&a,&b,&m)!=eof) {int u,v,w;
		memset (G,0,sizeof (g));
			for (i=1;i<=m;i++) {scanf ("(%d,%d)%d", &u,&v,&w);
		G[++U][++V] = W; for (i=1;i<=a;i++) {scanf ("(%d)%d",&u,&W);
		G[0][++u] = W;
			for (i=1;i<=b;i++) {scanf ("(%d)%d", &v,&w);
		G[++v][n+1] = W;
		} n++; s = 0;
		e = n;
	printf ("%d\n", Edmonds_karp ());
return 0; }


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.