Minimum cost maximum flow (luogu P3381 [template] minimum cost maximum flow)

topic link

Topic description

For example, given a network graph, as well as its source and sink points, each edge knows its maximum flow and unit flow cost, and finds the maximum flow of its network and the minimum cost in the case of maximum flow.

Input and output format

Input format:

 

The first line contains four positive integers N, M, S, and T, which represent the number of points, the number of directed edges, the sequence number of the source node, and the sequence number of the sink node, respectively.

The next M lines each contain four positive integers ui, vi, wi, fi, indicating that the i-th directed edge starts from ui and arrives at vi, and the edge weight is wi (that is, the maximum flow of this edge is wi), and the unit flow is wi. The fee is fi.

 

Output format:

 

A line containing two integers, followed by the maximum flow and the minimum cost in the case of maximum flow.

 

Input and output example

Input example #1: 
4 5 4 3
4 2 30 2
4 3 20 3
2 3 20 1
2 1 30 9
1 3 40 5
Sample output #1: 
50 280

 

Expense Flow Template:

Under the premise of maximum flow, the cost is minimum. Extended by the EK algorithm. Every time EK uses bfs to augment, change bfs to spfa to find a path with the least cost, and then use this path to optimize the answer.

Those who don't know EK first go to get EK and write the maximum flow, and those who don't know SPFA take SPFA and write the shortest path and look down.

 

Then, when building an edge, the cost of the reverse arc is the opposite of the forward arc. Taking the reverse arc is equivalent to not taking this section of the edge. Of course, the shortest path part must eliminate the influence of this section.

 

AC code

 

 1 #include <bits/stdc++.h>
 2 
 3 
 4 using namespace std;
 5 const int MAXN = 5010;
 6 const int MAXM = 50010;
 7 const int INF = 0x7FFFFFFF;
 8 
 9 int n, m, first[MAXN], s, t, sign;
10 
11 int max_flow, min_cost;
12 
13 struct Edge {
14     int to, cap, cost, next;
15 } edge[MAXM * 2];
16 
17 inline void init() {
18     for(int i = 0; i <= n; i++ ) {
19         first[i] = -1;
20     }
21     sign = 0;
22 }
23 
24 inline void add_edge(int u, int v, int cap, int cost) {
25     edge[sign].to = v, edge[sign].cap = cap, edge[sign].cost = cost;
26     edge[sign].next = first[u], first[u] = sign ++;
27     edge[sign].to = u, edge[sign].cap = 0, edge[sign].cost = -cost;
28     edge[sign].next = first[v], first[v] = sign ++;
29 }
30 
31 int dist[MAXN], inq[MAXN], pre[MAXN], incf[MAXN];
32 
33 bool spfa(int s, int t) {
34     for(int i = 1; i <= n ; i++ ) {
35         dist[i] = INF, inq[i] = 0;
36     }
37     queue<int>que;
38     que.push(s), inq[s] = 1, dist[s] = 0;
39     incf[s] = 0x3f3f3f3f;
40     while(!que.empty()) {
41         int now = que.front();
42         que.pop();
43         inq[now] = 0;
44         for(int i = first[now]; ~i; i = edge[i].next) {
45             int to = edge[i].to, cap = edge[i].cap, cost = edge[i].cost;
46             if(cap > 0 && dist[to] > dist[now] + cost) {
47                 dist[to] = dist[now] + cost;
48                 incf[to] = min(incf[now], cap);
49                 pre[to] = i;
50                 if(!inq[to]) {
51                     que.push(to);
52                     inq[to] = 1;
53                 }
54             }
55         }
56     }
57     return dist[t] != INF;
58 }
59 
60 void update(int s, int t) {
61     int x = t;
62     while(x != s) {
63         int pos = pre[x];
64         edge[pos].cap -= incf[t];
65         edge[pos ^ 1].cap += incf[t];
66         x = edge[pos ^ 1].to;
67     }
68     max_flow += incf[t];
69     min_cost += dist[t] * incf[t];
70 }
71 
72 void minCostMaxFlow(int s, int t) {
73     while(spfa(s, t)) {
74         update(s, t);
75     }
76 }
77 
78 int main()
79 {
80     while(~scanf("%d %d %d %d", &n, &m, &s, &t)) {
81         init();
82         for(int i = 1; i <= m; i++ ) {
83             int u, v, cap, cost;
84             scanf("%d %d %d %d", &u, &v, &cap, &cost);
85             add_edge(u, v, cap, cost);
86         }
87         max_flow = min_cost = 0;
88         minCostMaxFlow(s, t);
89         printf("%d %d\n", max_flow, min_cost);
90     }
91 
92     return 0;
93 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325030617&siteId=291194637