1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include <bits/stdc++.h> using namespace std; typedef long long LL; #define MAXN 2010 int n,m,k; int b[MAXN],c[MAXN]; namespace graph { const int N=1e4; struct node { int v; node *nnext; } pool[2*N],*h[2*N]; int cnt=-1; void addedge(int u,int v) { node *p=&pool[++cnt]; p->v=v,p->nnext=h[u],h[u]=p; node *q=&pool[++cnt]; q->v=u,q->nnext=h[v],h[v]=q; } int connect[N]; bool state[N]; bool dfs(int u) { int v; for(node *p=h[u]; p; p=p->nnext) { v=p->v; if(!state[v]) { state[v]=true; if(connect[v]==0 || dfs(connect[v])) { connect[v]=u; return true; } } } return false; } LL hungary() { LL ret=0; for(int j=1; j<=n; j++) { memset(state,0,sizeof(state)); if(dfs(j)) ret+=b[j]; } return ret;
} } int main() { int u,v; LL ans=0; scanf("%d%d%d",&n,&m,&k); for(int i=1; i<=n; i++) { scanf("%d",&b[i]); ans+=b[i]; } for(int i=1; i<=n; i++) { scanf("%d",&c[i]); ans+=c[i]; } for(int i=1; i<=m; i++) { scanf("%d%d",&u,&v); if(b[u]==c[v]) graph::addedge(u,n+v); } printf("%lld\n",ans - graph::hungary()); return 0; }
|