题目链接:http://poj.org/problem?id=1459
题目大意:
多源多汇网络流.
题目思路:
学习网络流的第一题,也是拿来测模板.
定一个虚源点指向所有源点,容量为每个原点的Pmax.
定一个虚汇点使所有汇点指向它,容量为每个汇点的Cmax.
流一下得到最大流即可.
该题用邻接表写速度可能会变慢,因为邻接表里,会出现重边,但是不影响最终结果.
代码:
Edmonds-Karp
#include <stdlib.h> #include <string.h> #include <stdio.h> #include <ctype.h> #include <math.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <string> #include <iostream> #include <algorithm> using namespace std; #define ll long long #define ls rt<<1 #define rs ls|1 #define lson l,mid,ls #define rson mid+1,r,rs #define middle (l+r)>>1 #define eps (1e-9) #define type int #define clr_all(x,c) memset(x,c,sizeof(x)) #define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1)) #define MOD 1000000007 #define inf 0x3f3f3f3f #define pi acos(-1.0) template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;} template <class T> T _max(T x,T y){return x>y? x:y;} template <class T> T _min(T x,T y){return x<y? x:y;} int test,cas; const int M=100 +5; int n,m,np,nc; int cap[M][M]; int flow[M][M],fa[M]; int q[M],head,tail; char str[22]; int maxFlow(int s,int t){ clr_all(flow,0); int f=0; for(;;){ clr_all(fa,-1); for(q[head=tail=0]=s;head<=tail;){ int u=q[head++]; for(int v=0;v<=n+2;v++){ if(fa[v]==-1 && flow[u][v]<cap[u][v]){ fa[v]=u,q[++tail]=v; } } } if(fa[t]==-1) break; int a=inf; for(int u=t;u!=s;u=fa[u]) a=_min(a,cap[fa[u]][u]-flow[fa[u]][u]); for(int u=t;u!=s;u=fa[u]){ flow[fa[u]][u]+=a; flow[u][fa[u]]-=a; } f+=a; } return f; } void run(){ int i,j; clr_all(cap,0); int u,v,cp; while(m--){ scanf("%s",str); sscanf(str,"(%d,%d)%d",&u,&v,&cp); ++u,++v; cap[u][v]=cp; } while(np--){ scanf("%s",str); sscanf(str,"(%d)%d",&u,&cp); ++u; cap[0][u]=cp; } while(nc--){ scanf("%s",str); sscanf(str,"(%d)%d",&u,&cp); ++u; cap[u][n+2]=cp; } printf("%d\n",maxFlow(0,n+2)); } void preSof(){ } int main(){ //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); preSof(); //run(); while(~scanf("%d%d%d%d",&n,&np,&nc,&m)) run(); //for(scanf("%d",&test),cas=1;cas<=test;cas++) run(); return 0; }
Dinic(邻接表)
#include <stdlib.h> #include <string.h> #include <stdio.h> #include <ctype.h> #include <math.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <string> #include <iostream> #include <algorithm> using namespace std; #define ll long long #define ls rt<<1 #define rs ls|1 #define lson l,mid,ls #define rson mid+1,r,rs #define middle (l+r)>>1 #define eps (1e-9) #define type int #define clr_all(x,c) memset(x,c,sizeof(x)) #define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1)) #define MOD 1000000007 #define inf 0x3f3f3f3f #define pi acos(-1.0) template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;} template <class T> T _max(T x,T y){return x>y? x:y;} template <class T> T _min(T x,T y){return x<y? x:y;} int test,cas; const int M=100 +5; int n,m,np,nc; char str[22]; struct edge{ int from,to,cap,flow; edge(){} edge(int u,int v,int c,int f){from=u,to=v,cap=c,flow=f;} }; struct dinic{ vector<edge>edg; //边表 vector<int>g[M]; //邻接表:g[i][j]表示结点i连接的第j条边在edg中的序号 int n,m,s,t; //顶点数,边数(包括反向弧),源点,汇点 int dis[M],cur[M]; //从起点到i的距离,当前弧下标 int q[M],head,tail; void clear(){ edg.clear(); for(int i=0;i<=n;i++) g[i].clear(); } void insert(int u,int v,int c){ edg.push_back(edge(u,v,c,0)); edg.push_back(edge(v,u,0,0)); m=edg.size(); g[u].push_back(m-2); g[u].push_back(m-1); } int bfs(){ clr(dis,0,n);dis[s]=1; for(q[head=tail=0]=s;head<=tail;){ int u=q[head++]; for(int i=0;i<g[u].size();i++){ edge& e=edg[g[u][i]]; if(!dis[e.to] && e.cap>e.flow){ dis[e.to]=dis[e.from]+1; q[++tail]=e.to; } } } return dis[t]; } int dfs(int u,int a){ if(u==t || a==0) return a; int flow=0,f; for(int& i=cur[u];i<g[u].size();i++){ edge& e=edg[g[u][i]]; if(dis[e.to]==dis[u]+1 && (f=dfs(e.to,_min(a,e.cap-e.flow)))>0){ e.flow+=f,edg[g[u][i]^1].flow-=f; flow+=f,a-=f; if(a==0) break; } } return flow; } int maxFlow(int _s,int _t){ s=_s,t=_t; int flow=0; while(bfs()){ clr(cur,0,n); flow+=dfs(s,inf); } return flow; } }p; void run(){ int i,j; int u,v,cp; p.clear(); p.n=n+2; while(m--){ scanf("%s",str); sscanf(str,"(%d,%d)%d",&u,&v,&cp); p.insert(u+1,v+1,cp); } while(np--){ scanf("%s",str); sscanf(str,"(%d)%d",&u,&cp); p.insert(0,u+1,cp); } while(nc--){ scanf("%s",str); sscanf(str,"(%d)%d",&u,&cp); p.insert(u+1,n+2,cp); } printf("%d\n",p.maxFlow(0,n+2)); } void preSof(){ } int main(){ //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); preSof(); //run(); while(~scanf("%d%d%d%d",&n,&np,&nc,&m)) run(); //for(scanf("%d",&test),cas=1;cas<=test;cas++) run(); return 0; }
SAP(邻接表)
#include <stdlib.h> #include <string.h> #include <stdio.h> #include <ctype.h> #include <math.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <string> #include <iostream> #include <algorithm> using namespace std; #define ll long long #define ls rt<<1 #define rs ls|1 #define lson l,mid,ls #define rson mid+1,r,rs #define middle (l+r)>>1 #define eps (1e-9) #define type int #define clr_all(x,c) memset(x,c,sizeof(x)) #define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1)) #define MOD 1000000007 #define inf 0x3f3f3f3f #define pi acos(-1.0) #define _max(x,y) (((x)>(y))? (x):(y)) #define _min(x,y) (((x)<(y))? (x):(y)) #define _abs(x) ((x)<0? (-(x)):(x)) #define getmin(x,y) (x= (x==-1 || (y)<x)? (y):x) #define getmax(x,y) (x= ((y)>x)? (y):x) template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;} int test,cas; const int M=100 +5; int n,m,np,nc; char str[22]; struct sap{ struct edge{ int from,to,flow; edge(){} edge(int u,int v,int f){from=u,to=v,flow=f;} }; int n,m,s,t; //顶点数,边数(包括反向弧),源点,汇点 int dis[M],cur[M]; //从起点到i的距离,当前所在的弧 int gap[M],pre[M]; //距离标号计数,可增广路上的上一条弧的序号 vector<edge>edg; //边表 vector<int>g[M]; //g[i][j]表示点i连接的第j条边的序号 void clear(){edg.clear();for(int i=0;i<n;i++) g[i].clear();} void insert(int u,int v,int c){ edg.push_back(edge(u,v,c)),edg.push_back(edge(v,u,0)); m=edg.size(); g[u].push_back(m-2),g[v].push_back(m-1); } int maxFlow(int _s,int _t){ s=_s,t=_t; int res=0; clr(dis,0,n),clr(gap,0,n),clr(cur,0,n); gap[0]=n; for(int u=s,a;dis[s]<n;){ if(u==t){//增广 for(a=-1;u!=s;u=edg[pre[u]].from) getmin(a,edg[pre[u]].flow); for(u=t;u!=s;u=edg[pre[u]].from) edg[pre[u]].flow-=a,edg[pre[u]^1].flow+=a; res+=a; } bool ok=0; for(int i=cur[u];i<g[u].size();i++){//前推 edge& e=edg[g[u][i]]; if(dis[u]==dis[e.to]+1 && e.flow){ pre[e.to]=g[u][i],cur[u]=i,u=e.to,ok=1;break; } } if(!ok){//回退 int mindis=n-1; for(int i=0;i<g[u].size();i++){ edge& e=edg[g[u][i]]; if(e.flow && mindis>dis[e.to]) mindis=dis[e.to],cur[u]=i; } if(--gap[dis[u]]==0) break; gap[dis[u]=mindis+1]++; if(u!=s) u=edg[pre[u]].from; } } return res; } }p; void run(){ int i,j; int u,v,cp; p.clear(); p.n=n+3; while(m--){ scanf("%s",str); sscanf(str,"(%d,%d)%d",&u,&v,&cp); p.insert(u+1,v+1,cp); } while(np--){ scanf("%s",str); sscanf(str,"(%d)%d",&u,&cp); p.insert(0,u+1,cp); } while(nc--){ scanf("%s",str); sscanf(str,"(%d)%d",&u,&cp); p.insert(u+1,n+2,cp); } printf("%d\n",p.maxFlow(0,n+2)); } void preSof(){ } int main(){ //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); preSof(); //run(); while(~scanf("%d%d%d%d",&n,&np,&nc,&m)) run(); //for(scanf("%d",&test),cas=1;cas<=test;cas++) run(); return 0; }