最小费用最大流 模板

 1 const int maxn=10001;
 2 struct Edge//
 3 {
 4     int from,to,cap,flow,cost;//出点,入点,容量,当前流量,费用(也就是权值)
 5     Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w){}
 6 };
 7 
 8 struct MCMF
 9 {
10     int n,m;
11     vector<Edge> edges;//保存表
12     vector<int> G[maxn];//保存邻接关系
13     int inq[maxn];//判断一个点是否在队列当中(SPFA算法当中要用)
14     int d[maxn];//起点到d[i]的最短路径保存值
15     int p[maxn];//用来记录路径,保存上一条弧
16     int a[maxn];//找到增广路径后的改进量
17 
18     void init(int n)//初始化
19     {
20         this->n=n;
21         for(int i=0;i<=n;i++)
22             G[i].clear();
23         edges.clear();
24     }
25 
26     void AddEdge(int from,int to,int cap,int cost)//添加边
27     {
28         edges.push_back(Edge(from,to,cap,0,cost));//正向
29         edges.push_back(Edge(to,from,0,0,-cost));//反向
30         m=edges.size();
31         G[from].push_back(m-2);//按照边的编号保存邻接关系
32         G[to].push_back(m-1);
33     }
34 
35     bool BellmanFord(int s,int t,int& flow,long long& cost)//最短路径算法
36     {
37         for(int i=0;i<=n;i++)
38             d[i]=INT_MAX;
39         memset(inq,0,sizeof(inq));
40         d[s]=0;
41         inq[s]=1;
42         p[s]=0;
43         a[s]=INT_MAX;
44 
45         queue<int> Q;
46         Q.push(s);
47         while(!Q.empty())
48         {
49             int u=Q.front();
50             Q.pop();
51             inq[u]=0;
52             for(int i=0;i<G[u].size();i++)
53             {
54                 Edge& e=edges[G[u][i]];
55                 if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)//寻找满足容量大于流量的可松弛边
56                 {
57                     d[e.to]=d[u]+e.cost;
58                     p[e.to]=G[u][i];
59                     a[e.to]=min(a[u],e.cap-e.flow);
60                     if(!inq[e.to])//是否在队列当中
61                     {
62                         Q.push(e.to);
63                         inq[e.to]=1;
64                     }
65                 }
66             }
67         }
68         if(d[t]==INT_MAX)//如果d[t]没有被更新,相当于没找到增广路径,则没有最大流也没有最小费用
69             return false;
70         flow+=a[t];//更新最大流
71         cost+=(long long )d[t]*(long long)a[t];//单位流量乘以单位路径长度用来计算消耗
72         for(int u=t;u!=s;u=edges[p[u]].from)//通过使用p[]保存的上一个边的值来对刚刚找到的增广路径上面的流量进行更新
73         {
74             edges[p[u]].flow+=a[t];//正向变更新
75             edges[p[u]^1].flow-=a[t];//反向变更新(用位运算实现的)
76         }
77         return true;
78     }
79 
80     int MincostMaxflow(int s,int t,long long& cost)//计算从s到t的最小消耗cost,返回最大流
81     {
82         int flow = 0;
83         cost=0;
84         while(BellmanFord(s,t,flow,cost));//不断寻找最短增广路径,直到找不到为止
85         return flow;
86     }
87 };

猜你喜欢

转载自www.cnblogs.com/romaLzhih/p/9567404.html