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

【模板】网络流模板

时间:2016-02-21 19:51:59      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

好吧我只会最大流

dinic

 1 int n,m,s,t,cnt=1;
 2 int last[1005];
 3 int h[1005],q[1005];
 4 struct edge {
 5     int next,v,to;
 6 } e[100005];
 7 void insert(int u,int v,int w) {
 8     e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;e[cnt].v=w;
 9     e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt;e[cnt].v=0;
10 }
11 bool bfs(int s,int t) {
12     int head=0,tail=1;
13     memset(h,-1,sizeof(h));
14     q[0]=s,h[0]=0;
15     while(head!=tail) {
16         int now=q[head];
17         head++;
18         for(int i=last[now]; i; i=e[i].next)
19             if(e[i].v&&h[e[i].to]==-1) {
20                 h[e[i].to]=h[now]+1;
21                 q[tail++]=e[i].to;
22             }
23     }
24     return h[t]!=-1;
25 }
26 int dfs(int x,int t,int f) {
27     if(x==t)
28         return f;
29     int w,used=0;
30     for(int i=last[x]; i; i=e[i].next)
31         if(h[e[i].to]==h[x]+1) {
32             w=dfs(e[i].to,t,min(f-used,e[i].v));
33             e[i].v-=w;
34             e[i^1].v+=w;
35             used+=w;
36             if(used==f)return f;
37         }
38     if(!used)h[x]=-1;
39     return used;
40 }
41 int dinic(int s,int t) {
42     int tmp=0;
43     while(bfs(s,t))
44         tmp+=dfs(s,t,inf);
45     return tmp;
46 }

 

 

RP++

【模板】网络流模板

标签:

原文地址:http://www.cnblogs.com/oierforever/p/5205341.html

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