Power Network
Time Limit: 2000MS   Memory Limit: 32768K
Total Submissions: 27229   Accepted: 14151

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.
 
    /*
EdmondsKarp
Memory 320K
Time 1282MS
和没过没什么区别
*/
#include <iostream>
#include <queue>
using namespace std;
#define min(a,b) (a<b?a:b)
#define MAXV 105
#define MAXINT INT_MAX typedef struct{
int flow; //流量
int capacity; //最大容量值
}maps; maps map[MAXV][MAXV]; int vertime; //顶点总数
int nedges; //边的总数
int power_stations; //发电站总数
int consumers; //消费者总数
int maxflow; //最大流
int sp,fp; //标记源点与汇点 int parent[MAXV]; //用于bfs寻找路径 int bfs(int start,int end){
int a[MAXV],i,v;
queue <int>q; memset(a,,sizeof(a));
memset(parent,-,sizeof(parent)); q.push(start);
a[start]=MAXINT;
while(!q.empty()){
v=q.front();q.pop();
for(i=;i<=vertime;i++){
if(!a[i] && map[v][i].capacity>map[v][i].flow){
q.push(i);
parent[i]=v;
a[i]=min(a[v],map[v][i].capacity-map[v][i].flow);
}
}
if(v==end) break;
}
return a[end];
} void EdmondsKarp(){
int i,tmp;
maxflow=;
while(tmp=bfs(sp,fp)){
for(i=fp;i!=sp;i=parent[i]){
map[i][parent[i]].flow-=tmp; //更新反向流
map[parent[i]][i].flow+=tmp; //更新正向流
}
maxflow+=tmp;
}
} int main(){
int i;
int x,y,z;
char ch;
while(scanf("%d%d%d%d", &vertime, &power_stations,&consumers,&nedges)!= EOF){
//Init
memset(map,,sizeof(map)); //Read Gragh
for(i=;i<=nedges;i++){ //设置读图从1开始
cin>>ch>>x>>ch>>y>>ch>>z;
map[x+][y+].capacity=z;
} //Build Gragh
//建立超级源点指向所有的发电站
sp=vertime+;fp=vertime+;vertime+=;
for (i=; i<=power_stations; i++){
cin>>ch>>x>>ch>>y;
map[sp][x+].capacity=y;
} //建立超级汇点,使所有消费者指向它
for (i=; i<=consumers; i++){
cin>>ch>>x>>ch>>y;
map[x+][fp].capacity=y;
} EdmondsKarp();
printf("%d\n",maxflow);
}
return ;
}

EdmondsKarp

    /*
dinic
Memory 320K
Time 563MS
*/
#include <iostream>
#include <queue>
using namespace std;
#define min(a,b) (a<b?a:b)
#define MAXV 105
#define MAXINT INT_MAX typedef struct{
int flow; //流量
int capacity; //最大容量值
}maps; maps map[MAXV][MAXV];
int dis[MAXV]; //用于dinic分层 int vertime; //顶点总数
int nedges; //边的总数
int power_stations; //发电站总数
int consumers; //消费者总数
int maxflow; //最大流
int sp,fp; //标记源点与汇点 bool bfs(){
int v,i;
queue <int>q;
memset(dis,,sizeof(dis)); q.push(sp);
dis[sp]=;
while(!q.empty()){
v=q.front();q.pop();
for(i=;i<=vertime;i++)
if(!dis[i] && map[v][i].capacity>map[v][i].flow){
q.push(i);
dis[i]=dis[v]+;
}
if(v==fp) return ;
}
return ;
} int dfs(int cur,int cp){
if(cur==fp) return cp; int tmp=cp,t;
for(int i=;i<=vertime;i++)
if(dis[i]==dis[cur]+ && tmp && map[cur][i].capacity>map[cur][i].flow){
t=dfs(i,min(map[cur][i].capacity-map[cur][i].flow,tmp));
map[cur][i].flow+=t;
map[i][cur].flow-=t;
tmp-=t;
}
return cp-tmp;
} void dinic(){
maxflow=;
while(bfs()) maxflow+=dfs(sp,MAXINT);
} int main(){
int i;
int x,y,z;
char ch;
while(scanf("%d%d%d%d", &vertime, &power_stations,&consumers,&nedges)!= EOF){
//Init
memset(map,,sizeof(map)); //Read Gragh
for(i=;i<=nedges;i++){ //设置读图从1开始
cin>>ch>>x>>ch>>y>>ch>>z;
map[x+][y+].capacity=z;
} //Build Gragh
//建立超级源点指向所有的发电站
sp=vertime+;fp=vertime+;vertime+=;
for (i=; i<=power_stations; i++){
cin>>ch>>x>>ch>>y;
map[sp][x+].capacity=y;
} //建立超级汇点,使所有消费者指向它
for (i=; i<=consumers; i++){
cin>>ch>>x>>ch>>y;
map[x+][fp].capacity=y;
} dinic();
printf("%d\n",maxflow);
}
return ;
}

