hdu1532(最大流)
来源:互联网 发布:java语言程序设计 pdf 编辑:程序博客网 时间:2024/05/20 15:59
好久没做题了,应该有2个多月了,最近在准备找实习工作,看了几道笔试题,遇到一道网络流的题目,可是做不来,所以又来学下网络流。
这是一道最大流的模板题。
思路:用bfs找最短增广路,求得最短增广路上能通过的最大流量flow,然后该路径上的每条边减去flow,在添加相应的反向边,重复上述操作,知道找不到增广路为止。注意重边!
代码如下:
#include <iostream>#include <stdio.h>#include <string.h>#include <queue>#define N 205#define INF 0x3f3f3f3fusing namespace std;int a[N][N],vis[N],pre[N];int n,m;//n node ,m edgestruct node{ int minn,id;};queue<node> q;int bfs(){ memset(vis,0,sizeof(vis)); memset(pre,0,sizeof(pre)); while(!q.empty()) q.pop(); node temp; temp.id = 1; temp.minn = INF; q.push(temp); vis[1] = 1; while(!q.empty()) { node now = q.front(); q.pop(); if(now.id == n) return now.minn; for(int i = 1; i <= n; i++) if(!vis[i] && a[now.id][i] > 0) { temp.id = i; temp.minn = min(now.minn,a[now.id][i]); q.push(temp); pre[i] = now.id; vis[i] = 1; } } return -1;}int EK(){ int ans = 0; while(1) { int flow = bfs(); if(flow == -1) break; ans += flow; int x = n; while(x) { a[x][ pre[x] ] += flow; a[ pre[x] ][x] -= flow; x = pre[x]; } } return ans;}int main(){ while(scanf("%d%d",&m,&n) != EOF) { int i,x,y,z; memset(a,0,sizeof(a)); for(i = 0; i < m; i++) { scanf("%d%d%d",&x,&y,&z); a[x][y] += z; } printf("%d\n",EK()); } return 0;}/*13 71 2 281 4 191 3 72 5 152 3 63 4 124 2 74 5 144 7 365 7 235 6 76 3 106 7 18*/
0 0
- hdu1532(最大流)
- HDU1532 Drainage Ditches(最大流)
- hdu1532 Drainage Ditches(最大流)
- HDU1532 Drainage Ditches (最大流)
- hdu1532-Drainage Ditches(最大流&EK)
- 最大网络流--hdu1532
- 最大流模板 hdu1532
- HDU1532 Drainage Ditches 最大流
- hdu1532 最大流 EK算法
- HDU1532 Drainage Ditches 【最大流】
- hdu1532--Drainage Ditches(最大流(EK算法))
- HDU1532 Drainage Ditches —— 最大流(sap算法)
- HDU1532 最大流-模板题
- hdu1532最大流EK与SAP算法
- HDU1532 Drainage Ditches【最大流】【Edmond-Karp】
- 【最大流之sap】【HDU1532】模板题
- hdu1532 Drainage Ditches--最大流ek
- HDU1532 Drainage Ditches 最大流Dinic模板
- hiho一下第二周:Trie树
- hdu2586 树上两点之间的距离 tarjan
- 农夫卖鸡问题->数学趣题
- poj完整分类
- Android Progress进度条代码,进度条刷新问题探讨
- hdu1532(最大流)
- 基于SmartAction工具的自动化,研究与探索
- 手机短信验证码的实现
- 半秒内筛一亿以内的所有素数
- 4个月,工厂女工转型魔都白领
- 沙龙邮箱助手
- 用C语言爬网页代码
- CodeForces 519D A and B and Interesting Substrings
- uva难度分级列表