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. 【转载】Mini6410启动过程

    这段时间在尝试使用uBoot来替代友善的Superboot,让板子支持从SD卡启动,所以就仔细研究了一下友善提供的内核和它的启动参数,发现 友善真的蛮聪明,把电脑的启动方式借鉴到它们自己的开发板上了. ...

  2. 多IDC GSLB的部署 - ADC技术博客 - 51CTO技术博客

    多IDC GSLB的部署 - ADC技术博客 - 51CTO技术博客 A10

  3. S3C2440的存储器映射(27根地址线如何寻找1G的地址)

    转:http://blog.csdn.net/ce123_zhouwei/article/details/6882091 查S3C2440的数据手册可知S3C2440可寻址1G的地址范围,但是S3C2 ...

  4. SqlServer_删除重复行只保留一条记录

      前提:相同的数据重复往数据库写入,导致存在仅主键Id不同的重复数据,现在需要去除重复数据,仅保留重复数据中Id最大的一条   思路: 1.找出存在重复数据的记录,并取重复数据中最大的Id值 2.删 ...

  5. Protostuff序列化工具类

    源代码 package org.wit.ff.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStre ...

  6. php图片木马讲解

    这是一个非常有趣的后门,它并没有依靠正常模式去隐藏起内容(比如 base64/gzip 编码),但是它却把自己的数据隐藏在JPEG图片的EXIF头部中了.它也使用exif_read_data和preg ...

  7. 主流浏览器CSS3和HTML5兼容性

    各大主流浏览器对 CSS3 和 HTML5 的支持越来越完善,曾经让多少前端开发人员心碎的IE系也开始拥抱标准.就在前几天,W3C的 HTML5 社区领袖 Shelley 宣布,HTML5的开发工作已 ...

  8. PHP5实现foreach语言结构遍历一个类的实例

    PHP5实现foreach语言结构遍历一个类 创建一个类集成Iterator接口,并实现Iterator里面的方法即可,下面见实例代码实现 <?php class Test implements ...

  9. JAVA学习笔记 -- 多线程之共享资源

    在多线程程序执行过程中,可能会涉及到两个或者多个线程试图同一时候訪问同一个资源.为了防止这样的情况的发生,必须在线程使用共享资源时给资源"上锁",以阻挡其他线程的訪问. 而这样的机 ...

  10. C++ 字符串转化成浮点型

    第一种: char  szString[] = "3.1415926535898"; double db1; db1 = atof(szString); printf(" ...