-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPFA.cpp
87 lines (78 loc) · 2.21 KB
/
SPFA.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Author: NogiNonoka
Date: 2021.1.12
Time Complexity: O(V^5)
Usage: */
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 205;
const int MAXE = 1007; // NO DOUBLE
const int INF = 0x3f3f3f3f;
struct SPFA {
struct Edge {
int v, cap, cost, nxt;
} edge[MAXE << 1]; // DOUBLE HERE
int cntedge, sumflow;
int head[MAXN];
void Init() {
cntedge = 0;
memset(head, 0xff, sizeof(head));
}
void Addedge(int u, int v, int cap,
int cost) { // SO, in main(), only add once.
edge[cntedge].v = v;
edge[cntedge].cap = cap;
edge[cntedge].cost = cost;
edge[cntedge].nxt = head[u];
head[u] = cntedge++;
edge[cntedge].v = u;
edge[cntedge].cap = 0;
edge[cntedge].cost = -cost;
edge[cntedge].nxt = head[v];
head[v] = cntedge++;
}
int dis[MAXN], pre[MAXN];
bool vis[MAXN];
bool Spfa(int s, int t, int n) {
int u, v;
queue<int> q;
memset(vis, 0, sizeof(vis));
memset(pre, 0xff, sizeof(pre));
for (int i = 0; i <= n; i++)
dis[i] = INF;
vis[s] = true;
dis[s] = 0;
q.push(s);
while (!q.empty()) {
u = q.front();
q.pop();
vis[u] = false;
for (int i = head[u]; i != -1; i = edge[i].nxt) {
v = edge[i].v;
if (edge[i].cap && dis[v] > dis[u] + edge[i].cost) {
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if (!vis[v]) {
q.push(v);
vis[v] = true;
}
}
}
}
return dis[t] != INF;
}
int MinCostMaxFlow(int s, int t, int n) {
int flow = 0;
int minflow, mincost;
mincost = 0;
while (Spfa(s, t, n)) {
minflow = INF + 1;
for (int i = pre[t]; i != 1; i = pre[edge[i ^ 1].v]) {
edge[i].cap -= minflow;
edge[i ^ 1].cap += minflow;
}
mincost += dis[t] * minflow;
}
sumflow = flow;
return mincost;
}
} mcmf;