HDU 3549 Flow Problem - 更新dinic模板..整成结构体...
2013-08-24 12:39
435 查看
我发现我以前写的dinic并没有完全做对...效率不高...这次更新了dinic的模板..整成一个结构体..用起来方便..效率不错....不过效率没有sap高..按道理来说dinic和sap的效率没有差别的...实在不想学sap了...先这样吧....
Program:
Program:
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<math.h> #include<queue> #define MAXN 5005 #define MAXM 50005 #define oo 2000000000 #define ll long long using namespace std; struct Dinic { struct node { int x,y,c,next; }line[MAXM]; int Lnum,_next[MAXN],dis[MAXN]; void initial() { memset(_next,-1,sizeof(_next)),Lnum=-1;} void addline(int x,int y,int c) { line[++Lnum].next=_next[x],_next[x]=Lnum; line[Lnum].x=x,line[Lnum].y=y,line[Lnum].c=c; line[++Lnum].next=_next[y],_next[y]=Lnum; line[Lnum].x=y,line[Lnum].y=x,line[Lnum].c=0; } bool BFS(int s,int e) { queue<int> Q; while (!Q.empty()) Q.pop(); memset(dis,0,sizeof(dis)); dis[s]=1; Q.push(s); while (!Q.empty()) { int h,k; h=Q.front(),Q.pop(); if (h==e) return dis[e]; for (k=_next[h];k!=-1;k=line[k].next) if (line[k].c && !dis[line[k].y]) dis[line[k].y]=dis[h]+1,Q.push(line[k].y); } return false; } int dfs(int x,int flow,int e) { if (x==e) return flow; int temp,cost=0; for (int k=_next[x];k!=-1;k=line[k].next) if (line[k].c && dis[line[k].y]==dis[x]+1) { temp=dfs(line[k].y,min(flow-cost,line[k].c),e); if (temp) { line[k].c-=temp,line[k^1].c+=temp; cost+=temp; if (flow==cost) return cost; }else dis[line[k].y]=-1; } return cost; } int MaxFlow(int s,int e) { int MaxFlow=0; while (BFS(s,e)) MaxFlow+=dfs(s,oo,e); return MaxFlow; } }T; int main() { int i,x,y,c,n,m,cases,C; scanf("%d",&C); for (cases=1;cases<=C;cases++) { scanf("%d%d",&n,&m); T.initial(); while (m--) { scanf("%d%d%d",&x,&y,&c); T.addline(x,y,c); } printf("Case %d: %d\n",cases,T.MaxFlow(1,n)); } return 0; }
相关文章推荐
- HDU 3549 Flow Problem(最大流模板)
- hdu 3549 Flow Problem(最大流模板题)
- hdu 3549 Flow Problem (ek算法模板)
- hdu 3549 Flow Problem 最大流模板题 Ford-Fulkerson算法
- hdu 3549 Flow Problem(简单网络流Dinic)
- hdu 3549 Flow Problem(最大流模板题)
- hdu 3549 Flow Problem (最大流—EK—Dinic)
- hdu - 3549 Flow Problem (最大流模板题)
- HDU 3549 Flow Problem (Ford-Fulkerson&Dinic)
- 【网络流第三弹】HDU 3549——Flow Problem(dinic解法)
- hdu 3549 Flow Problem(最大流EK算法模板)
- HDU 3549 Flow Problem 网络最大流问题 EK、Dinic、ISAP三种算法
- 文章标题 HDU 3549 : Flow Problem (最大流--模板)
- hdu 3549 Flow Problem(最大流模板)
- 【最大流+模板题】杭电 hdu 3549 Flow Problem
- [ACM] hdu 3549 Flow Problem (最大流模板题)
- hdu 1532 POJ 1273 Drainage Ditches 和hdu 3549 Flow Problem 网络流入门(EK和dinic)
- HDU-3549 Flow Problem (最大流模板)
- [HDU] 3549 Flow Problem [最大流][Dinic][读取优化]
- hdu 3549 Flow Problem【Dinic最大流】