網路流(小常數)
阿新 • • 發佈:2021-09-05
#include<bits/stdc++.h> using namespace std; const int inf=1000000007; struct edg{int y,nxt,v;}e[41000]; int lk[210],ltp=1; int n,m,o,a[210]; int s,t; int lvl[210]; int q[210],hd=0; int crt[210]; void addE(int x,int y,int v){ e[++ltp]=(edg){y,lk[x],v}; lk[x]=ltp; e[++ltp]=(edg){x,lk[y],0}; lk[y]=ltp; }rush!int rd(){int z=0,mk=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')mk=-1; ch=getchar();} while(ch>='0'&&ch<='9'){z=(z<<3)+(z<<1)+ch-'0'; ch=getchar();} return z*mk; } bool bfs(){ for(int i=s;i<=t;++i) lvl[i]=0; lvl[(q[(hd=1)]=s)]=1; for(int k=1;k<=hd;++k){ crt[q[k]]=lk[q[k]]; for(int i=lk[q[k]];i;i=e[i].nxt)if(e[i].v && !lvl[e[i].y]){ lvl[e[i].y]=lvl[q[k]]+1; q[++hd]=e[i].y; } } return lvl[t]; } int dfs(int x,int y){ if(x==t) return y; int bwl=0,flw=0; for(int i=crt[x];i && bwl<y;i=e[i].nxt)if(lvl[e[i].y]==lvl[x]+1 && e[i].v) if((flw=dfs(e[i].y,min(e[i].v,y-bwl)))){ e[i].v-=flw,e[i^1].v+=flw; bwl+=flw; if(!e[i].v) crt[x]=e[i].nxt; } if(!bwl) lvl[x]=0; return bwl; } int dinic(){ int bwl=0,flw=0; while(bfs())while((flw=dfs(s,inf))){ bwl+=flw; } return bwl; } void solve(){ int ans=0; s=0; t=n+m+1; for(int i=s;i<=t;i++){ lk[i]=0; } ltp=1; for(int i=1;i<=n;i++){ a[i]=rd(); addE(s,i,a[i]); ans+=a[i]; } for(int i=1;i<=m;i++){ a[i+n]=rd(); ans+=a[i+n]; addE(i+n,t,a[i+n]); } int qs=rd(); while(qs--){ int u,v; u=rd(); v=rd(); addE(u,v,inf); } ans-=dinic(); printf("%d\n",ans); return ; } int main(){ // ios::sync_with_stdio(false);cin.tie(0); while(scanf("%d%d",&n,&m)!=EOF){ solve(); } return 0; }