HDU 3549 Flow Problem 最大流 最小增广路 SAP算法 从EK算法的753MS降到了46MS
来源:互联网 发布:域名批量生成查询 编辑:程序博客网 时间:2024/05/16 06:45
EK 算法的链接:http://blog.csdn.net/ipqhjjybj/article/details/9171181
SAP算法果然对这个优化了好多。。时间直接从753MS降到46MS呀。。
题目描述
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)
#include <cstdio>#include <cmath>#include <cstdlib>#include <ctime>#include <iostream>#include <cmath>#include <algorithm>#include <numeric>#include <utility>#include <cstring>#include <vector>#include <stack>#include <queue>#include <map>#include <string>using namespace std;#define inf 0x3f3f3f3f#define MAXN 20#define MAXM 1005#define clr(x,k) memset((x),(k),sizeof(x))#define cpy(x,k) memcpy((x),(k),sizeof(x))#define Base 10000typedef vector<int> vi;typedef stack<int> si;typedef vector<string> vs;#define sz(a) int((a).size())#define pb push_back#define all(c) (c).begin(),(c).end()#define rep(i,n) for(int i = 0;i < n;++i)#define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)#define max(a,b) ((a)>(b)?(a):(b))#define min(a,b) ((a)<(b)?(a):(b))struct node{ int c,next,to;}edges[MAXM];int box[MAXN],n,m,tot;void add(int a,int b,int c){ edges[tot].to=b; edges[tot].c=c; edges[tot].next=box[a]; box[a]=tot++;}int Map[MAXN][MAXN]; //map:邻接数组 因为可能有重边情况。优化下,再用SAP算法void init(){ clr(Map,0); for(int i = 0,a,b,c;i < m;i++){ scanf("%d %d %d",&a,&b,&c); Map[a][b] += c; } tot=2; clr(box,-1); for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) if(Map[i][j]) add(i,j,Map[i][j]),add(j,i,0);}int SAP_Max_Flow(int start,int end,int N){ int numh[MAXN],h[MAXN],curedges[MAXN],pre[MAXN]; //numh: 用于GAP优化的统计高度数量数组;h:距离标号数组;curedges;当前弧数组,pre,前驱数组 int cur_flow,flow_ans=0,u,tmp,neck,i; clr(h,0); clr(numh,0); clr(pre,-1); for(i = 1;i <= n;i++) curedges[i]=box[i]; numh[0]=N; u=start; while(h[start]<N){ if(u==end){ cur_flow=inf; for(i=start;i!=end;i=edges[curedges[i]].to){ if(cur_flow>edges[curedges[i]].c){ neck=i; cur_flow=edges[curedges[i]].c; } } for(i=start;i!=end;i=edges[curedges[i]].to){ tmp=curedges[i]; edges[tmp].c-=cur_flow; edges[tmp^1].c+=cur_flow; } flow_ans+=cur_flow; u=neck; } for(i=curedges[u];i!=-1;i=edges[i].next){ if(edges[i].c&&h[u]==h[edges[i].to]+1) break; } if(i!=-1){ curedges[u]=i; pre[edges[i].to]=u; u=edges[i].to; } else{ if(0==--numh[h[u]])break; curedges[u]=box[u]; for(tmp=N,i=box[u];i!=-1;i=edges[i].next) if(edges[i].c) tmp=min(tmp,h[edges[i].to]); h[u]=tmp+1; ++numh[h[u]]; if(u!=start) u=pre[u]; } } return flow_ans;}int main(){ //freopen("3549.in","r",stdin); int t,tt=0; scanf("%d",&t); while(t--){ scanf("%d %d",&n,&m); init(); printf("Case %d: %d\n",++tt,SAP_Max_Flow(1,n,n)); } return 0;}
- Flow Problem 最大流 最小增广路 SAP算法 从EK算法的753MS降到了46MS
- HDU 3549 Flow Problem 最大流 最小增广路 SAP算法 从EK算法的753MS降到了46MS
- HDU 3549 Flow Problem 最大流 最小增广路 DINIC算法 也是46MS
- HDU 3549 Flow Problem 最大流 最小增广路 EK算法 传说中的入门算法
- hdu 3549 Flow Problem(最大流EK算法模板)
- HDU 3549 Flow Problem ( 最大流 -EK 算法)
- HDU 3549 Flow Problem【最大流模板题,EK算法】
- hdu 3549 Flow Problem(最大流 EK,sap)
- hdu 3549 Flow Problem 最大流入门 EK算法
- hdu3549 Flow Problem--EK算法 & 最大流
- hdu 3549 EK算法 Flow Problem
- hdu 3549 Flow Problem (EK 最大流)
- hdu 3549 Flow Problem 增广路ford-fullkerson算法
- hdu 3549 Flow Problem(网络流增广路径主要算法)
- 最大流的增广路算法(EK)
- HDU 3549 Flow Problem(最大流+EK邻接表||邻接矩阵||dinic算法模板比较)
- hdu 3549 Flow Problem ek算法,网络流
- hdu 3549 Flow Problem EK算法 网络流
- JavaScript----注释
- 给S5PV210裸机程序添加启动代码
- Cocos2d-X游戏开发之Json解析(七)
- 转载-redis全操作
- 放飞App:移动产品经理实战指南
- HDU 3549 Flow Problem 最大流 最小增广路 SAP算法 从EK算法的753MS降到了46MS
- C语言宏定义时#(井号)和##(双井号)的用法
- 实战 SSH 端口转发
- oracle 差异表数据插入
- SQL 行列转换
- java中equals和==的区别
- ORA-28000: the account is locked的解决办法
- 多线程调用COM组件的体会(CoInitialize)
- js 数字大写中文转换