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+10, M=200; int n,m,cap[M][M],pre[M],res[M],flow[M][M]; int bfs() { mes(res,0); mes(pre,0); queue<int >Q; Q.push(1); res[1]=INF; while(!Q.empty()) { int u=Q.front(); Q.pop(); FF(i,1,n) { if(res[i]==0 && 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,0); int ans=0; while(true) { int res_t=bfs(); if(res_t==0) return ans; int pos=m; while(pos!=1) { 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,0); int u,v,c; FF(i,1,n) { cin>>u>>v>>c; cap[u][v]+=c; } cout<<Max_Flow()<<endl; } return 0; } EK