POJ 1459 Power Network(多源点/汇点最大流问题)

题目链接:http://poj.org/problem?id=1459

题目:

Description

A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0 <= p(u) <= pmax(u) of power, may consume an 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 power. 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 dispatcher. 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 power delivered by u to v. Let Con=Σuc(u) be the power consumed in the net. The problem is to compute the maximum value of Con. 

An example is 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 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 <= 100 (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 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 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 do not contain white spaces, white spaces can occur freely in input. Input data terminate with an 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 be consumed in the corresponding network. Each result has an integral value and is printed from 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

Hint

The sample input contains two 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 power 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.

题意:上个题目是求从一个点(源点)到另一个点(汇点)的最大流,即源点可以看成无限流形态。这道题目,要求的是从nc个源点发出(这些源点发出流量有限制),汇聚到np个汇点(这些汇点能接收的流量也是有限制的)。

题解:我们可以建立一个超级源点(无限流的那种),把它和那些有限流的源点连接,同理建立一个超级汇点,于是就转成一道和上一题一样的简单最大流啦。(板子一拍...)

EK算法实现:(1547ms)cin读入过了,scanf()读入却TLE了...

 1 //EK算法
 2 #include <queue>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7
 8 const int N=233;
 9 const int INF=0x3f3f3f3f;
10 int n,m,s,t,Map[N][N],path[N],flow[N];
11
12 int bfs(){
13     int tmp;
14     queue <int> Q;
15     while(!Q.empty()) Q.pop();
16     memset(path,-1,sizeof(path));
17     path[s]=0;flow[s]=INF;
18     Q.push(s);
19     while(!Q.empty()){
20         tmp=Q.front();Q.pop();
21         if(tmp==t) break;
22         for(int i=0;i<=n+1;i++){
23             if(i!=s&&path[i]==-1&&Map[tmp][i]){
24                 flow[i]=flow[tmp]<Map[tmp][i]?flow[tmp]:Map[tmp][i];
25                 Q.push(i);
26                 path[i]=tmp;
27             }
28         }
29     }
30     if(path[t]==-1) return -1;
31     return flow[n+1];
32 }
33
34 int Edmonds_Karp(){
35     int max_flow=0,step,now,pre;
36     while((step=bfs())!=-1){
37         max_flow+=step;
38         now=t;
39         while(now!=s){
40             pre=path[now];
41             Map[pre][now]-=step;
42             Map[now][pre]+=step;
43             now=pre;
44         }
45     }
46     return max_flow;
47 }
48
49 int main(){
50     int np,nc;
51     while(cin>>n>>np>>nc>>m){
52         memset(Map,0,sizeof(Map));
53         char zf;
54         int a,b,c;
55         s=n;t=n+1;
56         for(int i=1;i<=m;i++){
57             cin>>zf>>a>>zf>>b>>zf>>c;
58             Map[a][b]=c;
59         }
60         for(int i=1;i<=np;i++){
61             cin>>zf>>b>>zf>>c;
62             Map[s][b]=c;
63         }
64         for(int i=1;i<=nc;i++){
65             cin>>zf>>a>>zf>>c;
66             Map[a][t]=c;
67         }
68         cout<<Edmonds_Karp()<<endl;
69     }
70     return 0;
71 }

优化(当前弧优化)版Dinic算法实现:(891ms)

 1 //Dinic算法
 2 #include <queue>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7
 8 const int N=233;
 9 const int M=23333;
10 const int INF=0x3f3f3f3f;
11 int n,m,s,t,cnt;
12 int Head[N],Depth[N],Next[M],V[M],W[M],cur[N];
13
14 void init(){
15     cnt=-1;
16     memset(Head,-1,sizeof(Head));
17     memset(Next,-1,sizeof(Next));
18 }
19
20 void add_edge(int u,int v,int w){
21     cnt++;
22     Next[cnt]=Head[u];
23     V[cnt]=v;
24     W[cnt]=w;
25     Head[u]=cnt;
26     cnt++;
27     Next[cnt]=Head[v];
28     V[cnt]=u;
29     W[cnt]=0;
30     Head[v]=cnt;
31 }
32
33 bool bfs(){
34     queue <int> Q;
35     while(!Q.empty()) Q.pop();
36     memset(Depth,0,sizeof(Depth));
37     Depth[s]=1;
38     Q.push(s);
39     while(!Q.empty()){
40         int u=Q.front();Q.pop();
41         for(int i=Head[u];i!=-1;i=Next[i]){
42             if((W[i]>0)&&(Depth[V[i]]==0)){
43                 Depth[V[i]]=Depth[u]+1;
44                 Q.push(V[i]);
45             }
46         }
47     }
48     if(Depth[t]==0) return 0;
49     return 1;
50 }
51
52 int dfs(int u,int dist){
53     if(u==t) return dist;
54     for(int& i=cur[u];i!=-1;i=Next[i]){
55         if((Depth[V[i]]==Depth[u]+1)&&W[i]!=0){
56             int di=dfs(V[i],min(dist,W[i]));
57             if(di>0){
58                 W[i]-=di;
59                 W[i^1]+=di;
60                 return di;
61             }
62         }
63     }
64     return 0;
65 }
66
67 int Dinic(){
68     int Ans=0;
69     while(bfs()){
70         for(int i=0;i<=n+1;i++) cur[i]=Head[i];
71         while(int d=dfs(s,INF)) Ans+=d;
72     }
73     return Ans;
74 }
75
76 int main(){
77     int np,nc;
78     while(cin>>n>>np>>nc>>m){
79         int a,b,c;
80         char zf;
81         init();
82         s=n;t=n+1;
83         for(int i=1;i<=m;i++){
84             cin>>zf>>a>>zf>>b>>zf>>c;
85             add_edge(a,b,c);
86         }
87         for(int i=1;i<=np;i++){
88             cin>>zf>>b>>zf>>c;
89             add_edge(s,b,c);
90         }
91         for(int i=1;i<=nc;i++){
92             cin>>zf>>a>>zf>>c;
93             add_edge(a,t,c);
94         }
95         cout<<Dinic()<<endl;
96     }
97     return 0;
98 }

时间: 2024-08-29 05:35:42

POJ 1459 Power Network(多源点/汇点最大流问题)的相关文章

POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

poj 1459 Power Network, 最大流,多源多汇

点击打开链接 多源多汇最大流,虚拟一个源点s'和一个汇点t',原来的源点.汇点向它们连边. #include<cstdiO> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> using namespace std; const int maxn = 500 + 5; const int INF = 100

poj 1459 Power Network (dinic)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23059   Accepted: 12072 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

POJ 1459 Power Network 最大流

建模不难,就读入有点麻烦,无脑拍完dinic 1A happy- #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #i

POJ 1459 Power Network 经典网络流构图问题 最大流,EK算法

题目链接:POJ 1459 Power Network Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23347   Accepted: 12231 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport line

POJ 1459 Power Network(ISAP 裸最大流)

题目链接:http://poj.org/problem?id=1459 注意输入格式就行,还是ISAP #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; const int maxn = 300; const int ma

初涉网络流 POJ 1459 Power Network

怒搞一下午网络流,又去我一块心病. 从2F到SAP再到Dinic终于过掉了.可是书上说Dinic的时间复杂度为v*v*e.感觉也应该超时的啊,可是过掉了,好诡异. 后两种算法都是在第一种的基础上进行优化.第一种方法就是不停的寻找增广路,后两种引进了层次网络的概念,第三种又改善了寻找增广路的方法. 现在只能理解到这里了... #include <algorithm> #include <iostream> #include <cstring> #include <c

POJ 1459 Power Network (网络流最大流基础 多源点多汇点 Edmonds_Karp算法)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24056   Accepted: 12564 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

poj 1459 Power Network【建立超级源点,超级汇点】

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 may be supplied