传送门:Drainage Ditches
题意:给出n个河流,m个点,以及每个河流的流量,求从1到m点的最大流量。
分析:网络流入门题,第一次写按照白书上毫无优化的Ford_fulkerson算法,先练练手感,再去学习sap和dinic算法吧。
#pragma comment(linker,"/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <limits.h> #include <iostream> #include <algorithm> #include <queue> #include <cstdlib> #include <stack> #include <vector> #include <set> #include <map> #define LL long long #define mod 100000000 #define inf 0x3f3f3f3f #define eps 1e-6 #define N 300 #define FILL(a,b) (memset(a,b,sizeof(a))) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define PII pair<int,int> using namespace std; inline int read() { char ch=getchar(); int x=0; while(ch>'9'||ch<'0')ch=getchar(); while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();} return x; } int cap[N][N]; int flow[N][N]; int a[N],p[N]; int Ford_fulkerson(int s,int t) { queue<int>que; FILL(flow,0); int f=0,u,v; while(1) { FILL(a,0); a[s]=inf; que.push(s); while(!que.empty()) { u=que.front();que.pop(); for(v=1;v<=t;v++) { if(!a[v]&&flow[u][v]<cap[u][v]) { p[v]=u;que.push(v); a[v]=a[u]<cap[u][v]-flow[u][v]?a[u]:cap[u][v]-flow[u][v]; } } } if(a[t]==0)return f; for(int i=t;i!=s;i=p[i]) { flow[i][p[i]]-=a[t]; flow[p[i]][i]+=a[t]; } f+=a[t]; } } int main() { int n,m; while(scanf("%d%d",&n,&m)>0) { FILL(cap,0); for(int i=1;i<=n;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); cap[u][v]+=w; } printf("%d ",Ford_fulkerson(1,m)); } }