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. SQLServer组成:

    SQL Server DB Engine (Relational Engine),SQL语言用于向Engine描述问题. Algebrizer:代数器,检查语法,并将查询转换成内部表达式 Query ...

  2. “自适应”高度的 textarea 文本输入框

    写在前面 那啥,在我的那个很安静的一个 CSS 群(群号:82991297)突然看到有人在问一个问题. 使用 css 如何实现:textarea 如何实现高度自适应? 当时看到这个问题的时候,我脑中只 ...

  3. 【spring data jpa】spring data jpa 中的update 更新字段,如果原字段值为null不处理,不为null则在原来的值上加一段字符串

    示例代码: /** * 如果barCode字段值为null则不处理 * 如果barCode字段值不为null则在原本值的前面拼接 del: * @param dealer * @return */ @ ...

  4. Ado.Net基础拾遗二:插入,更新,删除数据

    插入数据 public void InsertDataToSQL() { string conStr = ConfigurationManager.ConnectionStrings["No ...

  5. js数据加载完成的方法

    1.document.onreadystatechange(页面加载完成的方法)配合document.readyState(获取加载状态)使用 document.onreadystatechange= ...

  6. PostgreSQL on Linux 最佳部署手册

    安装常用包 # yum -y install coreutils glib2 lrzsz mpstat dstat sysstat e4fsprogs xfsprogs ntp readline-de ...

  7. iOS:二维码的生成

    所谓的二维码就是一个图片,只不过在iOS需要借用<CoreImage/CoreImage.h>来实现,  并且二维码图片是通过CIImage来转成UIImage的.具体步骤如下: // 1 ...

  8. WebGL可视化地球和地图引擎:Cesium.js

    http://www.open-open.com/lib/view/open1427341416418.html   Cesium 是一个JavaScript 库用于在Web浏览器创建 3D 地球和 ...

  9. 开源 免费 java CMS - FreeCMS1.9 移动APP管理 栏目配置

    项目地址:http://www.freeteam.cn/ 栏目配置 管理员能够在这里设设置栏目是否是否同意移动app訪问.栏目页的布局等属性. 从左側管理菜单点击栏目配置进入. 选择须要管理的栏目后点 ...

  10. LeetCode 格雷码序列的生成

    问题概述:在一组数的编码中,若随意两个相邻的代码仅仅有一位二进制数不同.则称这样的编码为格雷码. 2位数的格雷码序列:00 : 001 : 111 : 310 : 2找规律:假设要求n位的格雷码,先要 ...