Time Limit: 2000MS   Memory Limit: 32768K
Total Submissions: 24788   Accepted: 12922

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.
 
中文说一下

多组数据,每组先是有四个数据 N,np,nc,M 其中N表示节点数(包括用电用户,中继站,发电站的总数)np表示发电站的个数,nc表示,用电户的个数,M表示电线的个数

然后再输出M组数据表示电线,每根电线用(u,v)z表示表示从u到v最多可以传送的电量为z

再输出np个发电站,每个发电站形式是(u)z表示发电站u可以发电最多z

最后输出nc个用电户,每个用电户的形式是(u)z,表示用电户u最多会用z的电

这题用最大流方法可以求解。这题使用求Ford-Fulkerson方法求最大流,使用BFS的算法称为Edmonds-Karp算法。

 #include <iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<queue>
#define INF 100000
using namespace std;
int map[][],path[],flow[],start,end;
queue<int> q;
int BFS(){
while(!q.empty())q.pop();
memset(path,-,sizeof(path));
q.push(start);
path[start]=;
flow[start]=INF;
while(!q.empty()){
int v=q.front();
q.pop();
if(v==end)
break;
for(int i=;i<=end;i++){
if(i!=start&&map[v][i]&&path[i]==-){
flow[i]=flow[v]<map[v][i]?flow[v]:map[v][i];
q.push(i);
path[i]=v;
}
}
}
if(path[end]==-) return -;
else
return flow[end];
}
int Edmonds_Karp(){
int max_flow,step,now,pre;
max_flow=;
while(){
step=BFS();
if(step==-)
break;
max_flow+=step;
now=end;
while(now!=start){
pre=path[now];
map[pre][now]-=step;
map[now][pre]+=step;
now=pre;
}
}
return max_flow;
}
int main() {
int n,np,nc,m; while(~scanf("%d %d %d %d",&n,&np,&nc,&m)){
memset(map,,sizeof(map));
getchar();
for(int i=;i<m;i++){
int u,v,f;
scanf(" (%d,%d)%d ",&u,&v,&f);
map[u+][v+]=f; }
for(int i=;i<np;i++){
int s,f;
scanf(" (%d)%d ",&s,&f);
map[][s+]=f; }
for(int i=;i<nc;i++){
int s,f;
scanf(" (%d)%d ",&s,&f);
map[s+][n+]=f;
}
start=;
end=n+;
int result=Edmonds_Karp();
printf("%d\n",result);
} return ;
}

Power Network - poj 1459 (最大流 Edmonds-Karp算法)的更多相关文章

  1. Power Network (poj 1459 网络流)

    Language: Default Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23407   ...

  2. F - Power Network - poj 1459(简单最大流)

    题目大意:题目说了一大堆,其实都是废话......让人有些不知所云,其实就是给了一些电厂,和一些消费点,然后里面有一些路线什么的,求出消费点可以最多消费的电量是多少. 输入大意: 分析:懂了题意就是一 ...

  3. Power Network POJ - 1459 [网络流模板]

    http://poj.org/problem?id=1459 嗯,网络流模板...多源点多汇点的图,超级汇点连发电厂,用户连接超级汇点 Status Accepted Time 391ms Memor ...

  4. Power Network POJ - 1459 网络流 DInic 模板

    #include<cstring> #include<cstdio> #define FOR(i,f_start,f_end) for(int i=f_startl;i< ...

  5. F - Power Network POJ - 1459

    题目链接:https://vjudge.net/contest/299467#problem/F 这个是一个很简单的题目,但是读入很有意思,通过这个题目,我学会了一种新的读入方式. 一个旧的是(%d, ...

  6. 最大流算法之Ford-Fulkerson算法与Edmonds–Karp算法

    引子 曾经很多次看过最大流的模板,基础概念什么的也看了很多遍.也曾经用过强者同学的板子,然而却一直不会网络流.虽然曾经尝试过写,然而即使最简单的一种算法也没有写成功过,然后对着强者大神的代码一点一点的 ...

  7. Power Network(网络流最大流 & dinic算法 + 优化)

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24019   Accepted: 12540 D ...

  8. POJ1459 Power Network(网络最大流)

                                         Power Network Time Limit: 2000MS   Memory Limit: 32768K Total S ...

  9. POJ 1459 - Power Network 【Ek-最大流】

    <题目链接> 题目大意:给出 n 个点,其中包括 np个发电站,nc 个消费者, 剩下的全部都是中转点,再给出 这些点中的m 条边,代表这两点间的最大传输电量,并且给出发电站的最大发送电量 ...

随机推荐

  1. 获取XIB子视图的两个方法

    创建了一个XIB文件 CommentCell.xib,并设置好UIImageView的tag为100,昵称UILabel的tag为101,时间的UILabel的tag为102,并制定cell为Comm ...

  2. Android2017最新面试题(3-5年经验个人面试经历)

    2017最新Android面试题 大家好,在跟大家讲述自己的面试经历,以及遇到的面试题前,先说说几句题外话. 接触Android已经3年,在工作中遇到疑难问题总是在网上(csdn大牛博客,stacko ...

  3. JavaScript数组api简单说明

    1.一个数组加上另一个(一些)数组,不会修改原数组只会返回新数组 arrayObject.concat(arrayX,arrayX,......,arrayX) 2.把数组按照指定字符串分离,不会修改 ...

  4. Easyui的numberbox无法输入以0开头的数字编号(转载)

    1.问题 项目中碰到这样一个问题,Easyui的numberbox在输入数字编号的时候不能以0开头 在我输入以0开头的数字编号后,离开输入框的时候,那个前缀0就自动去掉了. 接下来,我们查看API说明 ...

  5. DICOM中几个判断图像方向的tag

    在DICOM标准里,有三个TAG与成像的方向相关. 参考来源:Kitware关于DICOM方向的说明 http://public.kitware.com/IGSTKWIKI/index.php/DIC ...

  6. OpenStack制作Windows 2008 KVM镜像

    1.下载驱动程序 [root@linux-node1 ~]# cd /usr/local/src [root@linux-node1 src]# wget https://launchpad.net/ ...

  7. http://zhidao.baidu.com/link?url=3tJ_i5gyYLrd7rFPk0eRYre_oxjCZvTOMOutp89LGhUgi6Ic6Ncama_GMAHnwfF73SVYGqy364vDfv6AY4ERPa

    http://zhidao.baidu.com/link?url=3tJ_i5gyYLrd7rFPk0eRYre_oxjCZvTOMOutp89LGhUgi6Ic6Ncama_GMAHnwfF73SV ...

  8. 文档对象模型-DOM(二)

    从NodeList中选择元素 方法一:item()方法,用于返回其中的单一节点,需要在方法的参数中指定所需元素的索引编号. 当其中没有任何元素时,执行代码是对资源的浪费.因此程序员会在执行代码之前,先 ...

  9. yoman搭建你的react-webpack应用

    还没有npm和node的要提前做好准备工作 做好这一切之后 我们安装yo,记住安装在全局变量中,我们需要用他的命令工具 npm install -g yo 接下来安装yo提供的react-webpac ...

  10. ajax乱码解决总结

    第一,javascript沿用java的字符处理方式,内部是使用unicode来处理所有字符的,第二,utf-8是每个汉字(unicode字符)用3个字节来存储.第三,用utf-8来send数据是不会 ...