码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj3396[Usaco2009 Jan]Total flow 水流*

时间:2016-09-15 22:52:41      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

bzoj3396[Usaco2009 Jan]Total flow 水流

题意:

求无环图的最大流。边数≤700。

题解:

管它有没有环。注意本题的节点标号既有大写字母,也有小写字母。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 #define inc(i,j,k) for(int i=j;i<=k;i++)
 6 #define maxn 100
 7 #define INF 0x3fffffff
 8 using namespace std;
 9 
10 struct e{int t,c,n;}es[maxn*80]; int g[maxn],ess;
11 void pe(int f,int t,int c){
12     es[++ess]=(e){t,c,g[f]}; g[f]=ess; es[++ess]=(e){f,0,g[t]}; g[t]=ess;
13     es[++ess]=(e){f,c,g[t]}; g[t]=ess; es[++ess]=(e){t,0,g[f]}; g[f]=ess;
14 }
15 queue<int>q; int h[maxn];
16 bool bfs(int s,int t){
17     while(!q.empty())q.pop(); memset(h,-1,sizeof(h)); q.push(s); h[s]=0;
18     while(!q.empty()){
19         int x=q.front(); q.pop();
20         for(int i=g[x];i;i=es[i].n)if(es[i].c&&h[es[i].t]==-1){h[es[i].t]=h[x]+1; q.push(es[i].t);}
21     }
22     return h[t]!=-1;
23 }
24 int dfs(int x,int t,int f){
25     if(x==t)return f; int u=0;
26     for(int i=g[x];i;i=es[i].n)if(es[i].c&&h[es[i].t]==h[x]+1){
27         int w=dfs(es[i].t,t,min(f,es[i].c)); f-=w; u+=w;
28         es[i].c-=w; es[i^1].c+=w; if(f==0)return u;
29     }
30     if(u==0)h[x]=-1; return u;
31 }
32 int dinic(int s,int t){
33     int f=0; while(bfs(s,t))f+=dfs(s,t,INF); return f;
34 }
35 int s,t,n;
36 int main(){
37     scanf("%d",&n); ess=1; s=0; t=Z-A;
38     inc(i,1,n){char x[3],y[3]; int z; scanf("%s%s%d",x,y,&z); pe(x[0]-A,y[0]-A,z);}
39     printf("%d",dinic(s,t)); return 0;
40 }

 

20160908

bzoj3396[Usaco2009 Jan]Total flow 水流*

标签:

原文地址:http://www.cnblogs.com/YuanZiming/p/5875540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!