问题描述:
最大流问题是网络流中的基本问题,是基于有向图的 可以用于解决最小割,二分图匹配等问题。
例题:
Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29214 Accepted Submission(s): 13686
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
本质:
最大流问题就是求二点之间的最大流速
想要遵守的原则:
1)流量守恒
2)反对称性
3)容量限制
方法总结:
1)增广路 Edmonds-Karp Dinic算法
2)预流推进算法 ISAP 算法
Edmonds-Karp算法:
先附上例题:https://blog.youkuaiyun.com/m0_57006708/article/details/120441920
思路:
用BFS来计算增广路径 直到无法搜索到增广路径时候返回
残留网络的利用
每次BFS完 找到最大流时候
从t到s进行回溯 对正向流-取最大流 反向流-最大流
/*
*@Author: GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r)/2
#define mms(x, y) memset(x, y, sizeof x)
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
// return a>b?a:b;
//}
//inline double min(double a,double b){
// return a<b?a:b;
//}
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
//start
int n,m;
int g[10010][10010];
int pre[10010];
int bfs(int s,int t){
int flow[10010];
mms(pre,-1);
flow[s]=INF; //初始化起点
pre[s]=0;
queue<int>q;
q.push(s);
while (!q.empty()) {
int u=q.front();
q.pop();
if(u==t) break; //到达终点退出
for(int i=1;i<=m;i++){ //BFS所有的点
if(i!=s&&g[u][i]>0&&pre[i]==-1){
pre[i]=u; //记录途径
q.push(i);
flow[i]=min(g[u][i],flow[u]); //更新结点流量
}
}
}
if(pre[t]==-1) return -1; //没有找到增广路
return flow[t];//返回该路的流量
}
int maxflow(int s,int t){
int MaxFlow=0;
while (1) {
int flow=bfs(s,t); //搜索最大流
if(flow==-1) break; //没有找到新的增广路 退出
int cur=t; //终点开始更新
while (cur!=s) {
int father=pre[cur]; //更新残留网络
g[father][cur]-=flow; //正向+反向-
g[cur][father]+=flow;
cur=father;
}
MaxFlow+=flow;
}
return MaxFlow;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
int a,b,c;
cin>>a>>b>>c;
g[a][b]+=c; //可能有重边
}
cout<<maxflow(1,m);
}
//end
https://acm.hdu.edu.cn/showproblem.php?pid=1532
2518



被折叠的 条评论
为什么被折叠?