dinic

    /*
sap
Memory 328K
Time 454MS
*/
#include <iostream>
#include <queue>
using namespace std;
#define MAXV 110
#define INF 1<<29
#define min(a,b) (a>b?b:a) int n,c[MAXV][MAXV],r[MAXV][MAXV],source,sink;
int dis[MAXV],maxflow; void bfs(){
int v,i;
queue <int>q;
memset(dis,,sizeof(dis));
q.push(sink);
while(!q.empty()){
v=q.front();q.pop();
for(i=;i<=sink;i++){
if(!dis[i] && c[i][v]>){
dis[i] = dis[v] +;
q.push(i);
}
}
}
} void sap(){
int top=source,pre[MAXV],i,j,low[MAXV]; bfs(); //分层
memset(low,,sizeof(low)); //保存路径的最小流量
while(dis[source]<n){
low[source]=INF;
for(i=;i<=sink;i++){ //找到一条允许弧
if(r[top][i]> && dis[top]==dis[i] +) break;
}
if(i<=sink){ //找到了
low[i]=min(r[top][i],low[top]); //更新最小流量
pre[i]=top;top=i; //记录增广路径
if(top==sink){ //找到一条增广路径更新残量
maxflow += low[sink];
j = top;
while(j != source){
i=pre[j];
r[i][j]-=low[sink];
r[j][i]+=low[sink];
j=i;
}
top=source; //再从头找一条增广路径
memset(low,,sizeof(low));
}
}
else{ //找不到这样一条允许弧更新距离数组
int mindis=INF;
for(j=;j <=sink;j++){
if(r[top][j]> && mindis>dis[j] +)
mindis=dis[j] +;
}
dis[top]=mindis;
if(top!=source) top=pre[top];
}
}
} int main(){
int i,nedges,power_stations,consumers;
int x,y,z;
char ch;
while(scanf("%d%d%d%d", &n, &power_stations,&consumers,&nedges)!= EOF){
//Init
memset(r,,sizeof(r));
memset(c,,sizeof(c));
source=;sink=n+;n+=;maxflow=; //Read Gragh
for(i=;i<=nedges;i++){ //设置读图从1开始
cin>>ch>>x>>ch>>y>>ch>>z;
c[x+][y+]=r[x+][y+]=z;
} //Build Gragh
//建立超级源点指向所有的发电站
for (i=;i<=power_stations;i++){
cin>>ch>>x>>ch>>y;
c[source][x+]=r[source][x+]=y;
} //建立超级汇点,使所有消费者指向它
for (i=;i<=consumers;i++){
cin>>ch>>x>>ch>>y;
c[x+][sink]=r[x+][sink]=y;
}
sap();
printf("%d\n",maxflow);
}
return ;
}

sap

sap+分层+gap优化
Memory 328K
Time 438MS
*/
#include <iostream>
#include <queue>
using namespace std;
#define MAXV 110
#define INF 1<<29
#define min(a,b) (a>b?b:a) int n,c[MAXV][MAXV],r[MAXV][MAXV],source,sink;
int dis[MAXV],maxflow,gap[MAXV]; void bfs(){
int v,i;
queue <int>q;
memset(dis,,sizeof(dis));
memset(gap,,sizeof(gap));
gap[]++;
q.push(sink);
while(!q.empty()){
v=q.front();q.pop();
for(i=;i<=sink;i++){
if(!dis[i] && c[i][v]>){
dis[i] = dis[v] +;
gap[dis[i]]++;
q.push(i);
}
}
}
} void sap(){
int top=source,pre[MAXV],i,j,low[MAXV]; bfs(); //分层
memset(low,,sizeof(low)); //保存路径的最小流量
while(dis[source]<n){
low[source]=INF;
for(i=;i<=sink;i++){ //找到一条允许弧
if(r[top][i]> && dis[top]==dis[i]+ && dis[i]>=) break;
}
if(i<=sink){ //找到了
low[i]=min(r[top][i],low[top]); //更新最小流量
pre[i]=top;top=i; //记录增广路径
if(top==sink){ //找到一条增广路径更新残量
maxflow += low[sink];
j = top;
while(j != source){
i=pre[j];
r[i][j]-=low[sink];
r[j][i]+=low[sink];
j=i;
}
top=source; //再从头找一条增广路径
memset(low,,sizeof(low));
}
}
else{ //找不到这样一条允许弧更新距离数组
int mindis=INF;
for(j=;j <=sink;j++){
if(r[top][j]> && mindis>dis[j] + && dis[j]>=)
mindis=dis[j] +;
}
gap[dis[top]]--;
if (gap[dis[top]] ==) break;
gap[mindis]++;
dis[top]=mindis;
if(top!=source) top=pre[top];
}
}
} int main(){
int i,nedges,power_stations,consumers;
int x,y,z;
char ch;
while(scanf("%d%d%d%d", &n, &power_stations,&consumers,&nedges)!= EOF){
//Init
memset(r,,sizeof(r));
memset(c,,sizeof(c));
source=;sink=n+;n+=;maxflow=; //Read Gragh
for(i=;i<=nedges;i++){ //设置读图从1开始
cin>>ch>>x>>ch>>y>>ch>>z;
c[x+][y+]=r[x+][y+]=z;
} //Build Gragh
//建立超级源点指向所有的发电站
for (i=;i<=power_stations;i++){
cin>>ch>>x>>ch>>y;
c[source][x+]=r[source][x+]=y;
} //建立超级汇点,使所有消费者指向它
for (i=;i<=consumers;i++){
cin>>ch>>x>>ch>>y;
c[x+][sink]=r[x+][sink]=y;
}
sap();
printf("%d\n",maxflow);
}
return ;
}

