记录编号 194845 评测结果 AAAAAAAAAA
题目名称 [NOI 2006]最大获利 最终得分 100
用户昵称 Gravatar四季木哥 是否通过 通过
代码语言 C++ 运行时间 0.779 s
提交时间 2015-10-17 14:57:05 内存使用 1.52 MiB
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
#define INF 0x3f3f3f3f
#define MAXN 60000
using namespace std;

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

vector<EDGE> edges;
vector<int> G[MAXN];

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

int d[MAXN],cur[MAXN],s,t;
bool vis[MAXN];

bool BFS(){
	memset(vis,0,sizeof(vis));
	memset(d,0,sizeof(d));
	queue<int> q;
	q.push(s);vis[s]=1; 	
	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(e.cap>e.flow&&!vis[e.to]){
				d[e.to]=d[x]+1;
				vis[e.to]=1;
				q.push(e.to); 
			}
		}
	}
	return vis[t];
}

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

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

int main(){
	freopen("profit.in","r",stdin);
	freopen("profit.out","w",stdout);
	int n,m;
	cin>>n>>m;
	int p,sum=0,ans;
	s=0,t=n+m+1;
	for(int i=1;i<=n;i++){
		cin>>p;
		Add_Edge(i,t,p);
	}
	int a,b,c;
	for(int i=n+1;i<=m+n;i++){
		cin>>a>>b>>c;
		Add_Edge(i,a,INF);
		Add_Edge(i,b,INF);
		Add_Edge(s,i,c);
		sum+=c;
	}
	ans=sum-Maxflow();
	cout<<ans;
return 0;
}