HDU 1532 最大流入门

时间:2023-03-08 21:39:59

1、HDU 1532

最大流入门,n个n条边,求第1点到第m点的最大流。只用EK做了一下

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define F(i,a,b) for (int i=a;i<b;i++)
#define FF(i,a,b) for (int i=a;i<=b;i++)
#define mes(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
typedef long long ll;
const int N = 1e5+, M=; int n,m,cap[M][M],pre[M],res[M],flow[M][M];
int bfs()
{
mes(res,); mes(pre,);
queue<int >Q;
Q.push(); res[]=INF;
while(!Q.empty()) {
int u=Q.front(); Q.pop();
FF(i,,n) {
if(res[i]== && flow[u][i]<cap[u][i]) {
res[i]=min(res[u], cap[u][i]-flow[u][i]);
pre[i]=u;
Q.push(i);
}
}
}
return res[m];
}
int Max_Flow()
{
mes(flow,);
int ans=;
while(true) {
int res_t=bfs();
if(res_t==) return ans;
int pos=m;
while(pos!=) {
flow[pre[pos]][pos]+=res_t;
flow[pos][pre[pos]]-=res_t;
pos=pre[pos];
}
ans+=res_t;
}
}
int main()
{
while(cin>>n>>m) {
mes(cap,);
int u,v,c;
FF(i,,n) {
cin>>u>>v>>c;
cap[u][v]+=c;
}
cout<<Max_Flow()<<endl;
} return ;
} EK

EK