很明显的最大流问题,添加超级源点与Power Station相连,超级汇点与Dispatchers相连 代码: #include<iostream> using namespace std; int g[105][105],cap[105][105]; int ResidualCap[105],pre[105],q[105]; int n,np,nc,m,ans; int min(int a,int b) { return a<b?a:b; } void update() { int i,min=9999999; /*for(i=n+1;i!=n;i=pre[i]) { if(cap[pre[i]][i]-g[pre[i]][i]<min) min=cap[pre[i]][i]-g[pre[i]][i]; }*/ for(i=n+1;i!=n;i=pre[i]) { g[pre[i]][i]+=ResidualCap[n+1]; g[i][pre[i]]=-ResidualCap[n+1]; } ans+=ResidualCap[n+1]; } bool bfs() { int head=0,tail=1,i,j; memset(ResidualCap,0,sizeof(ResidualCap)); q[1]=n; ResidualCap[n]=9999999; pre[n]=-1; while(head<tail) { head=head+1; j=q[head]; for(i=0;i<n+2;i++) { if(cap[j][i]>g[j][i]&&!ResidualCap[i]) { tail=tail+1; q[tail]=i; pre[i]=j; ResidualCap[i]=ResidualCap[j]; if(ResidualCap[i]>cap[j][i]-g[j][i]) ResidualCap[i]=cap[j][i]-g[j][i]; if(i==n+1) return true; } } } return false; } void EK() { while(bfs()) update(); } int main() { int i,j,k,a,b,t; while(scanf("%d %d %d %d",&n,&np,&nc,&m)!=EOF) { memset(g,0,sizeof(g)); memset(c,0,sizeof(c)); for(i=1;i<=m;i++) { scanf(" (%d,%d)%d",&a,&b,&t); cap[a][b]=t; } for(i=1;i<=np;i++)//添加超级源点 { scanf(" (%d)%d",&a,&t); cap[n][a]=t; } for(j=1;j<=nc;j++)//添加超级汇点 { scanf(" (%d)%d",&a,&t); cap[a][n+1]=t; } ans=0; EK(); printf("%d/n",ans); } return 0; }