problem
- 给定n个点,m条边的有向图
- 求源点s到汇点的最大流
solution
最大流模板,,不会看笔记吧。。。
codes
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
typedef long long LL;
const int maxn = 110, maxm = 5050<<1;
//Grape
int tot=1, head[maxn], Next[maxm], ver[maxm], edge[maxm];
void AddEdge(int x, int y, int z){
ver[++tot] = y; edge[tot] = z;
Next[tot] = head[x]; head[x] = tot;
ver[++tot] = x; edge[tot] = 0;
Next[tot] = head[y]; head[y] = tot;
}
//maxflow
int s, t;
queue<int>q;
LL dep[maxn];//到x最少需要经过的边数
bool bfs(){
memset(dep,0,sizeof(dep));
while(q.size())q.pop();
q.push(s); dep[s] = 1;
while(q.size()){
int x = q.front(); q.pop();
for(int i = head[x]; i; i = Next[i]){
if(edge[i] && !dep[ver[i]]){
q.push(ver[i]);
dep[ver[i]] = dep[x]+1;
if(ver[i] == t)return true;
}
}
}
return false;
}
int dinic(int x, int flow){
if(x == t)return flow;
int rest = flow;
for(int i = head[x]; i && rest; i = Next[i]){
if(edge[i] && dep[ver[i]]==dep[x]+1){
int k = dinic(ver[i], min(rest, edge[i]));
if(!k)dep[ver[i]] = 0;
edge[i] -= k;
edge[i^1] += k;
rest -= k;
}
}
return flow-rest;
}
LL Maxflow(){
LL maxflow = 0, flow;
while(bfs())
while(flow=dinic(s,1<<30))maxflow += flow;
return maxflow;
}
int n, m;
void input(){
cin>>n>>m>>s>>t;
for(int i = 1; i <= m; i++){
int x, y, z; cin>>x>>y>>z; AddEdge(x,y,z);
}
}
int main(){
ios::sync_with_stdio(false);
input();
cout<<Maxflow()<<'\n';
return 0;
}