isap测模板--HDU3549
来源:互联网 发布:网络发帖管理规定 编辑:程序博客网 时间:2024/05/16 00:57
Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 5990 Accepted Submission(s): 2790
Problem Description
Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
Input
The first line of input contains an integer T, denoting the number of test cases.
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
Output
For each test cases, you should output the maximum flow from source 1 to sink N.
Sample Input
2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1
Sample Output
Case 1: 1
Case 2: 2
Case 2: 2
/***************************************** author:crazy_石头* algorithm:sap+当前弧+gap优化* date:2013/09/29*****************************************/#include<cstdio>#include<cstring>#include<climits>using namespace std;#define N 90005#define M 4000005#define INF INT_MAXint s,t,num,pre[N],dis[N],gap[N],head[N],cur[N];struct node{ int u,v,w; int next;}e[M];inline void add(int u,int v,int w){ e[num].u=u; e[num].v=v; e[num].w=w; e[num].next=head[u]; head[u]=num++;}inline void addedge(int u,int v,int w){ add(u,v,w); add(v,u,0);}inline int max(int a,int b){ return a>b?a:b;}inline int min(int a,int b){ return a<b?a:b;}int isap(int s,int t,int n){ int top,mindis,maxflow=0,v,i,aug; bool flag; for(i=0;i<=n;i++) { cur[i]=head[i]; gap[i]=dis[i]=0; } top=pre[s]=s; aug=INF; while(dis[s]<n) { flag=0; for(i=cur[top];i!=-1;i=e[i].next) { v=e[i].v; if(e[i].w>0&&dis[top]==dis[v]+1) { flag=1; break; } } if(flag) { pre[v]=top; cur[top]=i; aug=min(aug,e[i].w); top=v; if(top==t) { while(top!=s) { top=pre[top]; e[cur[top]].w-=aug; e[cur[top]^1].w+=aug; } top=s; maxflow+=aug; aug=INF; } } else { if(--gap[dis[top]]==0) break; mindis=n; cur[top]=head[top]; for(i=head[top];i!=-1;i=e[i].next) { v=e[i].v; if(e[i].w>0&&dis[v]+1<mindis) { mindis=dis[v]+1; cur[top]=i; } } dis[top]=mindis; gap[mindis]++; if(top!=s) top=pre[top]; } } return maxflow;}void init(){ num=0; memset(head,-1,sizeof(head));}int main(){ int nodenum,m,n,test; scanf("%d",&test); for(int ii=1;ii<=test;ii++) { scanf("%d%d",&n,&m); init(); while(m--) { int u,v,w; scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); } //source=1,sink=n,nodenum=n; int res=isap(1,n,n); printf("Case %d: %d\n",ii,res); } return 0;}
- isap测模板--HDU3549
- hdu3549及最大流ISAP模板
- hdu3549 Flow Problem 最大流模板 isap
- HDU3549 ISAP板子题
- Hdu3549 Flow Problem(isap+bfs优化)
- (模板)isap
- ISAP模板
- hdu3549 Flow Problem(dinic算法和ISAP算法)
- hdu3549 最大流模板题
- hdu3549 网络流ek模板
- 网络流dinic模板 hdu3549
- Isap 的 CPP 模板
- 最大流ISAP模板
- 我的 ISAP 模板
- ISAP模板 邻接表
- 最大流 ISAP 模板
- 最大流ISAP模板
- isap sap 模板
- iOS SEL类型
- 【读书笔记】深入浅出数据分析
- 字符串 之 KMP算法
- 第2次会议纪要
- DCMTK3.6.0库在vs2010下编译成功通过
- isap测模板--HDU3549
- EXT.net弹出框传值与返回值
- 1.3 基础知识——GP2.1 方针(Policy)
- Mysql 主从数据库同步使用感受
- 达夫设备(Duff's Device)
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- Maven常用插件介绍
- Linux下升级Nodejs --nvm
- 当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法?