Road constructions
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1916 Accepted Submission(s): 657

Problem Description
N cities are required to connect with each other by a new transportation system. After several rounds of bidding, we have selected M constructions companies and
decided which section is assigned to which company, the associated cost and the direction of each road.

Due to the insufficiency of national fiscal revenue and special taxation system (the tax paid by each company pays is a fixed amount and tax payment occurs at the
beginning of the construction of the project) The government wishes to complete the project in several years and collects as much tax as possible to support the public
expense

For the restrictions of construction and engineering techniques, if a company is required to start the construction, then itself and its associated companies have to
complete all the tasks they commit (if company A constructs a road
from city 1 to city 2, company B constructs a road from city 2 to city 3, company C constructs a road from city 1 to city 3, we call
companies A and B are associated and other company pairs have no such relationship, pay attention, in this example and a are not associated, in other words,’
associated’ is a directed relationship).
Now the question is what the maximum income the government can obtain in the first year is?

Input
There are multiple cases (no more than 50).
Each test case starts with a line, which contains 2 positive integers, n and m (1<=n<=1000, 1<=m<=5000).
The next line contains m integer which means the tax of each company.
The Third line has an integer k (1<=k<=3000)which indicates the number of the roads.
Then k lines fellow, each contains 4 integers, the start of the roads, the end of the road, the company is responsible for this road and the cost of the road.
The end of the input with two zero

Output
For each test case output the maximum income in a separate line, and if you can not get any income, please output 0.

Sample Input
4 2
500 10
4
1 2 1 10
2 3 1 20
4 3 1 30
1 4 2 60
4 2
500 100
5
1 2 1 10
2 3 1 20
4 3 1 30
4 3 2 10
1 4 2 60
3 1
10
3
1 2 1 100
2 3 1 100
3 1 1 100
0 0

Sample Output
440
470
0


题目很难理解。

题目大意:有m个公司我们可以选择,如果选择,那么这些公司负责的道路也必须选择,如果道路之间有连接性,比如1->2 , 2->3 。如果我们选择了1到2,那么2到3的公司我们也必须选择。


一道最大权闭合子图,比较简单。按照最小割建图即可。


#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1e5+10,M=1e5+10;
int n,m,h[N],s,t,k,val[N],u[N],v[N],id[N],res;
int head[N],nex[M],to[M],w[M],tot;
vector<int> v1[N],v2[N];
inline void ade(int a,int b,int c){
	to[++tot]=b; w[tot]=c; nex[tot]=head[a]; head[a]=tot;
}
inline void add(int a,int b,int c){
	ade(a,b,c);	ade(b,a,0);
}
inline void init(){
	tot=1;	memset(head,0,sizeof head); t=m+1; res=0;
	for(int i=1;i<=n;i++)	v1[i].clear(),v2[i].clear();
}
inline int bfs(){
	memset(h,0,sizeof h); h[s]=1;	queue<int> q;	q.push(s);
	while(q.size()){
		int u=q.front();	q.pop();
		for(int i=head[u];i;i=nex[i]){
			if(w[i]&&!h[to[i]]){
				h[to[i]]=h[u]+1;	q.push(to[i]);
			}
		}
	}
	return h[t];
}
int dfs(int x,int f){
	if(x==t)	return f; int fl=0;
	for(int i=head[x];i&&f;i=nex[i]){
		if(w[i]&&h[to[i]]==h[x]+1){
			int mi=dfs(to[i],min(w[i],f));
			w[i]-=mi; w[i^1]+=mi; fl+=mi; f-=mi;
		}
	}
	if(!fl)	h[x]=-1;
	return fl;
}
inline int dinic(){
	int res=0;
	while(bfs())	res+=dfs(s,inf);
	return res;
}
signed main(){
	while(scanf("%d %d",&n,&m),n){
		init();
		for(int i=1;i<=m;i++)	scanf("%d",&val[i]);	scanf("%d",&k);
		for(int i=1;i<=k;i++){
			int c;	scanf("%d %d %d %d",&u[i],&v[i],&id[i],&c);
			val[id[i]]-=c; v1[u[i]].push_back(v[i]); v2[u[i]].push_back(id[i]);
		}
		for(int i=1;i<=m;i++)
			if(val[i]>0)	add(s,i,val[i]),res+=val[i];
			else	add(i,t,-val[i]);
		for(int i=1;i<=k;i++){
			for(int j=0;j<v1[v[i]].size();j++){
				if(v2[v[i]][j]!=id[i])	add(id[i],v2[v[i]][j],inf);
			}
		}
		printf("%d\n",res-dinic());
	}
	return 0;
}