显示代码纯文本
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<deque>
using namespace std;
const int SIZEN=210,SIZEM=5010,maxn=0x7fffffff/2;
int N,M,e[SIZEN],H[SIZEN],tot=0,S,T;
deque<int> s[SIZEN],Q;
class miku
{
public:
int fr,to,cap,flow;
miku(){}
miku(int a,int b,int c,int d)
{
fr=a,to=b,cap=c,flow=d;
}
}E[6*(SIZEN+SIZEM)];
void add(int fr,int to,int cap)
{
s[fr].push_back(tot);
E[tot++]=miku(fr,to,cap,0);
s[to].push_back(tot);
E[tot++]=miku(to,fr,0,0);
}
void read()
{
int a,b,w;
scanf("%d%d",&N,&M);
S=0,T=2*N+1;
for(int i=1;i<=N;i++)
{
scanf("%d",&w);
add(i+N,T,w);
}
for(int i=1;i<=N;i++)
{
scanf("%d",&w);
add(S,i,w);
}
for(int i=1;i<=M;i++)
{
scanf("%d%d",&a,&b);
add(a,b+N,maxn);
}
}
void push(int x,int t)
{
miku &v=E[t];
int flow=min(e[x],v.cap-v.flow);
e[x]-=flow;v.flow+=flow;e[v.to]+=flow;E[t^1].flow-=flow;
if(v.to!=S&&v.to!=T&&e[v.to]>0) Q.push_back(v.to);
}
void use_up(int x)
{
int mi;
while(e[x])
{
mi=maxn;
for(int i=0;i<s[x].size();i++)
{
miku &v=E[s[x][i]];
if(v.cap-v.flow<=0) continue;
if(H[v.to]==H[x]-1)
{
push(x,s[x][i]);
if(!e[x]) break;
}
else if(H[v.to]<mi) mi=H[v.to];
}
if(!e[x]) break;
H[x]=mi+1;
}
}
int maxflow()
{
memset(e,0,sizeof(e));
memset(H,0,sizeof(H));
e[0]=maxn;H[0]=N;if(H[0]>20) H[0]/=2;
for(int i=0;i<s[0].size();i++) push(0,s[0][i]);
while(!Q.empty())
{
int x=Q.front();Q.pop_front();
use_up(x);
}
return e[T];
}
void work()
{
int ans=maxflow();
printf("%d\n",ans);
}
int main()
{
freopen("destroyingthegraph.in","r",stdin);
freopen("destroyingthegraph.out","w",stdout);
read();
work();
return 0;
}