比较简单。
#include<stdio.h> #include<string.h> #include<queue> #define maxn 110 #define INF 99999999 using namespace std; int vis[maxn],n,map[maxn][maxn]; int min(int x,int y) {return x<y?x:y;} int dfs(int u,int low) { int i,a; if(u==n+1) return low; for(i=0;i<=n+1;i++) { if(vis[i]==vis[u]+1&&map[u][i]>0) { a=dfs(i,min(low,map[u][i])); if(!a)continue; map[u][i]-=a; map[i][u]+=a; return a; } } return 0; } bool BFS() { int i; queue<int>q; memset(vis,-1,sizeof(vis)); vis[0]=0; q.push(0); while(!q.empty()) { int t=q.front(); q.pop(); for(i=0;i<=n+1;i++) { if(vis[i]<0&&map[t][i]>0) { vis[i]=vis[t]+1; q.push(i); } } } if(vis[n+1]>0)return true; return false; } int main() { int i,nc,np,m; while(scanf("%d %d %d %d",&n,&np,&nc,&m)!=EOF) { memset(map,0,sizeof(map)); for(i=0;i<m;i++) { int x,y,z; while(getchar()!=‘(‘); scanf("%d,%d)%d",&x,&y,&z); map[x+1][y+1]+=z; } for(i=0;i<np;i++) { int x,y; while(getchar()!=‘(‘); scanf("%d)%d",&x,&y); map[0][x+1]=y; } for(i=0;i<nc;i++) { int x,y; while(getchar()!=‘(‘); scanf("%d)%d",&x,&y); map[x+1][n+1]=y; } int ans=0; while(BFS()) { while(1) { int a=dfs(0,INF); if(!a)break; ans+=a; } } printf("%d\n",ans); } }
时间: 2024-11-05 22:49:38