sap+分层+gap优化

POJ1459 Power Network(网络最大流)的更多相关文章

  1. POJ 1459 Power Network(网络最大流,dinic算法模板题)

    题意:给出n,np,nc,m,n为节点数,np为发电站数,nc为用电厂数,m为边的个数.      接下来给出m个数据(u,v)z,表示w(u,v)允许传输的最大电力为z:np个数据(u)z,表示发电 ...

  2. POJ1459 Power Network 网络流 最大流

    原文链接http://www.cnblogs.com/zhouzhendong/p/8326021.html 题目传送门 - POJ1459 题意概括 多组数据. 对于每一组数据,首先一个数n,表示有 ...

  3. POJ-1459 Power Network(最大流)

    https://vjudge.net/problem/POJ-1459 题解转载自:優YoU http://user.qzone.qq.com/289065406/blog/1299339754 解题 ...

  4. POJ1459 Power Network —— 最大流

    题目链接:https://vjudge.net/problem/POJ-1459 Power Network Time Limit: 2000MS   Memory Limit: 32768K Tot ...

  5. poj1459 Power Network (多源多汇最大流)

    Description A power network consists of nodes (power stations, consumers and dispatchers) connected ...

  6. Power Network (最大流增广路算法模板题)

    Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 20754   Accepted: 10872 Description A p ...

  7. POJ1459 - Power Network

    原题链接 题意简述 原题看了好几遍才看懂- 给出一个个点,条边的有向图.个点中有个源点,个汇点,每个源点和汇点都有流出上限和流入上限.求最大流. 题解 建一个真 · 源点和一个真 · 汇点.真 · 源 ...

  8. POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 D ...

  9. poj1087 A Plug for UNIX & poj1459 Power Network (最大流)

    读题比做题难系列…… poj1087 输入n,代表插座个数,接下来分别输入n个插座,字母表示.把插座看做最大流源点,连接到一个点做最大源点,流量为1. 输入m,代表电器个数,接下来分别输入m个电器,字 ...

随机推荐

  1. tornado介绍

    一.定义 tornado是一个异步非阻塞模型的服务器(tcp/http).web框架. 二.特性 1.高并发 原因:其一,网络事件循环部分根据操作系统选择最高效的,如Linux会是epoll: 其二, ...

  2. AngularJS结构简介

    AngularJS是MVC架构,M是C里面的属性-值,C是js的class,V是DOM 各个关键特性的结构如下图所示: http://my.oschina.net/tommyfok/blog/2970 ...

  3. Apache Jmeter(2)

    上一节中,我们了解了jmeter的一此主要元件,那么这些元件如何使用到性能测试中呢.这一节创建一个简单的测试计划来使用这些元件.该计划对应的测试需求. 1)测试目标网站是fnng.cnblogs.co ...

  4. jQuery 1.7_20120209 学习笔记

    html([val|fn]) parameters: function(index,html) 此函数返回一个html字符串,接受两个参数,index为元素在集合中的索引位置,html为原先的html ...

  5. 编译mahout0.9

    一.编译的必要性 由于hadoop的生态系统中的各个组建之间的兼容性的问题,因此导致将各个组件组合在一起的时候,会存在一个兼容性的问题,这也是apache hadoop生态系统给开发者带来的一个问题, ...

  6. php注册审核显示

    用户进行注册,管理员通过审核后,使用户通过审核 数据库建表 create database mydb; use mydb; create table User ( Uid int auto_incre ...

  7. Canopy使用教程 (2)

    1.下载https://reputation.alienvault.com/reputation.data alienvault公司的IP信誉数据库文件到本地,手动或者wget 2.使用 read_c ...

  8. Ubuntu 14.10 下运行进程实时监控pidstat命令详解

    简介 pidstat主要用于监控全部或指定进程占用系统资源的情况,如CPU,内存.设备IO.任务切换.线程等.pidstat首次运行时显示自系统启动开始的各项统计信息,之后运行pidstat将显示自上 ...

  9. 数据结构《14》----并查集 Union-Find

    描述: 并查集是一种描述解决等价关系.能够方便地描述不相交的多个集合. 支持如下操作    1. 建立包含元素 x 的集合  MakeSet(x) 2. 查找给定元素所在的集合 Find(x), 返回 ...

  10. 在国内时,更新ADT时需要配置的

    RT