题解:
dinic算法
然后注意一下读入
建立原点汇点
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int N=405; int q[N*N],n,m1,m2,m3,sum,dis[N],x,y,ans,f[N],z,a[N][N]; int bfs() { memset(dis,0xff,sizeof dis); dis[1]=0; int l=0,r=1; q[1]=1; while (l<r) { int j=q[++l]; for (int i=1;i<=n;i++) if (dis[i]<0&&a[j][i]>0) { dis[i]=dis[j]+1; q[++r]=i; } } if (dis[n]>0)return 1; return 0; } int find(int x,int low) { int b=0; if (x==n)return low; for (int i=1;i<=n;i++) if (a[x][i]>0&&dis[i]==dis[x]+1&&(b=find(i,min(low,a[x][i])))) { a[x][i]-=b; a[i][x]+=b; return b; } return 0; } int read() { int x=0;char c; for (;c<'0'||c>'9';c=getchar()); for (;c>='0'&&c<='9';c=getchar())x=x*10+c-48; return x; } int main() { while (~scanf("%d%d%d%d",&n,&m1,&m2,&m3)) { memset(a,0,sizeof a); while (m3--) { x=read();y=read();z=read(); a[x+2][y+2]+=z; } while (m1--) { x=read();y=read(); a[1][x+2]+=y; } while (m2--) { x=read();y=read(); a[x+2][n+2]+=y; } n+=2; ans=0;int t; while (bfs()) while (t=find(1,0x7fffffff)) ans+=t; printf("%d ",ans); } }