poj1459Power Network(最大流)
来源:互联网 发布:百叶窗js 编辑:程序博客网 时间:2024/06/07 09:35
题目请戳这里
题目大意:好难读的题目。。有n个点,编号0~n-1,每个点可能是发电站,中转站或者用户。发电站i可以发pi的电,最大pmax,用户i消耗ci的电,最大cmax,中转站不发电也不消耗电。每两个站点之间有一条边,边权L,表示这条边最多能送L的电。求所有用户消耗最大的电量。
题目分析:网络流。需要增加一个源点和汇点。发电站是供电者,所以对于每个发电站,源点与其建一条边,边权为该发电站最大发电量。消费者耗电,对于每个消费者,与汇点建一条边,边权为消费者最大耗电量。然后将题目中给定的边加入到图中,跑一遍最大流即可。
详情请见代码:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 105;const int M = 310005;const int inf = 0x3f3f3f3f;int n,m,np,nc,num;int head[N];struct node{ int to,next,c,pre;}arc[M];short int que[N],cnt[N];int dis[N],rpath[N],sta[N];void build(int s,int e,int cap){ arc[num].to = e; arc[num].c = cap; arc[num].next = head[s]; head[s] = num ++; arc[num - 1].pre = num; arc[num].pre = num - 1; arc[num].to = s; arc[num].c = 0; arc[num].next = head[e]; head[e] = num ++;}void re_Bfs(){ int i,front,rear; for(i = 0;i <= n + 1;i ++) { cnt[i] = 0; dis[i] = inf; } front = rear = 0; que[rear ++] = n + 1; dis[n + 1] = 0; cnt[0] = 1; while(front != rear) { int u = que[front ++]; for(i = head[u];i != -1;i = arc[i].next) { if(arc[arc[i].pre].c == 0 || dis[arc[i].to] < inf) continue; dis[arc[i].to] = dis[u] + 1; cnt[dis[arc[i].to]] ++; que[rear ++] = arc[i].to; } }}void ISAP(){ re_Bfs(); int i,u,v; int maxflow = 0; for(i = 0;i <= n + 1;i ++) sta[i] = head[i]; u = 0; while(dis[0] < n + 2) { if(u == n + 1) { int curflow = inf; for(i = 0;i != n + 1;i = arc[sta[i]].to) curflow = min(curflow,arc[sta[i]].c); for(i = 0;i != n + 1;i = arc[sta[i]].to) { arc[sta[i]].c -= curflow; arc[arc[sta[i]].pre].c += curflow; } maxflow += curflow; u = 0; } for(i = sta[u];i != -1;i = arc[i].next) if(arc[i].c && dis[u] == dis[arc[i].to] + 1) break; if(i != -1) { sta[u] = i; rpath[arc[i].to] = arc[i].pre; u = arc[i].to; } else { if((--cnt[dis[u]]) == 0) break; sta[u] = head[u]; int Min = inf; for(i = head[u];i != -1;i = arc[i].next) if(arc[i].c) Min = min(Min,dis[arc[i].to]); dis[u] = Min + 1; cnt[dis[u]] ++; if(u != 0) u = arc[rpath[u]].to; } } printf("%d\n",maxflow);}int nextint(){ char c; int ret = 0; while((c = getchar()) > '9' || c < '0') ; ret = c - '0'; while((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + c - '0'; return ret;}int main(){ int i,u,v,cap; //freopen("data.in","r",stdin); while(scanf("%d",&n) != EOF) { memset(head,-1,sizeof(head)); num = 0; np = nextint();nc = nextint();m = nextint(); //scanf("%d%d%d",&np,&nc,&m); while(m --) { u = nextint();v = nextint();cap = nextint(); build(u + 1,v + 1,cap); } while(np --) { u = nextint();cap = nextint(); build(0,u + 1,cap); } while(nc --) { u = nextint();cap = nextint(); build(u + 1,n + 1,cap); } ISAP(); } return 0;}//1008K16MS
- poj1459Power Network(最大流)
- POJ1459Power Network(最大流)
- poj1459Power Network
- POJ1459Power Network(电网)——最大流水题
- POJ1459Power Network(电网)
- POJ1459Power Network(电网)
- poj1459Power Network_最大流的增广路算法_多源点多汇点问题(Edmord_Karp)
- PKU1459 Power Network - 最大流
- POJ1459--Power Network(最大流)
- poj1459 最大流 Power Network
- poj1459 Power Network (最大流)
- Power Network poj1459 最大流
- POJ1459[Power Network]--最大流
- pku 1459 Power Network(最大流)
- (最大流) poj 1459 Power Network
- POJ--1459[Power Network] 网络最大流
- POJ-1459 Power Network【最大流】
- POJ 1459 Power Network 最大流
- Qt运行一个实例进程
- acm国家集训队论文(1999-2009)
- try catch finally的执行顺序到底是怎样的?
- Combo Box的用法总结
- struct与class的区别
- poj1459Power Network(最大流)
- 学习learn python the hard way习题46, ImportError问题
- W3C之SQL函数
- HDU 1078 FatMouse and Cheese
- 程序员编程艺术第三十二~三十三章:最小操作数,木块砌墙问题
- 跨平台获取CPU cache line大小的方法
- 程序员编程艺术第三十~三十一章:字符串转换成整数,通配符字符串匹配
- linux 线程私有数据之一键多值技术TSD池
- 程序员编程艺术第二十八~二十九章:最大连续乘积子串、字符串编辑距离