hdu 1532 Network Flow Drainage Ditches
来源:互联网 发布:程序员又叫什么 编辑:程序博客网 时间:2024/06/06 00:28
Using the code solve max_flow problem.I just using the mould.I don't know the details of isap algorithm.
ps:This is my first solve Network Flow problem.
The portal : http://acm.hdu.edu.cn/showproblem.php?pid=1532
#include<iostream>#include <cstdio>#include <cmath>#include <memory.h>#define MAXN 100005#define MAXE 2000000#define INF 0x3f3f3f3fusing namespace std;int tmp,src,des,cnt;int n,m;struct Edge{int from,to;int next,cap;}edge[MAXE];int head[MAXN];int gap[MAXN],dep[MAXN],cur[MAXN],stack[MAXN],top;int isap(){int cur_flow,max_flow,u,insert,i;memset(dep,0,sizeof(dep));memset(gap,0,sizeof(gap));memcpy(cur,head,n);max_flow = 0;u = src;top = 0;while(dep[src] < n){if(u == des){cur_flow = INF;for(i = 0;i < top;i++){if(cur_flow > edge[stack[i]].cap){insert = i;cur_flow = edge[stack[i]].cap;}}for(i = 0;i < top;i++){edge[stack[i]].cap -= cur_flow;edge[stack[i]^1].cap += cur_flow;}max_flow += cur_flow;u = edge[stack[top = insert]].from;}for(i = cur[u];i != -1;i=edge[i].next){if((edge[i].cap > 0)&&(dep[u] == dep[edge[i].to]+1))break;}if(i != -1){stack[top++] = i;u = edge[i].to;}else{if(0 == --gap[dep[u]])break;int minn = n;for(i = head[u];i!=-1;i=edge[i].next){if(edge[i].cap > 0)minn = (minn > dep[edge[i].to]) ? (cur[u] = i,dep[edge[i].to]) : minn;}++gap[dep[u] = minn + 1];if(u != src) u = edge[stack[--top]].from;}}return max_flow;}void addedge(int u,int v,int f){edge[cnt].next = head[u];edge[cnt].from = u;edge[cnt].to = v;edge[cnt].cap = f;head[u] = cnt ++;edge[cnt].next = head[v];edge[cnt].from = v;edge[cnt].to = u;edge[cnt].cap = 0;head[v] = cnt ++;}int main(void){while(~scanf("%d%d",&m,&n)){cnt = 0;src = 0;des = n-1;memset(head,-1,sizeof(head));while(m--){int a,b,c;scanf("%d %d %d",&a,&b,&c);--a; --b;addedge(a,b,c);}int ans = isap();printf("%d\n",ans);}return 0;}
0 0
- hdu 1532 Network Flow Drainage Ditches
- HDU 1532 Drainage Ditches
- hdu 1532 Drainage Ditches
- HDU 1532 Drainage Ditches
- hdu 1532 Drainage Ditches
- HDU 1532 Drainage Ditches
- hdu 1532 Drainage Ditches
- hdu 1532Drainage Ditches
- hdu 1532 Drainage Ditches
- hdu 1532 Drainage Ditches
- HDU 1532 Drainage Ditches
- HDU 1532 Drainage Ditches
- HDU 1532 Drainage Ditches
- hdu 1532 Drainage Ditches
- 【HDU】 1532 Drainage Ditches
- HDU 1532 - Drainage Ditches
- HDU 1532Drainage Ditches
- HDU 1532Drainage Ditches
- 进程与线程(二)
- 堆栈类模板
- oracle包、触发器2
- HTTP协议是什么?
- oracle包、触发器
- hdu 1532 Network Flow Drainage Ditches
- SqlServer INSTEAD OF UPDATE 视图触发器问题
- oracle自定义函数、存储过程2
- oracle自定义函数、存储过程
- Java外观模式(Facade模式)
- 创建三角形类
- ARM中的预取命令pld的使用
- oracle常用操作1
- IOS开发系列—Objective-C之Foundation框架