POJ 1459 & ZOJ 1734 Power network (maximum stream)

Source: Internet
Author: User

http://poj.org/problem?id=1459

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1734

Power Network
Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 22674 Accepted: 11880

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.

Source

Southeastern Europe 2003


Test instructions

There are a total of n points, of which NP power station, NC users, the remaining is a transit point, there is a M cable (direction), the capacity limit on the cable, the power station has a generation limit, the user has the upper limit of power consumption, to find the biggest drain in the grid.

Analysis:

Obviously is the network maximum flow, the power station is the source point, the user is the meeting point, establishes the super source point and the Super meeting point, the super source point and the power station to have a forward edge, the capacity is the power station's generation limit, the user and the super meeting point has a forward edge, the capacity is the user's electricity consumption limit.


This problem I respectively with the EK algorithm and dinic algorithm implementation, found in the subject of the Dinic algorithm efficiency than the EK algorithm is nearly 20 times times higher! In Poj 2112, the dinic algorithm is more than 7 times times faster than the EK algorithm! Dinic is simply an artifact, and he is used in the future.

Comparing two algorithms, the EK algorithm is a BFS to find an augmented road; Dinic is a BFS to establish a hierarchical map, on the hierarchical map multiple DFS found several augmented paths, so as to reduce the number of BFS, so as to achieve higher efficiency.


The EK algorithm implements:

#include <cstdio> #include <iostream> #include <cstdlib> #include <algorithm> #include < ctime> #include <cctype> #include <cmath> #include <string> #include <cstring> #include < stack> #include <queue> #include <list> #include <vector> #include <map> #include <set> #define SQR (x) ((x) * (x)) #define LL long long#define ITN int#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#defi NE eps 1e-10#define maxm 23456#define maxn 107using namespace Std;int fir[maxn];int U[MAXM],V[MAXM],CAP[MAXM],FLOW[MAXM]    , Nex[maxm];int e_max;int p[maxn],q[maxn],d[maxn];void add_edge (int _u,int _v,int _w) {int E;    e=e_max++;    U[e]=_u;v[e]=_v;cap[e]=_w;    Nex[e]=fir[u[e]];fir[u[e]]=e;    e=e_max++;    u[e]=_v;v[e]=_u;cap[e]=0; Nex[e]=fir[u[e]];fir[u[e]]=e;}    int max_flow (int s,int t) {memset (flow,0,sizeof flow);    int total_flow=0;    for (;;)        {memset (d,0,sizeof D);        D[s]=inf;        int f=0,r=0;        Q[0]=s; WHile (f<=r) {int _u=q[f++];                    for (int e=fir[_u];~e;e=nex[e]) {if (!d[v[e]] && Cap[e]>flow[e]) {                    Q[++r]=v[e];                    P[v[e]]=e;                D[v[e]]=min (D[u[e]],cap[e]-flow[e]);        }}} if (d[t]==0) break; for (int e=p[t];;            E=p[u[e]]) {flow[e]+=d[t];            FLOW[E^1]-=D[T];        if (u[e]==s) break;    } Total_flow+=d[t]; } return total_flow;}    int main () {#ifndef Online_judge freopen ("/home/fcbruce/document/code/t", "R", stdin);    #endif//Online_judge int n,np,nc,m,_u,_v,_w;        while (~SCANF ("%d%d%d", &n,&np,&nc,&m)) {e_max=0;        int s=n,t=n+1;        memset (fir,-1,sizeof fir);            for (int i=0;i<m;i++) {scanf ("(%d,%d)%d", &_u,&_v,&_w);        Add_edge (_U,_V,_W); } for (int i=0;i<np;i++) {scanf ("(%d)%d", &_u,&_w);        Add_edge (S,_U,_W);            } for (int i=0;i<nc;i++) {scanf ("(%d)%d", &_u,&_w);        Add_edge (_U,T,_W);    } printf ("%d\n", Max_flow (s,t)); } return 0;}


Dinic Algorithm implementation:

#include <cstdio> #include <iostream> #include <cstdlib> #include <algorithm> #include < ctime> #include <cctype> #include <cmath> #include <string> #include <cstring> #include < stack> #include <queue> #include <list> #include <vector> #include <map> #include <set> #define SQR (x) ((x) * (x)) #define LL long long#define ITN int#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#defi NE eps 1e-10#define maxm 13000#define maxn 300using namespace Std;int g[maxn][maxn];int fir[maxn];int u[maxm],v[maxm],cap    [Maxm],flow[maxm],nex[maxm];int e_max;int lv[maxn],iter[maxn];int q[maxm];void add_edge (int _u,int _v,int _w) {int E;    e=e_max++;    U[e]=_u;v[e]=_v;cap[e]=_w;    Nex[e]=fir[u[e]];fir[u[e]]=e;    e=e_max++;    u[e]=_v;v[e]=_u;cap[e]=0; Nex[e]=fir[u[e]];fir[u[e]]=e;}    void Dinic_bfs (int s) {int f,r;    memset (lv,-1,sizeof LV);    Q[f=r=0]=s;    lv[s]=0;        while (f<=r) {int x=q[f++]; for (int e=fir[x];~e;e=nex[e]) {if (Cap[e]>flow[e] && lv[v[e]]<0) {Lv[v[e]]=lv[u [E]]                +1;            Q[++r]=v[e];    }}}}int Dinic_dfs (int _u,int t,int _f) {if (_u==t) return _f;            for (int &e=iter[_u];~e;e=nex[e]) {if (Cap[e]>flow[e] && lv[_u]<lv[v[e]) {            int _d=dinic_dfs (V[e],t,min (_f,cap[e]-flow[e]));                if (_d>0) {flow[e]+=_d;                Flow[e^1]-=_d;            return _d; }}} return 0;}    int max_flow (int s,int t) {memset (flow,0,sizeof flow);    int total_flow=0;    for (;;)        {DINIC_BFS (s);        if (lv[t]<0) return total_flow;        memcpy (iter,fir,sizeof iter);        int _f;    while ((_f=dinic_dfs (S,t,inf)) >0) Total_flow+=_f; } return total_flow;}    int main () {#ifndef Online_judge freopen ("/home/fcbruce/document/code/t", "R", stdin); #endif//ONLIne_judge int l=inf,r=0,k,c,m;    scanf ("%d%d%d", &k,&c,&m); for (int i=1;i<=k+c;i++) for (int j=1;j<=k+c;j++) scanf ("%d", &g[i][j]), G[i][j]=g[i][j]?    G[i][j]:inf; for (int k=1;k<=k+c;k++) for (int i=1;i<=k+c;i++) for (int j=1;j<=k+c;j++) g[i][    J]=min (G[i][k]+g[k][j],g[i][j]);    int ans=-1;//printf ("%d%d\n", l,r);    L=0;r=inf;        while (l<=r) {int mid=l+r>>1;        e_max=0;        memset (fir,-1,sizeof fir);        for (int i=k+1;i<=k+c;i++) Add_edge (0,i,1);        for (int i=1;i<=k;i++) Add_edge (i,k+c+1,m);                    for (int i=k+1;i<=k+c;i++) for (int j=1;j<=k;j++) if (g[i][j]<=mid)        Add_edge (i,j,1);//printf ("mid=%d\n", mid);            if (Max_flow (0,k+c+1) ==c) {ans=mid;        R=mid-1;        } else {l=mid+1; }} printf ("%d\n", ans); 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.