Drainage Ditches---hdu1532(最大流, 模板)
2015-09-12 14:57
579 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532
最大流模板题;
EK:(复杂度为n*m*m);
View Code
最大流模板题;
EK:(复杂度为n*m*m);
#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; #define N 220 #define INF 0xfffffff int n, ans, Head , cnt, Layer ; struct Edge { int v, flow, next; } e[2*N]; void Add(int u, int v, int flow) { e[cnt].v = v; e[cnt].flow = flow; e[cnt].next = Head[u]; Head[u] = cnt++; } bool bfs(int S, int E) { memset(Layer, 0, sizeof(Layer)); Layer[S] = 1; queue<int>Q; Q.push(S); int p, q; while(!Q.empty()) { p = Q.front(); Q.pop(); if(p == E)return true; for(int i=Head[p]; i!=-1; i=e[i].next) { q = e[i].v; if(!Layer[q] && e[i].flow) { Layer[q] = Layer[p]+1; Q.push(q); } } } return false; } int dfs(int u, int MaxFlow, int E) { if(u == E)return MaxFlow; int uflow=0; for(int i=Head[u]; i!=-1; i=e[i].next) { int v = e[i].v; if(Layer[v]==Layer[u]+1 && e[i].flow) { int flow = min(e[i].flow, MaxFlow - uflow); flow = dfs(v, flow, E); e[i].flow -= flow; e[i^1].flow += flow; uflow += flow; if(uflow==MaxFlow)break; } } if(uflow==0) Layer[u]=0; return uflow; } void Dinic() { while(bfs(1, n)) { ans+=dfs(1, INF, n); } } int main() { int a, b, flow, m; while(scanf("%d%d", &m, &n)!=EOF) { memset(Head, -1, sizeof(Head)); cnt = 0; for(int i=1; i<=m; i++) { scanf("%d%d%d", &a, &b, &flow); Add(a, b, flow); Add(b, a, 0); } ans = 0; Dinic(); printf("%d\n", ans); } return 0; }
View Code
相关文章推荐
- bundle update rails 报错处理
- State模式及与Strategy/Commmand/Chain of Resposibility区别
- kamailio服务器内容补充
- storcli中raid级别之间的转换
- HUD1022 Train Problem I
- Contains Duplicate II 解答
- hdoj 1787 GCD Again【欧拉函数】
- pthread_barrier_wait()源码解析
- Linux 文件系统(三)---dup和fork函数执行后的文件情况 http://blog.csdn.net/shanshanpt/article/details/39049579
- CodeForces - 240E Road Repairs(最小树形图+输出路径)
- hdoj 1022 Train Problem I【栈】
- 使用dup和dup2重定向和还原 http://blog.csdn.net/kangear/article/details/24098315
- hdu5029Relief grain 线段树
- kvm随笔系列二: Qemu中的AIO小结
- 使用Windows的分析等待链(analyze wait chain)来诊断没用响应的应用
- Chain of Responsibility - 责任链模式
- Websphere8 AIX静默安装卸载升级
- ADB server didn't ACK failed to start daemon *解决方案
- AIX采用LV创ASM磁盘组
- 使用Keychain存储用户敏感信息