多源点、多汇点最大流问题
2015-08-22 14:45
381 查看
[poj 1459] (http://poj.org/problem?id=1459)
题目描述:
Power Network(给力电网)
Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 25274 Accepted: 13175
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.
Source
Southeastern Europe 2003
言简意赅:
有发电站,有消耗站,有转换站,关于转化的数据是根据三元组给出的,关于源点和汇点是根据二元组给出的,需要计算的是从源点到汇点的最大可以得到的电力。
解题思路:解决此题的关键是,把题目描述的模型给抽象出来,此题为多源点多汇点的题目,增加一个超级源点和一个超级汇点,再用最大流即可,此题选择用队列来实现。
代码实现:
题目描述:
Power Network(给力电网)
Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 25274 Accepted: 13175
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.
Source
Southeastern Europe 2003
言简意赅:
有发电站,有消耗站,有转换站,关于转化的数据是根据三元组给出的,关于源点和汇点是根据二元组给出的,需要计算的是从源点到汇点的最大可以得到的电力。
解题思路:解决此题的关键是,把题目描述的模型给抽象出来,此题为多源点多汇点的题目,增加一个超级源点和一个超级汇点,再用最大流即可,此题选择用队列来实现。
代码实现:
#include <iostream> #include <stdio.h> #include <queue> #include <string.h> using namespace std; const int MAX=130; const int inf=0x3f3f3f3f; int m[MAX][MAX],flow[MAX],visit[MAX],front[MAX]; int N,NC,NP,M; int S,T; int bfs() { int i,u; queue<int> Q; while(!Q.empty()) Q.pop(); memset(flow,0,sizeof(flow)); memset(visit,0,sizeof(visit)); flow[S]=inf; visit[S]=1; Q.push(S); while(!Q.empty()) { u=Q.front(); Q.pop(); for(i=0; i<=N+1; i++) { if(!visit[i] && m[u][i]>0) { visit[i]=1; front[i]=u; Q.push(i); flow[i]=m[u][i]<flow[u]? m[u][i]:flow[u]; } } } return flow[T]; } int Edmonds() { int max_flow=0; int f,u; while((f=bfs())!=0) { for(u=T; u!=S; u=front[u]) { m[front[u]][u]-=f; m[u][front[u]]+=f; } max_flow+=f; } return max_flow; } int main() { int tmp_pc=0,max_flow; int u,v,z,c; while(scanf("%d%d%d%d",&N,&NP,&NC,&M)!=EOF) { S=N; T=N+1; memset(m,0,sizeof(m)); while(M--) { while((c=getchar())!='(') NULL; scanf("%d",&u); getchar(); scanf("%d",&v); getchar(); scanf("%d",&z); m[u][v]=z;//用二维数组来建图 } for(tmp_pc=0; tmp_pc<NP+NC; tmp_pc++) { while((c=getchar())!='(') NULL; scanf("%d",&u); getchar(); scanf("%d",&z); if(tmp_pc<NP)//由超级源点到输入的小源点 m[S][u]=z; else m[u][T]=z;//由输入的小汇点到超级汇点 } max_flow=Edmonds(); printf("%d\n",max_flow); } return 0; }
相关文章推荐
- 一篇短文看懂天津港事件(转)
- IOS开发之对象生命周期
- 归并排序(C/C++)
- CodeForces 453A 概率题
- python中的装饰符@
- Gtk与Qt编译环境安装与配置
- 报错:1130-host ... is not allowed to connect to this MySql server 开放mysql远程连接 不使用localhost
- js 实现class作为选择器
- Linux sed命令删除指定行
- Cocos2d—android 中常用的工具类
- js 中class选择器,addClass,removeClass,hasClass,toggleClass,getByClass
- Spring Autowiring @Qualifier example
- 关于将缓冲器内容写入文件
- centos常用命令
- 四 HDU 2090
- java8新增特性(一)---Lambda表达式
- hibernate设计一棵普通的树
- 黑马程序员—————Java基础--------集合
- Object-C 对象 (创建/销毁 对象)-- 笔记
- poj 2909 Goldbach's Conjecture (哥德巴赫猜想)