POJ 1459 Power Network "building a super Source point, super Meeting point"

Source: Internet
Author: User

Power Network
Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 25514 Accepted: 13287

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

Topic meaning: Give n nodes (number 0 to n-1), these points have power station (only responsible for generating power), consumption station (only responsible for the consumption of electricity), the transfer station (only responsible for transshipment electricity). Now give the maximum capacity of each power station, the maximum consumption of the stations, the maximum amount of transfer, so that you can find out the consumption of the maximum capacity of the station.    idea: To establish a super-source 0 node         Only connect to all power stations, the maximum power generation capacity for this edge.             set up the Super sink n+1 node    Only connect all the consumption stations, The consumption station's maximum consumption is the capacity on this edge. This translates into the problem of maximizing the flow of the super-source to the super-sink. Note: When adding edges, add one to the number since the super source is 0 nodes, and the node given in the topic starts from 0.  
#include <stdio.h> #include <string.h> #include <stack> #include <queue> #include <algorithm > #define MAX 1100#define maxm 40010#define INF 0x7fffffusing namespace std;struct node{int from,to,cap,flow,next;} Edge[maxm];int n,m,np,nc;int ans,head[max];int vis[max];//use BFS to determine whether the current point in the queue, int dis[max];//The current point to the source point of the distance int cur[max];/    /Save the node is participating in the computed arc to avoid repeating the calculation of void init () {ans=0; memset (head,-1,sizeof (Head));} void Add (int u,int v,int W) {node E1={u,v,w,0,head[u]};edge[ans]=e1;head[u]=ans++;node E2={v,u,0,0,head[v]};edge[ans] =e2;head[v]=ans++;} void Getmap () {int A, B, D;while (m--) {scanf ("(%d,%d)%d", &a, &b, &d); Add (a+1, b+1, D);} while (np--) {scanf ("(%d)%d", &b, &d), add (0, b+1, d);//Super Source}while (nc--) {scanf ("(%d)%d", &a, &d); Add (a+1    , n+1, D);//Super sink}}int BFS (int beg,int end) {int i;    memset (vis,0,sizeof (VIS));    memset (dis,-1,sizeof (dis));    queue<int>q;    while (!q.empty ()) Q.pop ();    Vis[beg]=1;    dis[beg]=0;    Q.push (Beg); WhIle (!q.empty ()) {int U=q.front ();        Q.pop ();            for (I=head[u];i!=-1;i=edge[i].next)//traverse all the sides connected to u {node e=edge[i];                if (!vis[e.to]&&e.cap>e.flow)//If the edge is not accessed and the traffic is not full continue operation {dis[e.to]=dis[u]+1;//establishing a hierarchy chart                                                                                                                             vis[e.to]=1;//marks the current point                if (e.to==end)//If the current point searches to the end, stop searching returns 1 indicating that there is a path from the origin to the meeting point return 1; Q.push (e.to);//Queue the current Point}}} return 0;//return 0 indicates that the path from the source point to the sink point is not found}int dfs (int x,int a,int end)//Put the found edge on the    There is current flow plus a (a is the minimum residual flow in this path) {//int i; if (x==end| |    a==0)//If the search ends or the minimum residual flow is 0 return a;    int flow=0,f;        for (int& I=cur[x];i!=-1;i=edge[i].next)//i starting from the last end of the arc {node& e=edge[i]; if (dis[e.to]==dis[x]+1&& (F=dfs (E.to,min (A,e.cap-e.flow), end)) >0)//If {//BFS We have already established a hierarchy chart, now if DIS[E.TO] ==dis[x]+1 said we found it.Path//If the dfs>0 indicates a minimum residual flow, we will always find the minimum residual flow of 0 e.flow+=f;//positive edge current flow plus the smallest residual flow edge[i^1].flow-=f;//reverse        flow+=f;//total flow to edge plus f a-=f;//min. increment flow minus F if (a==0) break;    The value}int maxflow (int beg,int end) {int flow=0, after all edges plus the minimum residual traffic flow;//}}    while (BFS (beg,end))//exists Shortest path {memcpy (cur,head,sizeof (head));//copy Array Flow+=dfs (beg,inf,end);    } return flow;//maximum flow}int main () {int i,j;        while (scanf ("%d%d%d%d", &n,&np,&nc,&m)!=eof) {init ();        Getmap ();    printf ("%d\n", Maxflow (0,n+1)); } return 0;}

  

POJ 1459 Power Network "building a super Source point, super Meeting point"

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.