Control

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5620 Accepted Submission(s): 2281

Problem Description
  You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD 1 from one city (the source) to another one (the destination). You know their date, source and destination, and they are using the highway network.
  The highway network consists of bidirectional highways, connecting two distinct city. A vehicle can only enter/exit the highway network at cities only.
  You may locate some SA (special agents) in some selected cities, so that when the terrorists enter a city under observation (that is, SA is in this city), they would be caught immediately.
  It is possible to locate SA in all cities, but since controlling a city with SA may cost your department a certain amount of money, which might vary from city to city, and your budget might not be able to bear the full cost of controlling all cities, you must identify a set of cities, that:
  * all traffic of the terrorists must pass at least one city of the set.
  * sum of cost of controlling all cities in the set is minimal.
  You may assume that it is always possible to get from source of the terrorists to their destination.

1 Weapon of Mass Destruction

Input
  There are several test cases.
  The first line of a single test case contains two integer N and M ( 2 <= N <= 200; 1 <= M <= 20000), the number of cities and the number of highways. Cities are numbered from 1 to N.
  The second line contains two integer S,D ( 1 <= S,D <= N), the number of the source and the number of the destination.
  The following N lines contains costs. Of these lines the ith one contains exactly one integer, the cost of locating SA in the ith city to put it under observation. You may assume that the cost is positive and not exceeding 107.
  The followingM lines tells you about highway network. Each of these lines contains two integers A and B, indicating a bidirectional highway between A and B.
  Please process until EOF (End Of File).

Output
  For each test case you should output exactly one line, containing one integer, the sum of cost of your selected set.
  See samples for detailed information.

Sample Input
5 6
5 3
5
2
3
4
12
1 5
5 4
2 3
2 4
4 3
2 1

Sample Output
3


题意: 给出许多城市,有***来炸城市,给出起点和终点,我们可以在城市中设置人口阻止***,每个城市的需要的价格不同。然后让我们求最小价格。就是一个简单的最小割。


因为给出的是城市的价格,但是最小割是割边,所以我们拆点即可。

这道题我当前弧优化跑的时间是不优化的10倍,很神奇。。。。。。

还有个zz的bug,就是我初始化 tot 写成0了。。。。。。。。因为我们要得到反边所以要初始化为1


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int N=1010,M=8e6+10;
const int inf=0x3f3f3f3f;
int n,m,s,t,h[N],cur[N];
int head[N],nex[M],to[M],w[M],tot=1;
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);
}
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;
}
int dinic(){
	int res=0;
	while(bfs())	res+=dfs(s,inf);
	return res;
}
signed main(){
	while(~scanf("%d %d",&n,&m)){
		memset(head,0,sizeof head);	tot=1;
		scanf("%d %d",&s,&t);	t+=n;
		for(int i=1;i<=n;i++){
			int w;	scanf("%d",&w);	add(i,i+n,w);
		}
		while(m--){
			int a,b; scanf("%d %d",&a,&b);	add(a+n,b,inf);	add(b+n,a,inf);
		}
		printf("%d\n",dinic());
	}
	return 0;
}