标签:style blog http color io os ar for 数据
#include "cstdio" #include "vector" #include "cstring" #include "queue" using namespace std; #define maxn 405 #define inf 100000000 struct Edge { int from,to,cap,flow; Edge(int FROM,int TO,int CAP,int FLOW):from(FROM),to(TO),cap(CAP),flow(FLOW) {} }; int d[maxn],p[maxn],gap[maxn],cur[maxn]; bool vis[maxn]; vector<int> G[maxn]; vector<Edge> edges; int n,m; void addedge(int from,int to,int cap) { edges.push_back(Edge(from,to,cap,0)); edges.push_back(Edge(to,from,0,0)); int m=edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } void bfs(int s,int t) { memset(vis,false,sizeof(vis)); memset(d,0,sizeof(d)); memset(p,0,sizeof(p)); d[t]=0;vis[t]=true; queue<int> Q;Q.push(t); while(!Q.empty()) { int u=Q.front();Q.pop(); for(int v=0;v<G[u].size();v++) { Edge e=edges[G[u][v]^1]; if(!vis[e.from]&&e.cap>e.flow) { vis[e.from]=true; d[e.from]=d[u]+1; Q.push(e.from); } } } } int augment(int s,int t) { int x=t,a=inf; while(x!=s) { Edge e=edges[p[x]]; a=min(a,e.cap-e.flow); x=e.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 flow=0,u=s; bfs(s,t); memset(gap,0,sizeof(gap)); memset(cur,0,sizeof(cur)); for(int i=0;i<n;i++) gap[d[i]]++; while(d[s]<n) { if(u==t) { flow+=augment(s,t); u=s; } bool flag=false; for(int v=cur[u];v<G[u].size();v++) //Advance { Edge e=edges[G[u][v]]; if(e.cap>e.flow&&d[u]==d[e.to]+1) { flag=true; p[e.to]=G[u][v]; cur[u]=v; u=e.to; break; } } if(!flag) //Retreat { int m=n-1; for(int v=0;v<G[u].size();v++) { Edge e=edges[G[u][v]]; if(e.cap>e.flow) m=min(m,d[e.to]); } if(--gap[d[u]]==0) break; gap[d[u]=m+1]++; cur[u]=0; if(u!=s) u=edges[p[u]].from; } } return flow; } int main() { //freopen("in.txt","r",stdin); int u,v,w; while(~scanf("%d%d",&m,&n)) { for(int i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); } printf("%d\n",maxflow(1,n)); for(int i=0;i<maxn;i++) G[i].clear(); edges.clear(); } }
vector<Edge> Mincut(LL s,LL t) // call this after maxflow { BFS(s,t); vector<Edge> ans; for(int i = 0; i < edges.size(); i++) { Edge& e = edges[i]; if(!vis[e.from] && vis[e.to] && e.cap > 0) ans.push_back(e); } return ans; }
#include "cstdio" #include "vector" #include "cstring" #include "queue" using namespace std; #define maxn 210 #define inf 99999999 struct Edge { int from,to,cap,flow,cost; Edge(int FROM,int TO,int CAP,int FLOW,int COST):from(FROM),to(TO),cap(CAP),flow(FLOW),cost(COST) {} }; vector<int> G[maxn]; vector<Edge> edges; int d[maxn],p[maxn],a[maxn]; bool inq[maxn]; void addedge(int from,int to,int cap,int cost) { edges.push_back(Edge(from,to,cap,0,cost)); edges.push_back(Edge(to,from,0,0,-cost)); int m=edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool spfa(int s,int t,int &flow,int &cost) { memset(p,-1,sizeof(p)); for(int i=s;i<=t;i++) d[i]=(i==s?0:inf); a[s]=inf; queue<int> Q;Q.push(s); while(!Q.empty()) { int u=Q.front();Q.pop(); inq[u]=false; for(int v=0;v<G[u].size();v++) { Edge e=edges[G[u][v]]; if(e.cap>e.flow&&d[u]+e.cost<d[e.to]) { d[e.to]=d[u]+e.cost; a[e.to]=min(a[u],e.cap-e.flow); p[e.to]=G[u][v]; if(!inq[e.to]) { inq[e.to]=true; Q.push(e.to); } } } } if(d[t]==inf) return false; flow+=a[t]; cost+=d[t]; //很多人这里写着a[t]*d[t]是很坑的,最早的费用=权*流量,但不是永远都是这个,当然对于cap=1就无所谓了。 int u=t; while(u!=s) { edges[p[u]].flow+=a[t]; edges[p[u]^1].flow-=a[t]; u=edges[p[u]].from; } return true; }
@费用流与建模
标签:style blog http color io os ar for 数据
原文地址:http://www.cnblogs.com/neopenx/p/4004302.html