POJ 1459 Power Network
2015-10-03 11:39
344 查看
最大流问题。
题意,表示电网有三个站点, 发电站。集线器,用户站。 超直接建立S 和 T 。
S-> 发电站 发电量。用户-> T 容量是电。
然后,你可以找到的最大流量。
题意,表示电网有三个站点, 发电站。集线器,用户站。 超直接建立S 和 T 。
S-> 发电站 发电量。用户-> T 容量是电。
然后,你可以找到的最大流量。
#include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream> #include<list> #include<set> #include<cmath> #define INF 0x7fffffff #define eps 1e-6 #define LL long long #define acfun std::ios::sync_with_stdio(false) using namespace std; int n,np,nc,m; struct lx { int c,f; }; lx g[301][301]; bool vis[301]; int path[301]; int flow[301]; void EK(int start,int thend) { int maxflow=0; while(1) { for(int i=0;i<n+2;i++) vis[i]=0,path[i]=-1,flow[i]=0; queue<int>q; q.push(start); vis[start]=1,flow[start]=INF; while(!q.empty()&&!vis[thend]) { int u=q.front();q.pop(); for(int j=0;j<n+2;j++) { if(vis[j])continue; if(g[u][j].f<g[u][j].c) { vis[j]=1; path[j]=u; flow[j]=min(flow[u],g[u][j].c-g[u][j].f); q.push(j); } else if(g[j][u].f>0) { vis[j]=1; path[j]=u; flow[j]=min(flow[u],g[j][u].f); q.push(j); } } } if(!vis[thend]||flow[thend]==0)break; int v=thend,u=path[thend]; int tmp=flow[thend]; maxflow+=tmp; while(u!=-1) { if(g[u][v].f<g[u][v].c) g[u][v].f+=tmp; else g[v][u].f-=tmp; v=u,u=path[v]; } } printf("%d\n",maxflow); } int main() { while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF) { for(int i=0;i<n+2;i++) for(int j=0;j<n+2;j++) g[i][j].c=g[i][j].f=0; int u,v,c; char str[1001]; while(m--) { scanf("%s",str); sscanf(str,"(%d,%d)%d",&u,&v,&c); g[u][v].c=c; } for(int i=0;i<np;i++) { scanf("%s",str); sscanf(str,"(%d)%d",&v,&c); g [v].c=c; } for(int i=0;i<nc;i++) { scanf("%s",str); sscanf(str,"(%d)%d",&u,&c); g[u][n+1].c=c; } EK(n,n+1); } }
相关文章推荐
- 面试题25:二叉树中和为某一值的路径
- android---Activity仿Dialog
- 产生一个螺旋矩阵
- SVN安装步骤的页面地址
- 学习ThinkPHP3.2.2:video8,以数组方式显示插入的记录
- C++学习(一)-纯虚函数和抽象类(4)
- 马哥运维笔记----4
- SQL查询分析器使用
- 下载、编译Android5.1.0_r2源码简明版
- 学习ThinkPHP3.2.2:video8,提交页面后的处理方法包括有效判断数据插入页面跳转功能
- 类String
- 35. Search Insert Position (Array)
- PAT1010 一元多项式求导 (25)
- Lua错误:nesting of [[...]] is deprecated near '['
- 学习ThinkPHP3.2.2:video8,用M函数的delete方法删除wish表中的所有数据
- 一个老程序员的5点建议
- [LeetCode] 116 - Populating Next Right Pointers in Each Node
- Apache Struts 2入门指南
- 非排序寻找从小到大第3400个数
- pat1016Phone Bills (25)