Power Network
Time Limit: 2000MS   Memory Limit: 32768K
Total Submissions: 25514   Accepted: 13287

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

  1. 2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
  2. 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. (3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5
  4. (0)5 (1)2 (3)2 (4)1 (5)4

Sample Output

  1. 15
  2. 6
  3.  
  4. 觉得这篇题解写的很不错http://blog.csdn.net/lyy289065406/article/details/6647060
题目意思:给出n个节点(编号0到n-1),这些点里面有发电站(只负责发电),消耗站(只负责消耗电),转运站(只负责转运电)。现在给出每个发电站的最大发电量,消耗站的最大消耗量,转运站的最大转运量, 让你求出消耗站所能消耗的最大电量。 
 
思路:建立超级源0节点        只连通所有发电站,发电站的最大发电量为这条边上的容量。
            建立超级汇n+1节点   只连通所有消耗站,消耗站的最大消耗量为这条边上的容量。 这样讲问题转化成求超级源到超级汇的 最大流问题。
注意:增边的时候,把编号自加一,因为超级源为0节点,而题目中给的节点是从0开始的。
 
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stack>
  4. #include<queue>
  5. #include<algorithm>
  6. #define MAX 1100
  7. #define MAXM 40010
  8. #define INF 0x7fffff
  9. using namespace std;
  10. struct node
  11. {
  12. int from,to,cap,flow,next;
  13. }edge[MAXM];
  14. int n,m,np,nc;
  15. int ans,head[MAX];
  16. int vis[MAX];//用bfs求路径时判断当前点是否进队列,
  17. int dis[MAX];//当前点到源点的距离
  18. int cur[MAX];//保存该节点正在参加计算的弧避免重复计算
  19. void init()
  20. {
  21. ans=0;
  22. memset(head,-1,sizeof(head));
  23. }
  24. void add(int u,int v,int w)
  25. {
  26. node E1={u,v,w,0,head[u]};
  27. edge[ans]=E1;
  28. head[u]=ans++;
  29. node E2={v,u,0,0,head[v]};
  30. edge[ans]=E2;
  31. head[v]=ans++;
  32. }
  33. void getmap()
  34. {
  35. int a, b, d;
  36. while(m--)
  37. {
  38. scanf(" (%d,%d)%d", &a, &b, &d);
  39. add(a+1, b+1, d);
  40. }
  41. while(np--)
  42. {
  43. scanf(" (%d)%d", &b, &d);
  44. add(0, b+1, d);//超级源
  45. }
  46. while(nc--)
  47. {
  48. scanf(" (%d)%d", &a, &d);
  49. add(a+1, n+1, d);//超级汇
  50. }
  51. }
  52.  
  53. int bfs(int beg,int end)
  54. {
  55. int i;
  56. memset(vis,0,sizeof(vis));
  57. memset(dis,-1,sizeof(dis));
  58. queue<int>q;
  59. while(!q.empty())
  60. q.pop();
  61. vis[beg]=1;
  62. dis[beg]=0;
  63. q.push(beg);
  64. while(!q.empty())
  65. {
  66. int u=q.front();
  67. q.pop();
  68. for(i=head[u];i!=-1;i=edge[i].next)//遍历所有的与u相连的边
  69. {
  70. node E=edge[i];
  71. if(!vis[E.to]&&E.cap>E.flow)//如果边未被访问且流量未满继续操作
  72. {
  73. dis[E.to]=dis[u]+1;//建立层次图
  74. vis[E.to]=1;//将当前点标记
  75. if(E.to==end)//如果当前点搜索到终点则停止搜索 返回1表示有从原点到达汇点的路径
  76. return 1;
  77. q.push(E.to);//将当前点入队
  78. }
  79. }
  80. }
  81. return 0;//返回0表示未找到从源点到汇点的路径
  82. }
  83. int dfs(int x,int a,int end)//把找到的这条边上的所有当前流量加上a(a是这条路径中的最小残余流量)
  84. {
  85. //int i;
  86. if(x==end||a==0)//如果搜索到终点或者最小的残余流量为0
  87. return a;
  88. int flow=0,f;
  89. for(int& i=cur[x];i!=-1;i=edge[i].next)//i从上次结束时的弧开始
  90. {
  91. node& E=edge[i];
  92. if(dis[E.to]==dis[x]+1&&(f=dfs(E.to,min(a,E.cap-E.flow),end))>0)//如果
  93. {//bfs中我们已经建立过层次图,现在如果 dis[E.to]==dis[x]+1表示是我们找到的路径
  94. //如果dfs>0表明最小的残余流量还有,我们要一直找到最小残余流量为0
  95. E.flow+=f;//正向边当前流量加上最小的残余流量
  96. edge[i^1].flow-=f;//反向边
  97. flow+=f;//总流量加上f
  98. a-=f;//最小可增流量减去f
  99. if(a==0)
  100. break;
  101. }
  102. }
  103. return flow;//所有边加上最小残余流量后的值
  104. }
  105. int Maxflow(int beg,int end)
  106. {
  107. int flow=0;
  108. while(bfs(beg,end))//存在最短路径
  109. {
  110. memcpy(cur,head,sizeof(head));//复制数组
  111. flow+=dfs(beg,INF,end);
  112. }
  113. return flow;//最大流量
  114. }
  115. int main()
  116. {
  117. int i,j;
  118. while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF)
  119. {
  120. init();
  121. getmap();
  122. printf("%d\n",Maxflow(0,n+1));
  123. }
  124. return 0;
  125. }

  

