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

Spfa费用流模板

时间:2016-02-27 12:30:54      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <queue>
 5 using namespace std;
 6 const int INF=233333333;
 7 const int maxn=1010,maxm=40010;
 8 int cnt,fir[maxn],nxt[maxm],to[maxm],cap[maxm],val[maxm],dis[maxn],path[maxn];
 9 
10 void addedge(int a,int b,int c,int v)
11 {
12     nxt[++cnt]=fir[a];to[cnt]=b;cap[cnt]=c;val[cnt]=v;fir[a]=cnt;
13 }
14 int S,T;
15 int Spfa()
16 {
17     queue<int>q;
18     memset(dis,127,sizeof(dis));
19     q.push(S);dis[S]=0;
20     while(!q.empty())
21     {
22         int node=q.front();q.pop();
23         for(int i=fir[node];i;i=nxt[i])
24             if(cap[i]&&dis[node]+val[i]<dis[to[i]]){
25                 dis[to[i]]=val[i]+dis[node];
26                 path[to[i]]=i;
27                 q.push(to[i]);
28             }
29     }
30     return dis[T]==dis[T+1]?0:dis[T]; 
31 }
32 
33 int Aug()
34 {
35     int p=T,f=INF;
36     while(p!=S)
37     {
38         f=min(f,cap[path[p]]);
39         p=to[path[p]^1];
40     }
41     p=T;
42     while(p!=S)
43     {
44         cap[path[p]]-=f;
45         cap[path[p]^1]+=f;
46         p=to[path[p]^1];
47     }
48     return f;
49 }
50 
51 int MCMF()
52 {
53     int ret=0,d;
54     while(d=Spfa())
55         ret+=Aug()*d;
56     return ret;    
57 }
58 
59 void Init(int n)
60 {
61     cnt=1;S=0;T=n+1;
62     for(int i=1;i<=n;i++)fir[i]=0;
63 }
64 
65 int main()
66 {
67     int n,m;
68     while(~scanf("%d%d",&n,&m))
69     {
70         Init(n);
71         int a,b,v;
72         for(int i=1;i<=m;i++)
73         {
74             scanf("%d%d%d",&a,&b,&v);
75             addedge(a,b,1,v);
76             addedge(b,a,0,-v);
77             addedge(b,a,1,v);
78             addedge(a,b,0,-v);
79         }
80         addedge(S,1,2,0);
81         addedge(1,S,0,0);
82         addedge(n,T,2,0);
83         addedge(T,n,0,0);
84         printf("%d\n",MCMF());
85     }
86     return 0;
87 }

 

Spfa费用流模板

标签:

原文地址:http://www.cnblogs.com/TenderRun/p/5222617.html

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