之前学的时候就好一顿理解不了,现在又忘了。于是记一下。
无源汇
-
把每条边的容量设为 ———— 强制流 。
-
记录每个点的入出。如果入大于出,那么说明这个点要多往外流一点,原图无源点,所以多往外的这一部分流量用虚拟源点向他连边提供。反之亦然。
-
如果虚拟源点建的边满流,说明正好补上了
-
流到了对应的点上,这个点要往外流,补足了。
-
因为往外流出去了,所以其他边的上下界没有被打破,所以是正确的。
-
-
即从源点向汇点跑最大流,如果最大流等于新建的边的总容量,即存在可行流。
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define int long long
using namespace std;
inline int read(){
register int x = 0, f = 1; register char ch = getchar();
for(; ch > '9' || ch < '0'; ch = getchar()) if(ch == '-') f = -1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ '0');
return x * f;
}
const int Inf = 2147483647;
const int N = 3e4 + 10;
const int s = 3e4 + 3;
const int t = 3e4 + 4;
int n, m;
int ans;
int fl[N];
int low[N];
int head[N << 1], Next[N << 1], e[N << 1], idx = 1, f[N << 1];
inline void add(int a, int b, int c, int d){
e[++ idx] = b; Next[idx] = head[a]; head[a] = idx; f[idx] = d - c; low[idx] = c;
e[++ idx] = a; Next[idx] = head[b]; head[b] = idx; f[idx] = 0;
}
int d[N], q[N], cur[N], hh, tt;
bool bfs(){
tt = hh = 0;
memset(d, -1, sizeof d);
q[0] = s; d[s] = 0; cur[s] = head[s];
while(hh <= tt){
int tp = q[hh ++];
for(int i = head[tp]; i; i = Next[i]){
int j = e[i];
if(d[j] == -1 && f[i]){
d[j] = d[tp] + 1;
cur[j] = head[j];
if(j == t) return true;
q[++ tt] = j;
}
}
}
return false;
}
int dfs(int now, int limit){
if(now == t) return limit;
int flow = 0;
for(int i = cur[now]; i && flow < limit; i = Next[i]){
cur[now] = i;
int j = e[i];
if(d[j] == d[now] + 1 && f[i]){
int t = dfs(j, min(f[i], limit - flow));
if(!t) d[j] = -1;
f[i] -= t; f[i ^ 1] += t; flow += t;
}
}
return flow;
}
void dinic(){
int flow;
while(bfs()) while(flow = dfs(s, Inf)) ans += flow;
}
signed main(){
n = read(), m = read();
for(int i = 1; i <= m; ++ i){
int a = read(), b = read(), c = read(), d = read();
fl[a] -= c; fl[b] += c;
add(a, b, c, d);
}
int sum = 0;
for(int i = 1; i <= n; ++ i){
if(fl[i] < 0) add(i, t, fl[i], 0);
else add(s, i, 0, fl[i]), sum += fl[i];
}
dinic(); if(ans < sum) puts("NO");
else{
puts("YES");
for(int i = 2; i <= (m << 1) + 1; i += 2) printf("%lld\n", f[i ^ 1] + low[i]);
}
return 0;
}有源汇最大流
-
为了保证流量守恒,汇点向源点连边。
-
删去连的边,跑 dinic — 此时已经可行。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define int long long
using namespace std;
inline int read(){
register int x = 0, f = 1; register char ch = getchar();
for(; ch > '9' || ch < '0'; ch = getchar()) if(ch == '-') f = -1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ '0');
return x * f;
}
const int Inf = 2147483647;
const int N = 3e4 + 10;
int s = 3e4 + 3;
int t = 3e4 + 4;
int n, m, S, T;
int ans;
int fl[N];
int low[N];
int head[N << 1], Next[N << 1], e[N << 1], idx = 1, f[N << 1];
inline void add(int a, int b, int c, int d){
e[++ idx] = b; Next[idx] = head[a]; head[a] = idx; f[idx] = d - c; low[idx] = c;
e[++ idx] = a; Next[idx] = head[b]; head[b] = idx; f[idx] = 0;
}
int d[N], q[N], cur[N], hh, tt;
bool bfs(){
tt = hh = 0;
memset(d, -1, sizeof d);
q[0] = s; d[s] = 0; cur[s] = head[s];
while(hh <= tt){
int tp = q[hh ++];
for(int i = head[tp]; i; i = Next[i]){
int j = e[i];
if(d[j] == -1 && f[i]){
d[j] = d[tp] + 1;
cur[j] = head[j];
if(j == t) return true;
q[++ tt] = j;
}
}
}
return false;
}
int dfs(int now, int limit){
if(now == t) return limit;
int flow = 0;
for(int i = cur[now]; i && flow < limit; i = Next[i]){
cur[now] = i;
int j = e[i];
if(d[j] == d[now] + 1 && f[i]){
int t = dfs(j, min(f[i], limit - flow));
if(!t) d[j] = -1;
f[i] -= t; f[i ^ 1] += t; flow += t;
}
}
return flow;
}
void dinic(){
int flow; ans = 0;
while(bfs()) while(flow = dfs(s, Inf)) ans += flow;
}
signed main(){
n = read(), m = read(); S = read(); T = read();
for(int i = 1; i <= m; ++ i){
int a = read(), b = read(), c = read(), d = read();
fl[a] -= c; fl[b] += c;
add(a, b, c, d);
}
int sum = 0;
for(int i = 1; i <= n; ++ i){
if(fl[i] < 0) add(i, t, fl[i], 0);
else add(s, i, 0, fl[i]), sum += fl[i];
}
add(T, S, 0, Inf);
dinic();
if(ans < sum) puts("No Solution");
else{
int res = f[idx];
f[idx] = f[idx ^ 1] = 0;
s = S; t = T;
dinic();
printf("%lld\n", ans + res);
}
return 0;
}有源汇最小流
改为从 向 跑,res - ans 即可。