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

USACO草地排水-网络流dinic模板

时间:2017-03-12 11:26:18      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:logs   code   scan   back   网络流   set   ++   标准   scanf   

广搜计算层次图,在层次图上深搜。标准dinic模板。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<vector>
 5 #include<queue>
 6 #include<cmath>
 7 #include<algorithm>
 8 #include<string.h>
 9 #define INF 0x7fffffff
10 using namespace std;
11 struct edge{
12        int to,f,rev;
13 };
14 int ch[210];
15 int N,M,ans;
16 vector <edge> E[210];
17 void add_edge(int s,int e,int c){
18      E[s].push_back((edge){e,c,E[e].size()});
19      E[e].push_back((edge){s,0,E[s].size()-1});
20 }
21 bool bfs()
22 {
23      memset(ch,-1,sizeof(ch));
24      queue <int> que;
25      que.push(1);
26      ch[1]=0;
27      while(que.size()!=0){
28                           int now=que.front();que.pop();
29                           for(int i=0;i<E[now].size();i++){
30                                   if(ch[E[now][i].to]==-1&&E[now][i].f>0){
31                                                            ch[E[now][i].to]=ch[now]+1;
32                                                            que.push(E[now][i].to);
33                                   }
34                           }
35      }
36      if(ch[M]==-1) return false;
37      return true;
38 }
39 int dfs(int now,int maxf)
40 {
41      if(now==M) return maxf;
42      if(maxf==0) return 0;
43      for(int i=0;i<E[now].size();i++){
44              if(ch[E[now][i].to]==ch[now]+1 && E[now][i].f>0){
45                                              int maxn=dfs(E[now][i].to,min(E[now][i].f,maxf));
46                                              if(maxn>0){
47                                                         E[now][i].f-=maxn;
48                                                         E[E[now][i].to][E[now][i].rev].f+=maxn;
49                                                         return maxn;
50                                                         }
51              }
52      }
53      return 0;
54 }
55                                                               
56                                   
57                                                               
58 int main()
59 {
60     int f,s,e,c;
61     scanf("%d%d",&N,&M);
62     for(int i=0;i<N;i++){
63             scanf("%d%d%d",&s,&e,&c);
64             add_edge(s,e,c);
65             
66     }
67     while(bfs()){
68                  while((f=dfs(1,INF))>0){
69                            ans+=f;
70                  }
71     }
72     cout<<ans<<endl;
73     
74     return 0;
75 }

 

USACO草地排水-网络流dinic模板

标签:logs   code   scan   back   网络流   set   ++   标准   scanf   

原文地址:http://www.cnblogs.com/rdzrdz-acm/p/6536987.html

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