代码:
#include <iostream>
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> using namespace std; const int N = 109; const int INF = 0x3f3f3f3f; int g[N][N],gap[N],dist[N],pre[N],cur[N]; int n,m,st,en; int calc(){ int ret=0,aug=INF,u,v; memset(gap,0,sizeof(gap)); memset(dist,0,sizeof(dist)); for(int i=0; i<=n; i++)cur[i]=1; u=pre[st]=st; gap[0]=n; while(dist[st]<=n){ loop: for(v=cur[u]; v<=n; v++) if(g[u][v]>0 &&dist[u]==dist[v]+1) { cur[u]=v; aug=min(aug,g[u][v]); pre[v]=u; u=v; if(v==en){ ret+=aug; for(u=pre[u];v!=st;v=u,u=pre[u]){ g[u][v]-=aug; g[v][u]+=aug; } aug=INF; } goto loop; } int mind=n; for(v=1; v<=n; v++) if(g[u][v]>0 &&mind>dist[v]){ mind=dist[v]; cur[u]=v; } if(--gap[dist[u]]<=0)break; gap[dist[u]=mind+1]++; u=pre[u]; } return ret; } int main() { freopen("in.txt","r",stdin); int p,c; while(~scanf("%d%d%d%d",&n,&p,&c,&m)) { memset(g,0,sizeof(g)); int f,tt,d; char a[50]; for(int i=0;i<m;i++) { scanf("%s",a); sscanf(a,"(%d,%d)%d",&f,&tt,&d); g[f+2][tt+2]+=d; } for(int i=0;i<p;i++) { scanf("%s",a); sscanf(a,"(%d)%d",&f,&d); g[1][f+2]+=d; } for(int i=0;i<c;i++) { scanf("%s",a); sscanf(a,"(%d)%d",&f,&d); g[f+2][n+2]=d; } n+=2; st = 1,en = n; printf("%d\n",calc()); } return 0; }