链接:点击打开链接
题意:输入n,np,nc,m,分别代表节点个数,发电站个数,消耗站个数,电线个数,依次按要求输入问消耗站最多能获得的电量是多少
代码:
#include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> using namespace std; int s[505][505],dis[505],q[2005]; int n,np,nc,m; int bfs(){ int i,u,l,h; memset(dis,-1,sizeof(dis)); dis[0]=0;q[0]=1; l=0;h=1; while(l<h){ u=q[++l]; for(i=0;i<=n+1;i++) if(s[u][i]&&dis[i]==-1){ dis[i]=dis[u]+1; q[++h]=i; } } if(dis[n+1]!=-1) return 1; return 0; } int dinic(int x,int sum){ int i,a; if(x==n+1) return sum; for(i=0;i<=n+1;i++) if(s[x][i]>0&&(dis[i]==dis[x]+1)&&(a=dinic(i,min(sum,s[x][i])))){ s[x][i]-=a; s[i][x]+=a; return a; } dis[x]=-1; return 0; } //dinic算法模板 int main(){ int u,v,w,ans,temp; while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF){ memset(s,0,sizeof(s)); while(m--){ while(getchar()!='('); scanf("%d,%d)%d",&u,&v,&w); s[u+1][v+1]+=w; } while(np--){ //将0和n+1分别设为源点和汇点 while(getchar()!='('); scanf("%d)%d",&u,&v); s[0][u+1]+=v; } while(nc--){ while(getchar()!='('); scanf("%d)%d",&u,&v); s[u+1][n+1]+=v; } ans=0; while(bfs()){ while(temp=dinic(0,99999999)) ans+=temp; } printf("%d ",ans); } return 0; }