最大流ISAP模板
来源:互联网 发布:数据分析表格怎么做 编辑:程序博客网 时间:2024/05/16 09:08
//复杂度O(n^2*m)一般比dinic快些
#include<bits/stdc++.h>using namespace std;struct Edge{ int from,to,cap,flow;};const int maxn=650;const int INF=0x3f3f3f3f;struct ISAP{ int n,m,s,t;//结点数,边数(包括反向弧),源点编号,汇点编号 vector<Edge>edges; vector<int>G[maxn]; bool vis[maxn]; int d[maxn]; int cur[maxn]; int p[maxn]; int num[maxn]; void AddEdge(int from,int to,int cap){ edges.push_back((Edge){from,to,cap,0}); edges.push_back((Edge){to,from,0,0}); m=edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool RevBFS(){ memset(vis,0,sizeof(vis)); queue<int>Q; Q.push(t); d[t]=0; vis[t]=1; while(!Q.empty()){ int x=Q.front();Q.pop(); for(int i=0;i<G[x].size();i++){ Edge &e =edges[G[x][i]^1]; if(!vis[e.from]&&e.cap>e.flow){ vis[e.from]=1; d[e.from]=d[x]+1; Q.push(e.from); } } } return vis[s]; } int Augment(){ int x=t, a=INF; while(x!=s){ Edge &e = edges[p[x]]; a= min(a,e.cap-e.flow); x=edges[p[x]].from; } x=t; while(x!=s){ edges[p[x]].flow+=a; edges[p[x]^1].flow-=a; x=edges[p[x]].from; } return a; } int Maxflow(int s,int t,int n){ this->s=s,this->t=t,this->n=n; int flow=0; RevBFS(); memset(num,0,sizeof(num)); for(int i=0;i<n;i++){ num[d[i]]++; } int x=s; memset(cur,0,sizeof(cur)); while(d[s]<n){ if(x==t){ flow+=Augment(); x=s; } int ok=0; for(int i=cur[x];i<G[x].size();i++){ Edge &e =edges[G[x][i]]; if(e.cap>e.flow&&d[x]==d[e.to]+1){ ok=1; p[e.to]=G[x][i]; cur[x]=i; x=e.to; break; } } if(!ok){ int m=n-1; for(int i=0;i<G[x].size();i++){ Edge &e =edges[G[x][i]]; if(e.cap>e.flow) m=min(m,d[e.to]); } if(--num[d[x]]==0) break; num[d[x]=m+1]++; cur[x]=0; if(x!=s) x=edges[p[x]].from; } } return flow; }};int main(){ int n,m,a,b,c,res; while(scanf("%d%d",&m,&n)!=EOF){ ISAP tmp; for(int i=0;i<m;i++){ scanf("%d%d%d",&a,&b,&c); tmp.AddEdge(a,b,c); } res=tmp.Maxflow(1,n,n); printf("%d\n",res); } return 0;}
0 0
- 最大流ISAP模板
- 最大流 ISAP 模板
- 最大流ISAP模板
- 最大流ISAP模板
- 最大流 isap 模板
- 最大流ISAP算法模板
- 最大流模板(Dinic, ISAP)
- 最大流ISAP算法模板
- hdu3549及最大流ISAP模板
- hdu3549 Flow Problem 最大流模板 isap
- [最大流模板ISAP] codevs 1993
- 【模板】ISAP网络最大流 (模板题:洛谷P3376)
- 网络流模板:最大流ISAP算法和Dinic算法
- 网络流模板:最大流ISAP算法和Dinic算法
- 【最大流-ISAP】POJ-1273-网络流模板!!
- ACM网络流模板 最大流ISAP URAL1774
- isap算法模板poj 1273gap+弧优化 最大流
- HDU 3572 Task Schedule(ISAP模板&&最大流问题)
- Java子类的继承概念(一)
- ios学习笔记:使用CocoaPods
- mysql 序列创建函数
- ScalaNLP-breeze&breeze-viz总结
- table边框合并
- 最大流ISAP模板
- Java binarySearch 学习
- 在Visual Studio 2010中使用Visual Leak Detector
- C/C++程序设计06(面向对象)
- nyoj260 数数小木块
- Android (shape,gradient)详解
- hdu, KMP algorithm, linear string search algorithm, a nice reference provided
- vs未加载任何符号
- qtp 参数类 VBS 利用Default 关键字来 实现构造函数的功能