# [HZNOI #514] Magic

## 题意

$$n\le 1000,m\le50000$$

## 题解

### 参考代码

#include <bits/stdc++.h>

const int MAXV=1e5+10;
const int MAXE=5e6+10;
const int INF=0x7FFFFFFF;

struct Edge{
int from;
int to;
int flow;
Edge* rev;
Edge* next;
};
Edge E[MAXE];
Edge* cur[MAXV];
Edge* top=E;

int v;
int e;
int a[1010];
int b[1010];
int depth[MAXV];

bool BFS(int,int);
int Dinic(int,int);
int DFS(int,int,int);
void Insert(int,int,int);

int main(){
freopen("magic.in","r",stdin);
freopen("magic.out","w",stdout);
scanf("%d%d",&v,&e);
for(int i=1;i<=v;i++)
scanf("%d",a+i);
for(int i=1;i<=v;i++)
scanf("%d",b+i);
for(int i=0;i<e;i++){
int a,b;
scanf("%d%d",&a,&b);
}
for(int i=1;i<=v;i++)
int s=0,t=1,cnt=v*2+1;
for(int i=1;i<=v;i++){
Insert(s,i+1,INF);
Insert(i+v+1,t,b[i]);
int last=i+1;
++cnt;
last=cnt;
}
}
printf("%d\n",Dinic(s,t));
return 0;
}

int Dinic(int s,int t){
int ans=0;
while(BFS(s,t))
ans+=DFS(s,INF,t);
return ans;
}

bool BFS(int s,int t){
memset(depth,0,sizeof(depth));
std::queue<int> q;
q.push(s);
depth[s]=1;
while(!q.empty()){
s=q.front();
q.pop();
if(i->flow>0&&depth[i->to]==0){
depth[i->to]=depth[s]+1;
if(i->to==t)
return true;
q.push(i->to);
}
}
}
return false;
}

int DFS(int s,int flow,int t){
if(s==t||flow<=0)
return flow;
int rest=flow;
for(Edge*& i=cur[s];i!=NULL;i=i->next){
if(i->flow>0&&depth[i->to]==depth[s]+1){
int tmp=DFS(i->to,std::min(rest,i->flow),t);
if(tmp<=0)
depth[i->to]=0;
rest-=tmp;
i->flow-=tmp;
i->rev->flow+=tmp;
if(rest<=0)
break;
}
}
return flow-rest;
}

inline void Insert(int from,int to,int flow){
top->from=from;
top->to=to;
top->flow=flow;
top->rev=top+1;