网络流学习日记02
本期介绍网络流最大流的 dinic 与 isap。
顺带一提,更喜欢 isap,这个比 dinic 更难卡,几乎不会被卡。如果被卡了,就要使用 HLPP 了。HLPP 并不属于增广路算法,其时间复杂度略优于 isap,
dinic
dinic,我们上集说到了 EK,EK 在每一次 bfs 的过程只招一条边,着实是有点慢,能不能一次找很多边呢?
当然可以,我们一次搜索当前网络的所有增广路,这些增广路组合成残余网络的阻塞流。每次遍历,我们找到当前网络的阻塞流,加入答案。
那么怎么寻找阻塞流?一个起点,向下流动,直到终点,返回流量。这个过程很容易就联想到树上 dp。使用前序遍历,每次返回的值是最大流出流量,如果直接来到终点,直接返回流入流量。
// 最大流 dinic
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
const int N = 1e3+2e2+4;
long long graph[N][N];
bool vis[N][N];
vector<int> my_vector[N];
queue<int> my_queue;
int n, m, st, ed;
int lev[N];
bool bfs(){
for(int i=1; i<=n; i++) lev[i] = 0;
my_queue.push(st); lev[st] = 1;
while(!my_queue.empty()){
int pos = my_queue.front(); my_queue.pop();
for(int &i:my_vector[pos]){
if(graph[pos][i] && !lev[i]){
lev[i] = lev[pos] + 1;
my_queue.push(i);
}
}
}
if(lev[ed]) return true;
return false;
}
long long dfs(int pos, long long flow){
if(pos==ed) return flow;
long long ret = 0;
for(auto &i: my_vector[pos]){
if(graph[pos][i] && lev[i] == lev[pos]+1){
long long f = dfs(i, min(flow-ret, graph[pos][i]));
graph[pos][i] -= f;
graph[i][pos] += f;
ret += f;
if(ret == flow) return ret;
}
}
return ret;
}
void dinic(){
long long res=0;
while(bfs()){
res += dfs(st, 1e12);
}
cout<<res;
}
int main(){
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cin>>n>>m>>st>>ed;
for(int i=1; i<=m; i++){
int x, y, val; cin>>x>>y>>val;
if(!vis[x][y]){
my_vector[x].push_back(y); my_vector[y].push_back(x);
vis[x][y] = vis[y][x] = true;
}
graph[x][y] += val;
}
dinic();
return 0;
}
isap
在这个过程,我们需要哐哐跑 bfs。有没有办法只跑一遍 bfs 呢?聪明的没有事干的科学家很快就想到了办法。考虑反向 bfs(),在此过程如果一个点与终点的距离没有增广路了,说明这个点废了,直接动态更新距离直到重新找到增广路。
// ISAP
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
const int N = 1230;
long long graph[N][N];
vector<int> my_vector[N];
queue<int> my_queue;
int n, m, st, ed;
int dep[N], gap[N];
long long ans;
void bfs(){
my_queue.push(ed); dep[ed] = 1; gap[1] = 1;
while(!my_queue.empty()){
int pos = my_queue.front(); my_queue.pop();
for(auto &i:my_vector[pos]){
if(!dep[i]){
dep[i] = dep[pos]+1; gap[dep[i]]++;
my_queue.push(i);
}
}
}
}
long long dfs(int pos, long long flow){
if(pos == ed) {
ans += flow;
return flow;
}
long long ret = 0;
for(auto &i: my_vector[pos]){
if(graph[pos][i] && dep[pos]==dep[i]+1){
long long f = dfs(i, min(flow-ret, graph[pos][i]));
graph[pos][i] -= f; graph[i][pos] += f;
ret += f;
if(ret == flow) return ret;
}
}
gap[dep[pos]]--;
if(gap[dep[pos]]==0) dep[st] = n;
dep[pos]++; gap[dep[pos]]++;
return ret;
}
void isap(){
bfs();
while(dep[st]<=n) {
dfs(st, 1e12);
}
cout<<ans;
}
int main(){
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cin>>n>>m>>st>>ed;
for(int i=1; i<=m; i++){
int x, y, val; cin>>x>>y>>val;
if(!graph[x][y] && !graph[y][x]) my_vector[x].push_back(y), my_vector[y].push_back(x);
graph[x][y] += val;
}
isap();
return 0;
}
优化
vector 的优化有两种。一种是二维数组优化,在稠密图,
for(int i=1; i<=m; i++){
int x, y, val; cin>>x>>y>>val;
if(!vis[x][y]){
my_vector[x].push_back(y); my_vector[y].push_back(x);
vis[x][y] = vis[y][x] = true;
}
graph[x][y] += val;
}
另一种是一维数组优化,这种情况点通常很多
struct edge{int to, val, inv;};
vector<edge> my_vector[N];
for(int i=1; i<=2*n; i++){
int x, y; cin>>x>>y;
my_vector[0].push_back({i, 1, vec[i]});
my_vector[i].push_back({0, 0, vec[0]});
vec[0]++; vec[i]++;
}