标签:problem cstring += string algo pen onclick stdin splay
慢慢更...
HihoCoder上的最大流模板题:网络流二·最大流最小割定理
抄的《挑战程序设计竞赛》上的代码
#include <cstdio> #include <cmath> #include <vector> #include <algorithm> #include <queue> #include <cstring> using namespace std; const int MAX=10005; const int INF=1<<30; struct edge { int to,cap,rev; edge(int x,int y,int z) { to=x,cap=y,rev=z; } }; int s,t; vector<edge> v[MAX]; inline void add_edge(int x,int y,int cap) { v[x].push_back(edge(y,cap,v[y].size())); v[y].push_back(edge(x,0,v[x].size()-1)); } int level[MAX]; queue<int> Q; inline void bfs() { memset(level,-1,sizeof(level)); level[s]=0; Q.push(s); while(!Q.empty()) { int x=Q.front(); Q.pop(); for(int i=0;i<v[x].size();i++) { edge &e=v[x][i]; if(e.cap>0 && level[e.to]<0) { level[e.to]=level[x]+1; Q.push(e.to); } } } } int iter[MAX]; inline int dfs(int x,int f) { if(x==t) return f; for(int &i=iter[x];i<v[x].size();i++) { edge &e=v[x][i]; if(e.cap>0 && level[e.to]>level[x]) { int d=dfs(e.to,min(f,e.cap)); if(d>0) { e.cap-=d; v[e.to][e.rev].cap+=d; return d; } } } return 0; } inline int max_flow() { int flow=0; while(1) { bfs(); if(level[t]<0) break; memset(iter,0,sizeof(iter)); int f=0; while((f=dfs(s,INF))>0) flow+=f; } return flow; } int n,m; int main() { // freopen("input.txt","r",stdin); scanf("%d%d",&n,&m); s=1,t=n; for(int i=1;i<=m;i++) { int x,y,cap; scanf("%d%d%d",&x,&y,&cap); add_edge(x,y,cap); } printf("%d ",max_flow()); vector<int> cut; for(int i=1;i<=n;i++) if(level[i]>=0) cut.push_back(i); printf("%d\n",(int)cut.size()); for(int i=0;i<cut.size();i++) printf("%d ",cut[i]); return 0; }
标签:problem cstring += string algo pen onclick stdin splay
原文地址:https://www.cnblogs.com/LiuRunky/p/Network_Flow.html