一道裸的网络流,我用的是Edmonds_Karp算法,中间居然在一个很SB的地方上老实WA,找了好久才意识到其实是变量名弄错了QAQ……
#include <stdio.h> #include<stdlib.h> #include<queue> #include<string.h> #define INF 1e8 using std::queue; int m,n; int flow[201][201]={0},cap[201][201]={0},a[201],p[201]; int Edmonds_Karp() { queue<int>q; memset(flow,0,sizeof(flow)); memset(p,0,sizeof(p)); int f=0; for(;;) { memset(a,0,sizeof(a)); a[1]=INF; q.push(1); while(!q.empty()) { int u=q.front(); q.pop(); for(int v=1;v<=m;v++) if(!a[v]&&cap[u][v]>flow[u][v]) { p[v]=u; q.push(v); if(a[u]>cap[u][v]-flow[u][v]) a[v]=cap[u][v]-flow[u][v]; else a[v]=a[u]; } } if(a[m]==0)break; for(int u=m;u!=1;u=p[u]) { flow[p[u]][u]+=a[m]; flow[u][p[u]]-=a[m]; } f+=a[m]; } return f; } int main() { int i,j,k; while(scanf("%d%d",&n,&m)!=EOF) { memset(cap,0,sizeof(cap)); for(i=1;i<=n;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); cap[a][b]+=c; } int f=Edmonds_Karp(); printf("%d\n",f); } return 0; }