记录编号 194206 评测结果 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
题目名称 [NEERC 2003][POJ2125]有向图破坏 最终得分 100
用户昵称 Gravatar四季木哥 是否通过 通过
代码语言 C++ 运行时间 0.060 s
提交时间 2015-10-16 08:20:04 内存使用 0.32 MiB
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#define INF 0x3f3f3f3f
#define N 250
using namespace std;

struct EDGE{
	int from,to,cap,flow;
};

vector<EDGE> edges;
vector<int> G[N];
bool vis[N];
int d[N],cur[N];
int s,t;

void Add_Edge(int from,int to,int cap){
	int m=edges.size();
	edges.push_back((EDGE){from,to,cap,0});
	G[from].push_back(m);   
}

bool BFS(){
	memset(vis,0,sizeof(vis));
	queue<int> q;
	q.push(s); 
	vis[s]=1;
	d[s]=0;
	while(!q.empty()){
		int x=q.front();
		q.pop();
		for(int i=0;i<G[x].size();i++){
			EDGE e=edges[G[x][i]];
			if(!vis[e.to]&&e.cap>e.flow){
				q.push(e.to);
				d[e.to]=d[x]+1; 
				vis[e.to]=1;
			}
		}
	}
	return vis[t];
}

int DFS(int x,int Min){
	if(x==t||Min==0) return Min;
	int f,flow=0;
	for(int &i=cur[x];i<G[x].size();i++){
		EDGE &e=edges[G[x][i]];
		if(d[e.to]==d[x]+1){
			f=DFS(e.to,min(Min,e.cap-e.flow));	
			if(f>0){
				e.flow+=f;
				edges[G[x][i]^1].flow-=f;
				flow+=f;
				Min-=f;
				if(Min==0) return flow;
			}
		}
	}
	return flow;
}

int Maxflow(){
	int flow=0;
	while(BFS()){
		memset(cur,0,sizeof(cur));
		flow+=DFS(s,INF);
	}
	return flow;
}

int w[2][N];

int main(void){
	freopen("destroyingthegraph.in","r",stdin);
	freopen("destroyingthegraph.out","w",stdout);
	int n,m,u,v;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		cin>>w[0][i];
	for(int i=1;i<=n;i++)
		cin>>w[1][i];
	for(int i=1;i<=m;i++){
		cin>>u>>v;
		Add_Edge(u,v+n,INF);
		Add_Edge(v+n,u,0);
	}
	for(int i=1;i<=n;i++){
		Add_Edge(0,i,w[1][i]);
		Add_Edge(i,0,0);
		Add_Edge(i+n,2*n+1,w[0][i]);
		Add_Edge(2*n+1,i+n,0);
	}
	s=0,t=2*n+1;
	int ans=Maxflow();
	cout<<ans;
return 0;
}