POJ 1459 Power Network(最大流+EK算法邻接矩阵写法)
来源:互联网 发布:爱奇艺mac版 编辑:程序博客网 时间:2024/06/15 15:20
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) <= p max(u) of power, may consume an amount 0 <= c(u) <= min(s(u),c max(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) <= l max(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 c max(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and l max(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.
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 c max(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and l max(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.
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 l max(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of p max(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 c max(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.
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.
2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)207 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
156
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,汇点为n+1,先给你n个点(0到n-1),然后给一个与源点相连的边数num1,如果给一个通往汇点的边数num2,给一个普通的边数m
输入是m个(x,y)c,表示邻接矩阵r[x][y]=c;然后后面的num1个(x)y表示源点通往x的边的值为y,同理后面的num2个(x)y表示x通往汇点的值为y
。。。终于说完了
思路:
一开始照着紫书上打的邻接表的写法疯狂TLE,加上输入输出外挂也是疯狂TLE,后来没办法上网随便搜了个邻接矩阵的EK算法的模板,加上输入输出外挂只用了600ms就过了。。我想是因为这是一个稠密图,适合用邻接矩阵的原因吧qaq
代码:
#include<iostream>#include<cstring>#include<stdio.h>#include<math.h>#include<string>#include<stdio.h>#include<queue>#include<stack>#include<map>#include<vector>#include<deque>#include<algorithm>using namespace std;#define INF 100861111#define ll long long#define eps 1e-5#define maxn 105int r[maxn][maxn]; //残留网络,初始化为原图bool visit[maxn]; int pre[maxn]; int m,n;bool bfs(int s,int t) //寻找一条从s到t的增广路,若找到返回true { int p; queue<int>q; memset(pre,-1,sizeof(pre)); memset(visit,false,sizeof(visit)); pre[s]=s; visit[s]=true; q.push(s); while(!q.empty()) { p=q.front(); q.pop(); for(int i=0;i<=n;i++) { if(r[p][i]>0&&!visit[i]) { pre[i]=p; visit[i]=true; if(i==t) return true; q.push(i); } } } return false; }int EdmondsKarp(int s,int t) { int flow=0,d,i; while(bfs(s,t)) { d=INF; for(i=t;i!=s;i=pre[i]) d=d<r[pre[i]][i]? d:r[pre[i]][i]; for(i=t;i!=s;i=pre[i]) { r[pre[i]][i]-=d; r[i][pre[i]]+=d; } flow+=d; } return flow; }void scan_d(int &ret){ char c; ret=0; while((c=getchar())<'0'||c>'9'); while(c>='0'&&c<='9') { ret=ret*10+(c-'0'),c=getchar(); }}int main(){ int i,j,x,y,c,num1,num2,s,t,f; while(scanf("%d%d%d%d",&n,&num1,&num2,&m)!=EOF) { memset(r,0,sizeof(r)); for(i=0;i<m;i++) { scan_d(x); scan_d(y); scan_d(c); r[x][y]=c; } s=n; t=n+1; n+=2; for(i=0;i<num1;i++) { scan_d(x); scan_d(y); r[s][x]=y; } for(i=0;i<num2;i++) { scan_d(x); scan_d(y); r[x][t]=y; } printf("%d\n",EdmondsKarp(s,t)); } return 0;}
阅读全文
0 0
- POJ 1459 Power Network(最大流+EK算法邻接矩阵写法)
- POJ 1459 Power Network【最大流基础题 EK 算法】
- POJ 1459 Power Network 多源点多汇点+最大流(EK算法)
- poj 1459 Power Network 【图论-网络流-最大流-EK】
- Poj 1459 Power Network (多源多汇最大流EK)
- Power Network--最大流的EK()算法
- poj 1459 Power Network (ek算法,网络流)
- poj 1459 Power Network 网络流 ek算法
- poj1459--Power Network(最大流EK、)
- Power Network poj 1459 增广流EK
- POJ 1459 Power Network(EK)
- POJ-1459-Power Network(网络流 EK)
- POJ 1459 :Power Network:最大流算法
- POJ 1459-Power Network(最大流-Edmond-Karp算法)
- POJ 1459 Power Network(最大流+dinic算法模板)
- Power Network(poj1459 网络最大流 EK法)
- poj1459 Power Network --- 最大流 EK/dinic
- poj 1459 Power Network(最大流)
- 八大排序算法
- ORACLE 数据文件丢失处理
- Java必懂之Java内存分配
- PHP表单常用正则表达式(URL、HTTP、手机、邮箱等)
- C++ 常用类型间的转换
- POJ 1459 Power Network(最大流+EK算法邻接矩阵写法)
- cp -fr Camera /mnt/hgfs/share/cp: cannot create symbolic link '/mnt/hgfs/share/Camera/lib/arm64/libr
- phpStudy 2014的Apache虚拟主机配置
- 提交审核版本小程序-微信小程序开发-视频教程16
- 系统吞吐量(TPS)、用户并发量、性能测试概念和公式
- IOS多线程同步问题
- Android学习笔记四之Activity
- 《深入理解java虚拟机》学习-第八章-虚拟机字节码执行引擎-方法调用
- 基于配置的Spring声明式事务管理详解