poj 1459 Power Network【建立超级源点,超级汇点】的更多相关文章

  1. POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)

    POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...

  2. poj 1459 Power Network

    题目连接 http://poj.org/problem?id=1459 Power Network Description A power network consists of nodes (pow ...

  3. poj 1459 Power Network : 最大网络流 dinic算法实现

    点击打开链接 Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 20903   Accepted:  ...

  4. POJ 1459 Power Network 最大流(Edmonds_Karp算法)

    题目链接: http://poj.org/problem?id=1459 因为发电站有多个,所以需要一个超级源点,消费者有多个,需要一个超级汇点,这样超级源点到发电站的权值就是发电站的容量,也就是题目 ...

  5. 2018.07.06 POJ 1459 Power Network(多源多汇最大流)

    Power Network Time Limit: 2000MS Memory Limit: 32768K Description A power network consists of nodes ...

  6. 【POJ 1459 power network】

    不可以理解的是,测评站上的0ms是怎么搞出来的. 这一题在建立超级源点和超级汇点后就变得温和可爱了.其实它本身就温和可爱.对比了能够找到的题解: (1)艾德蒙·卡普算法(2)迪尼克算法(3)改进版艾德 ...

  7. POJ 1459 - Power Network 【Ek-最大流】

    <题目链接> 题目大意:给出 n 个点,其中包括 np个发电站,nc 个消费者, 剩下的全部都是中转点,再给出 这些点中的m 条边,代表这两点间的最大传输电量,并且给出发电站的最大发送电量 ...

  8. POJ - 1459 Power Network(最大流)(模板)

    1.看了好久,囧. n个节点,np个源点,nc个汇点,m条边(对应代码中即节点u 到节点v 的最大流量为z) 求所有汇点的最大流. 2.多个源点,多个汇点的最大流. 建立一个超级源点.一个超级汇点,然 ...

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

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

随机推荐

  1. 数据库应该使用异步吗 Should my database calls be Asynchronous?

    Should my database calls be Asynchronous? http://blogs.msdn.com/b/rickandy/archive/2009/11/14/should ...

  2. Grails的redirect无法跳转时的一个可能原因

    由于controller的命名一般首字母大写,如Login 此时如 class LoginController { def index = { redirect(action:Login, param ...

  3. 领域驱动设计和实践(转:http://kb.cnblogs.com/page/112298/)

    引言 软件系统面向对象的设计思想可谓历史悠久,20世纪70年代的Smalltalk可以说是面向对象语言的经典,直到今天我们依然将这门语言视为面向对象语言的基础.随着编程语言和技术的发展,各种语言特性层 ...

  4. python 图片上添加数字源代码

    最近因工作需要,需要在图片上添加数字,查询了资料,自己写了一个方法,并进行了测试,由于代码用到了PIL库,需要下载安装,下载地址:http://www.pythonware.com/products/ ...

  5. 基于jQuery的上下左右无缝滚动应用(单行或多行)

    $(function(){     var _wrap=$('ul.line');//定义滚动区域     var _interval=2000;//定义滚动间隙时间     var _moving; ...

  6. 黑客是怎样绕过WAF之三重防护绕过讲解

    什么是WAF Web Application Firewall 通过执行一系列针对HTTP/HTTPS的安全策略来防御对Web应用的攻击. 目前主要有单设备WAF与云WAF WAF的现状 1.太多数W ...

  7. MD5算法步骤详解

    转自MD5算法步骤详解 之前要写一个MD5程序,但是从网络上看到的资料基本上一样,只是讲了一个大概.经过我自己的实践,我决定写一个心得,给需要实现MD5,但又不要求很高深的编程知识的童鞋参考.不多说了 ...

  8. 使用flask的时候遇到的问题及其解答

    在网上看到了mircoblog的这个web程序,用flask框架写的,自己就在windows的环境下实现了下. 1.这个博客系统用到了一个flask插件叫flask_Login 里面涉及到编码解码的问 ...

  9. 转一贴,今天实在写累了,也看累了--【Python异步非阻塞IO多路复用Select/Poll/Epoll使用】

    下面这篇,原理理解了, 再结合 这一周来的心得体会,整个框架就差不多了... http://www.haiyun.me/archives/1056.html 有许多封装好的异步非阻塞IO多路复用框架, ...

  10. SQL SERVER查看当前连接情况

    使用超级管理员账户登录,并执行以下命令: SELECT * FROM [Master].[dbo].[SYSPROCESSES] WHERE [DBID] IN ( SELECT [DBID] FRO ...