## Luo Gu P3381 minimum cost maximum flow template

https://www.luogu.org/problem/P3381

### Title Description

If that, given a network diagram, and the source and sink, each side units is known maximum flow and traffic charges, which determine the maximum flow and minimum cost network in the case of maximum flow.

### Input Format

The first row contains four positive integers N, M, S, T, respectively, the number of points, there are the number of edges, number source, sink point number.

Next M lines contains four integer ui, vi, wi, fi, denotes the i th ui from there to the edge and arriving at VI, the right side of wi (i.e., the side of the maximum flow rate Wi), per unit flow rate fee fi.

### Output Format

One line containing two integers, followed by the maximum flow and minimum cost at maximum flow conditions.

Entry
```4 5 4 3
4 2 30 2
4 3 20 3
2 3 20 1
2 1 30 9
1 3 40 5```
Export
`50 280`
```. 1 #include <stdio.h>
2 #include < String .h>
. 3 #include <Queue>
. 4  #define MEM (A, B) Memset (A, B, the sizeof (A))
. 5  the using  namespace STD;
. 6  const  int = 1E5 + MAXN 10 ;
. 7  const  int INF = 0x3f3f3f3f ;
. 8
. 9  int n-, m, ST, ED; // count the number of edges source sink
10  int VIS [MAXN], DIS [MAXN], Flow [MAXN]; // source to the cost and the flow point i
. 11  int pre [MAXN]; // precursor for each point
12 is  int Last [MAXN]; // each point of a front edge connected
13 is  int minCost, MaxFlow;
14 Queue < int > Q;
15
16  struct Edge
. 17  {
18 is      int to, Next, Flow, DIS; // fee as dis to the shortest run
. 19  } edge [MAXN];
20 is  int head [MAXN], CNT;
21 is
22 is  void the Add ( int A, int B, int C, int D) // use ^ to zero from the operating side so
23  {
24     Edge [CNT ++] .to = B;
25      Edge [CNT] = .next head [A];
26 is      Edge [CNT] .flow = C;
27      Edge [CNT] .dis = D;
29  }
30
31 is  BOOL SPFA ( int ST, int ED)
32  {
33 is      MEM (DIS, INF), MEM (flow, INF), MEM (VIS, 0 ); // find the maximum flow and the shortest (least cost )
34 is      Q.push (ST);
35      VIS [ST] = . 1 ;
36      DIS [ST] = 0 ;
37 [     pre[ed] = -1;
38     while(!Q.empty())
39     {
40         int now = Q.front();
41         Q.pop();
42         vis[now] = 0;
43         for(int i = head[now]; i != -1; i = edge[i].next)
44         {
45             if(edge[i].flow > 0 && dis[edge[i].to] > dis[now] + edge[i].dis)
46             {
47                 dis[edge[i].to] = dis[now] + edge[i].dis;
48                 pre [Edge [I] .to] = now; // new record updated after the precursor
49                  Last [Edge [I] .to] = I; // after recording updated before a new edge to the back edge to update flow rate
50                  flow [Edge [I] .to] = min (flow [now], Edge [I] .flow); // find the maximum flow
51 is                  IF (! VIS [Edge [I] .to])
52 is                  {
53 is                      VIS [Edge [I] .to] = . 1 ;
54 is                      Q.push (Edge [I] .to);
55                  }
56 is              }
57 is          }
58      }
59      return ! pre [ED] = - . 1;
60 }
61
62 void min_cost_max_flow()
63 {
64     while(spfa(st, ed))
65     {
66         int now = ed;
67         maxflow += flow[ed];
68         mincost += flow[ed] * dis[ed];
69         while(now != st)//从汇点回溯更新边剩下的流量
70         {
71             edge[last[now]].flow -= flow[ed];
72             edge[last[now] ^ 1].flow += flow[ed];
73             now = pre[now];
74         }
75     }
76 }
77
78 int main()
79 {
80     mem(head, -1), cnt = -1;
81     scanf("%d%d%d%d", &n, &m, &st, &ed);
82     for(int i = 1; i <= m; i ++)
83     {
84         int a, b, c, d;
85         scanf("%d%d%d%d", &a, &b, &c, &d); //Capacity to start and end points have unit cost edges
86          the Add (A, B, C, D);
87          the Add (B, A, 0 , -d); // reverse side flow is zero, it takes a negative
88      }
89      min_cost_max_flow ();
90      the printf ( " % D% D \ n- " , MaxFlow, minCost);
91 is      return  0 ;
92 }```
The minimum cost maximum flow template

### Guess you like

Origin www.cnblogs.com/yuanweidao/p/11246839.html
Recommended
Ranking
Daily