【模板】网络流模板

好吧我只会最大流

dinic

 2016.2.25:修改,原来那个有点问题,换了个stl_queue的广搜,改了几个人畜无害的变量名

 2016.2.25比上面那个晚些时候:司机把我虐成狗,poj3469教我做人,加了当前弧,还是没A掉,但是我对正确性有信心

 1 const int inf=0x7fffffff;
 2 struct edge{
 3     int to,cap,next;
 4 }e[M*4]; 
 5 int last[N],iter[N],ecnt=1;
 6 void insert(int from,int to,int cap){
 7     e[++ecnt]=(edge){to,cap,last[from]};last[from]=ecnt;
 8     e[++ecnt]=(edge){from,0,last[to]};last[to]=ecnt;
 9 }
10 int h[N];
11 bool bfs(int s,int t){
12     memset(h,-1,sizeof(h));
13     queue<int>q;
14     q.push(s);h[s]=0;
15     while(!q.empty()){
16         int c=q.front();q.pop(); 
17         for(int u=last[c];u;u=e[u].next){
18             if(e[u].cap>0&&h[e[u].to]==-1){
19                 h[e[u].to]=h[c]+1;
20                 q.push(e[u].to);
21             }
22         }
23     }
24     return h[t]!=-1;
25 }
26 int dfs(int x,int t,int f){
27     if(x==t)    return f;
28     int w,used=0;
29     for(int u=iter[x];u;u=e[u].next){
30         if(h[e[u].to]==h[x]+1){
31             w=dfs(e[u].to,t,min(f-used,e[u].cap));
32             e[u].cap-=w;
33             e[u^1].cap+=w;
34             if(e[u].cap)    iter[x]=u;
35             used+=w;
36             if(used==f)    return f; 
37         }
38     }
39     if(!used)h[x]=-1;
40     return used;
41 }
42 int dinic(int s,int t){
43     int flow=0;
44     while(bfs(s,t)){
45         for(int i=0;i<=t;i++)    iter[i]=last[i];
46         flow+=dfs(s,t,inf);
47     }
48     return flow; 
49 }
50 int m,n;

 

RP++

posted on 2016-02-21 18:32  抽屉的抽屉  阅读(235)  评论(0)    收藏  举报