【学习笔记】图论
WilliamAlbert · · 个人记录
最短路
Johnson 全源最短路
求两两之间的最短路,有负权。
1 \le n \le 3000, 1 \le m \le 6000 。
用 floyd 是可以
首先建立一个超级源点向所有点连边,边权
网络流
最大流
:::success[Max-Flow]
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 1e4 + 5, M = 1e5 + 5, INF = 2e9;
int h[N], e[M * 2], ne[M * 2], f[M * 2], idx;
int n, m, S, T;
void add(int a, int b, int c) {
e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx++;
e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx++;
}
int d[N], cur[N];
bool bfs() {
For(i, 0, n + 1) d[i] = -1, cur[i] = -1;
queue<int> q;
q.push(S);
d[S] = 0;
cur[S] = h[S];
while (q.size()) {
int t = q.front();
q.pop();
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
if (f[i] && d[j] == -1) {
d[j] = d[t] + 1;
cur[j] = h[j];
if (j == T) return 1;
q.push(j);
}
}
}
return 0;
}
int find(int x, int limit) {
if (x == T) return limit;
int flow = 0;
for (int i = cur[x]; (~i) && flow < limit; i = ne[i]) {
cur[x] = i;
int j = e[i];
if (f[i] && d[j] == d[x] + 1) {
int t = find(j, min(limit - flow, f[i]));
if (!t) d[j] = -1;
flow += t;
f[i] -= t, f[i ^ 1] += t;
}
}
return flow;
}
int dinic() {
int flow = 0, t = 0;
while (bfs()) {
while (flow = find(S, INF)) t += flow;
}
return t;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
memset(h, -1, sizeof(h));
idx = 0;
cin >> n >> m >> S >> T;
For(i, 1, m) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
cout << dinic() << '\n';
return 0;
}
:::
最小割
最大流 = 最小割
费用流
EK 费用流
:::success[MaxCostFlow]
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 5005, M = 50005, INF = 2e9;
int h[N], e[M * 2], ne[M * 2], f[M * 2], w[M * 2], idx;
int n, m, S, T;
void add(int a, int b, int c, int d) {
e[idx] = b, f[idx] = c, w[idx] = d, ne[idx] = h[a], h[a] = idx++;
e[idx] = a, f[idx] = 0, w[idx] = -d, ne[idx] = h[b], h[b] = idx++;
}
int pre[N], incf[N], dist[N];
bool vis[N];
bool spfa() {
For(i, 0, n + 1) incf[i] = 0, dist[i] = INF, vis[i] = 0;
queue<int> q;
q.push(S);
vis[S] = 1;
incf[S] = INF, dist[S] = 0;
while (q.size()) {
int t = q.front();
q.pop();
vis[t] = 0;
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
if (f[i] && dist[j] > dist[t] + w[i]) {
dist[j] = dist[t] + w[i];
pre[j] = i;
incf[j] = min(incf[t], f[i]);
if (!vis[j]) {
vis[j] = 1;
q.push(j);
}
}
}
}
return incf[T] > 0;
}
void EK(int &flow, int &cost) {
while (spfa()) {
flow += incf[T], cost += incf[T] * dist[T];
for (int i = T; i != S; i = e[pre[i] ^ 1]) {
f[pre[i]] -= incf[T];
f[pre[i] ^ 1] += incf[T];
}
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
memset(h, -1, sizeof(h));
idx = 0;
cin >> n >> m >> S >> T;
For(i, 1, m) {
int a, b, c, d;
cin >> a >> b >> c >> d;
add(a, b, c, d);
}
int flow = 0, cost = 0;
EK(flow, cost);
cout << flow << ' ' << cost;
return 0;
}
:::
Prime-Dual 费用流(原始对偶)
考虑先求一遍 spfa,源点为
:::success[Prime-Dual]
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 5e3 + 5, M = 50005, INF = 1e8;
int n, m, S, T;
int h[N], e[M * 2], ne[M * 2], f[M * 2], w[M * 2], idx;
void add(int a, int b, int c, int d) {
e[idx] = b, f[idx] = c, w[idx] = d, ne[idx] = h[a], h[a] = idx++;
e[idx] = a, f[idx] = 0, w[idx] = -d, ne[idx] = h[b], h[b] = idx++;
}
int d[N];
bool vis[N];
void spfa() {
For(i, 1, n) d[i] = INF;
queue<int> q;
q.push(S);
vis[S] = 1, d[S] = 0;
while (q.size()) {
int t = q.front();
q.pop();
vis[t] = 0;
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
if (f[i] && d[j] > d[t] + w[i]) {
d[j] = d[t] + w[i];
if (!vis[j]) vis[j] = 1, q.push(j);
}
}
}
}
int dist[N], pre[N], incf[N];
bool dijkstra() {
For(i, 1, n) vis[i] = 0, dist[i] = INF, incf[i] = 0;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
q.push(mp(0, S));
incf[S] = INF;
dist[S] = 0;
while (q.size()) {
auto t = q.top();
q.pop();
if (vis[t.y]) continue;
vis[t.y] = 1;
for (int i = h[t.y]; ~i; i = ne[i]) {
if (vis[e[i]]) continue;
int j = e[i], W = w[i] + d[t.y] - d[j];
if (f[i] && dist[j] > dist[t.y] + W) {
dist[j] = dist[t.y] + W;
pre[j] = i;
incf[j] = min(incf[t.y], f[i]);
q.push(mp(dist[j], j));
}
}
}
return incf[T];
}
void EK(int &flow, int &cost) {
spfa();
while (dijkstra()) {
For(i, 1, n) d[i] = min(INF, d[i] + dist[i]);
for (int i = T; i != S; i = e[pre[i] ^ 1]) {
f[pre[i]] -= incf[T];
f[pre[i] ^ 1] += incf[T];
}
flow += incf[T], cost += incf[T] * d[T];
}
}
signed main() {
//assert(freopen(".in", "r", stdin));
//assert(freopen(".out", "w", stdout));
cin.tie(nullptr)->sync_with_stdio(false);
memset(h, -1, sizeof(h));
cin >> n >> m >> S >> T;
For(i, 1, m) {
int u, v, c, w;
cin >> u >> v >> c >> w;
add(u, v, c, w);
}
int flow = 0, cost = 0;
EK(flow, cost);
cout << flow << ' ' << cost;
return 0;
}
:::
Dinic 费用流(zkw 费用流)
就是每一次跑 spfa,然后 dfs 找到所有增广路。
:::success[Dinic] 咕噜咕噜 :::
上下界网络流
无源汇上下界可行流
给定一个无源汇网络,每个边都有流量下界和上界,输出一种方案,若无输出
-1。
考虑将下界删掉,对于
输出方案就是对于此时每条边加上
:::success[无源汇上下界可行流]
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 205, M = 10205 + N, INF = 2e9;
int d[N], cur[N];
int n, m, S, T;
int sum[N];
int h[N], e[M * 2], ne[M * 2], f[M * 2], l[M * 2], idx;
void add(int a, int b, int c, int d) {
e[idx] = b, f[idx] = d - c, l[idx] = c, ne[idx] = h[a], h[a] = idx++;
e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx++;
}
bool bfs() {
For(i, 0, n + 1) d[i] = -1;
queue<int> q;
q.push(S);
d[S] = 0, cur[S] = h[S];
while (q.size()) {
int t = q.front();
q.pop();
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
if (d[j] == -1 && f[i]) {
d[j] = d[t] + 1;
cur[j] = h[j];
if (j == T) return 1;
q.push(j);
}
}
}
return 0;
}
int find(int x, int limit) {
if (x == T) return limit;
int flow = 0;
for (int i = cur[x]; ~i && flow < limit; i = ne[i]) {
int j = e[i];
cur[x] = i;
if (d[j] == d[x] + 1 && f[i]) {
int t = find(j, min(limit - flow, f[i]));
if (t == 0) d[j] = -1;
f[i] -= t, f[i ^ 1] += t, flow += t;
}
}
return flow;
}
int dinic() {
int flow = 0, t = 0;
while (bfs()) while (flow = find(S, INF)) t += flow;
return t;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
memset(h, -1, sizeof(h));
cin >> n >> m;
For(i, 1, m) {
int a, b, c, d;
cin >> a >> b >> c >> d;
add(a, b, c, d);
sum[a] -= c;
sum[b] += c;
}
S = 0, T = n + 1;
int tot = 0;
For(i, 1, n) {
if (sum[i] >= 0) add(S, i, 0, sum[i]), tot += sum[i];
else add(i, T, 0, -sum[i]);
}
if (dinic() != tot) cout << "NO";
else {
cout << "YES\n";
for (int i = 0; i < 2 * m; i += 2) cout << f[i ^ 1] + l[i] << '\n';
}
return 0;
}
:::
有源汇上下界最大/最小流
和无源汇的一样,只是此时要新添一条
最大流
先从
当然还有一种比较简单巧妙的方法,第一次跑完后不删边,而是直接跑
:::success[有源汇上下界最大流]
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 205, M = 10005 + N, INF = 2e9;
int h[N], e[M * 2], ne[M * 2], f[M * 2], idx;
int n, m, s, t, S, T;
int d[N], cur[N];
int sum[N], tot;
void add(int a, int b, int c) {
e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx++;
e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx++;
}
bool bfs() {
For(i, 0, n + 1) d[i] = -1;
queue<int> q;
d[S] = 0, cur[S] = h[S];
q.push(S);
while (q.size()) {
int t = q.front();
q.pop();
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
if (d[j] == -1 && f[i]) {
d[j] = d[t] + 1;
cur[j] = h[j];
if (j == T) return 1;
q.push(j);
}
}
}
return 0;
}
int find(int x, int limit) {
if (x == T) return limit;
int flow = 0;
for (int i = cur[x]; ~i && flow < limit; i = ne[i]) {
int j = e[i];
cur[x] = i;
if (d[j] == d[x] + 1 && f[i]) {
int t = find(j, min(limit - flow, f[i]));
if (t == 0) d[j] = -1;
f[i] -= t, f[i ^ 1] += t, flow += t;
}
}
return flow;
}
int dinic() {
int t = 0, flow = 0;
while (bfs()) while (flow = find(S, INF)) t += flow;
return t;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
memset(h, -1, sizeof(h));
cin >> n >> m >> s >> t;
For(i, 1, m) {
int a, b, c, d;
cin >> a >> b >> c >> d;
add(a, b, d - c);
sum[a] -= c;
sum[b] += c;
}
S = 0, T = n + 1;
For(i, 1, n) {
if (sum[i] >= 0) add(S, i, sum[i]), tot += sum[i];
else add(i, T, -sum[i]);
}
add(t, s, INF);
if (dinic() != tot) cout << "No Solution";
else {
int res = f[idx - 1];
f[idx - 1] = f[idx - 2] = 0;
S = s, T = t;
res += dinic();
cout << res << '\n';
}
return 0;
}
:::
最小流
同最大流一样,但第二次变成求从
并且注意,不能用上面第二种方式了。
:::success[有源汇上下界最小流]
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 5e4 + 10, M = 125000 + 10 + N + 10, INF = 2147483647;
int n, m, s, t;
int S, T;
int h[N], e[M * 2], ne[M * 2], f[M * 2], idx;
int sum[N];
void add(int a, int b, int c) {
e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx++;
e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx++;
}
int cur[N], d[N];
bool bfs() {
For(i, 0, n + 1) d[i] = -1;
queue<int> q;
q.push(S);
d[S] = 0, cur[S] = h[S];
while (q.size()) {
int t = q.front();
q.pop();
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
if (d[j] == -1 && f[i]) {
d[j] = d[t] + 1;
cur[j] = h[j];
if (j == T) return 1;
q.push(j);
}
}
}
return 0;
}
int find(int x, int limit) {
if (x == T) return limit;
int flow = 0;
for (int i = cur[x]; ~i && flow < limit; i = ne[i]) {
int j = e[i];
cur[x] = i;
if (d[x] + 1 == d[j] && f[i]) {
int t = find(j, min(f[i], limit - flow));
if (!t) d[j] = -1;
flow += t, f[i] -= t, f[i ^ 1] += t;
}
}
return flow;
}
int dinic() {
int t = 0, flow = 0;
while (bfs()) while (flow = find(S, INF)) t += flow;
return t;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
memset(h, -1, sizeof(h));
cin >> n >> m >> s >> t;
For(i, 1, m) {
int a, b, c, d;
cin >> a >> b >> c >> d;
add(a, b, d - c);
sum[a] -= c;
sum[b] += c;
}
S = 0, T = n + 1;
int tot = 0;
For(i, 1, n) {
if (sum[i] >= 0) add(S, i, sum[i]), tot += sum[i];
else add(i, T, -sum[i]);
}
add(t, s, INF);
if (dinic() != tot) cout << "No Solution";
else {
int res = f[idx - 1];
f[idx - 1] = f[idx - 2] = 0;
S = t, T = s;
cout << res - dinic();
}
return 0;
}
:::
无源汇上下界最小/最大费用可行流
其实就是直接改成 spfa 就行。
有源汇上下界最小/最大费用可行流
这里就只用跑第一遍
有源汇上下界最小/最大费用最大/最小流
最大流
考虑第一遍
最小流
考虑第一遍
带负权的费用流
我们发现,若一个网络中存在负环,但并不意味着没有最小费用流。
考虑和上下界的处理方式一样,我们强制那些负权边
:::success[带负权的费用流]
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 205, M = 1e4 + 5, K = M + N, INF = 2e9;
int n, m, s, t, S, T;
int in[N], out[N];
int h[N], e[K * 2], ne[K * 2], f[K * 2], w[K * 2], idx;
void add(int a, int b, int c, int d) {
e[idx] = b, f[idx] = c, w[idx] = d, ne[idx] = h[a], h[a] = idx++;
e[idx] = a, f[idx] = 0, w[idx] = -d, ne[idx] = h[b], h[b] = idx++;
}
int dist[N], pre[N], incf[N];
bool vis[N];
bool spfa() {
memset(vis, 0, sizeof(vis));
memset(dist, 0x3f, sizeof(dist));
memset(incf, 0, sizeof(incf));
incf[S] = INF;
dist[S] = 0;
queue<int> q;
q.push(S);
vis[S] = 1;
while (q.size()) {
int t = q.front();
q.pop();
vis[t] = 0;
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
if (f[i] && dist[j] > dist[t] + w[i]) {
dist[j] = dist[t] + w[i];
pre[j] = i;
incf[j] = min(incf[t], f[i]);
if (!vis[j]) q.push(j), vis[j] = 1;
}
}
}
return incf[T] > 0;
}
void EK(int &flow, int &cost) {
while (spfa()) {
flow += incf[T];
cost += incf[T] * dist[T];
for (int i = T; i != S; i = e[pre[i] ^ 1]) {
f[pre[i]] -= incf[T];
f[pre[i] ^ 1] += incf[T];
}
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
memset(h, -1, sizeof(h));
cin >> n >> m >> s >> t;
int ans = 0;
For(i, 1, m) {
int x, y, f, v;
cin >> x >> y >> f >> v;
if (v < 0) {
in[y] += f, out[x] += f;
ans += f * v;
add(y, x, f, -v);
} else add(x, y, f, v);
}
S = 0, T = n + 1;
For(i, 1, n) {
if (in[i] > out[i]) add(S, i, in[i] - out[i], 0);
else add(i, T, out[i] - in[i], 0);
}
add(t, s, INF, 0);
int flow1 = 0, cost1 = 0;
EK(flow1, cost1);
flow1 = f[idx - 1];
f[idx - 1] = f[idx - 2] = 0;
int flow2 = 0, cost2 = 0;
S = s, T = t;
EK(flow2, cost2);
cout << flow1 + flow2 << ' ' << cost1 + cost2 + ans;
return 0;
}
:::
关于图论一些定理
Hall 定理
:::info[Hall 定理]{open}
给定一个二分图,设左边点集为
:::info[推论 1]{open} 对于一个正则二分图(所有点的度数相同的二分图),一定存在完美匹配。 :::
:::info[推论 2]{open} 对于一个二分图,其最大匹配为:
:::
我们证明一下 推论 2,那么 Hall 定理 也就自然成立。
这里有两种方法。
:::success[Method 1(最大流最小割定理)]
考虑对于一个集合
然后我们将其变为流:
| 那么最大匹配数就一定是这个图的最大流。根据 最大流最小割定理 ,我们得出这里的最小割要么为 |
|---|