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. Orcale自增/Hibernate 配置

    -- 自增  create sequence SEQ_T_APP_USER start with 1 increment by 1; -- 触发器 create trigger DECTUSER_T_ ...

  2. Eclipse快速补全快捷键Ctrl+1修改为Android Studio的Alt+Enter

    步骤: Window ->Preferences->key-> type filter text 下输入quick fix(这个是快速补全的快捷键)改为Alt+Enter 下面的wh ...

  3. 魅族 -- 禁止D及以下级别LOG的输出

    真机调试时:Log.d()打印不出信息,模拟器可以. 使用的手机:魅族. 状况:禁止D及以下级别LOG的输出. PS.Log.d("h_bl", "进入执行") ...

  4. Java下String逗号数组和List<String>的互相转换

    说明:很遗憾,组装的时候只能遍历. 方法: public static String listToString(List<String> list){ if(list==null){ re ...

  5. telnet协议的作用详解,以及telnet端口号介绍

    转:http://www.ctowhy.com/382.html Telnet协议,工作在TCP/IP协议栈的“应用层”,telnet是一种使用命令行的远程终端管理的协议,可以远程连接到网络设备上,并 ...

  6. C语言 printf格式化输出,参数详解

      有关输出对齐 int main(int argc, char* argv[]){ char insertTime[20] = {"1234567890"}; double in ...

  7. 【java】乱码处理+编码转化+判断字符串编码方式

    之前有一篇是修改IDE的编码,服务器的编码等处理乱码,但是在所有环境因素上,保证了编码方式之后,也会有前台传递给后台[get方式提交]传递给后台的编码方式是非UTF-8的,也会有例如FTP服务器的编码 ...

  8. php漏洞挖掘书籍

    PHP是一种被广泛使用的脚本语言,尤其适合web开发.具有跨平台,容易学习,功能强大等特点,据统计全世界超过34%的网站有php的应用,包括Yahoo.sina.163.sohu等大型门户网站.而且很 ...

  9. JavaScript里面向对象的继承:构造函数"继承"的六种方法

    //现在有一个"动物"对象的构造函数. function Animal(){ this.species = "动物"; } //还有一个"猫" ...

  10. Spark Streaming揭秘 Day2-五大核心特征

    Spark Streaming揭秘 Day2 五大核心特征 引子 书接上回,Streaming更像Spark上的一个应用程序,会有多个Job的配合,是最复杂的Spark应用程序.让我们先从特征角度进行 ...