记录编号 347251 评测结果 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
题目名称 [NEERC 2003][POJ2125]有向图破坏 最终得分 100
用户昵称 GravatarAntiLeaf 是否通过 通过
代码语言 C++ 运行时间 0.070 s
提交时间 2016-11-13 07:46:53 内存使用 0.50 MiB
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF (0x3f3f3f3f)
using namespace std;
const int maxn=310,maxe=10010;
struct edge{int to,cap,prev;}e[maxe<<1];
void addedge(int,int,int);
void AddEdge(int,int,int);
int Dinic();
void bfs();
int dfs(int,int);
int n,m,last[maxn],len=0,s,t,d[maxn],now[maxn],q[maxn],head,tail,x,y;
int main(){
#define MINE
#ifdef MINE
	freopen("destroyingthegraph.in","r",stdin);
	freopen("destroyingthegraph.out","w",stdout);
#endif
	memset(last,-1,sizeof(last));
	scanf("%d%d",&n,&m);
	s=(n<<1|1);t=s+1;
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		addedge(i+n,t,x);
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		addedge(s,i,x);
	}
	while(m--){
		scanf("%d%d",&x,&y);
		addedge(x,y+n,INF);
	}
	printf("%d",Dinic());
#ifndef MINE
	printf("\n-------------------------DONE-------------------------\n");
	for(;;);
#endif
	return 0;
}
void addedge(int x,int y,int z){
	AddEdge(x,y,z);
	AddEdge(y,x,0);
}
void AddEdge(int x,int y,int z){
	e[len].to=y;
	e[len].cap=z;
	e[len].prev=last[x];
	last[x]=len++;
}
int Dinic(){
	int ans=0;
	for(;;){
		bfs();
		if(d[t]==INF)break;
		memcpy(now,last,sizeof(now));
		ans+=dfs(s,INF);
	}
	return ans;
}
void bfs(){
	int x;
	memset(d,63,sizeof(d));
	head=tail=0;
	q[tail++]=s;
	d[s]=0;
	while(head!=tail){
		x=q[head++];
		for(int i=last[x];i!=-1;i=e[i].prev)if(e[i].cap>0&&d[e[i].to]>d[x]+1){
			d[e[i].to]=d[x]+1;
			q[tail++]=e[i].to;
		}
	}
}
int dfs(int x,int delta){
	if(x==t||!delta)return delta;
	int flow=0,f;
	for(int &i=now[x];i!=-1;i=e[i].prev)if(e[i].cap>0&&d[e[i].to]==d[x]+1){
		f=dfs(e[i].to,min(delta,e[i].cap));
		if(!f)continue;
		e[i].cap-=f;
		e[i^1].cap+=f;
		flow+=f;
		delta-=f;
		if(!delta)break;
	}
	return flow;
}