HDU - 4289 Control (最小割+拆点)

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 10 7.
  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

题解:

割断所有的通路,并使得花费最少,这样就一定能抓住所有恐怖分子。先拆点,每个点拆成一条有向边a->a ’边权为控制这个城市的花费,原图中的边b->a,则建成:b+200->a,a+200->b,边权都为无限大,剩下就是基础最大流了。

最大流最小割定理是网络流理论的重要定理。指在一个网络流中,能够从源点到达汇点的最大流量等于如果从网络中移除就能够导致网络流中断的边的集合的最小容量和。即在任何网络中,最大流的值等于最小割的容量。

推论一
  如果f是网络中的一个流,CUT(S,T)是一个割,那么f的值不超过割CUT(S,T)的容量。
推论二
  网络中的最大流不超过任何割的容量。
定理二
  在网络中,如果f是一个流,CUT (S,T)是一个割,且f的值等于割CUT(S,T)的 容量,那么f是一个最大流, CUT(S,T)是一个最小割。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>

using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 510;

struct Edge{
	int flow,to,rev;
	Edge(){}
	Edge(int a,int b,int c):to(a),flow(b),rev(c){}
};

vector<Edge> E[MAXN];

inline void Add(int from,int to,int flow){
	E[from].push_back(Edge(to,flow,E[to].size()));
	E[to].push_back(Edge(from,0,E[from].size()-1));
}

int deep[MAXN];
int iter[MAXN];

bool BFS(int from,int to){
	memset(deep,-1,sizeof deep);
	deep[from] = 0;
	queue<int> Q;
	Q.push(from);
	while(!Q.empty()){
		int t = Q.front();
		Q.pop();
		for(int i=0 ; i<E[t].size() ; ++i){
			Edge& e = E[t][i];
			if(e.flow > 0 && deep[e.to] == -1){
				deep[e.to] = deep[t] + 1;
				Q.push(e.to);
			}
		}
	}
	return deep[to] != -1;
}

int DFS(int from,int to,int flow){
	if(from == to || flow == 0)return flow;
	for(int& i=iter[from] ; i<E[from].size() ; ++i){
		Edge& e = E[from][i];
		if(e.flow > 0 && deep[e.to] == deep[from] + 1){
			int nowflow = DFS(e.to,to,min(flow,e.flow));
			if(nowflow > 0){
				e.flow -= nowflow;
				E[e.to][e.rev].flow += nowflow;
				return nowflow;
			}
		}
	}
	return 0;
}

int Dinic(int from,int to){
	int sumflow = 0;
	while(BFS(from,to)){
		memset(iter,0,sizeof iter);
		int mid;
		while((mid = DFS(from,to,INF)) > 0)sumflow += mid;
	}
	return sumflow;
}

int main(){
	
	int N,M;
	int from,to;
	while(scanf("%d %d",&N,&M)!=EOF){
		scanf("%d %d",&from,&to);
		to += 200;//一定要注意这里to加个200,把目标城市的单向边也考虑进去。
		for(int i=1 ; i<=N ; ++i){
			int t;
			scanf("%d",&t);
			Add(i,i+200,t);
		}
		for(int i=1 ; i<=M ; ++i){
			int a,b;
			scanf("%d %d",&a,&b);
			Add(a+200,b,INF);
			Add(b+200,a,INF);
		}
		printf("%d\n",Dinic(from,to));
		for(int i=1 ; i<=N ; ++i){
			E[i].clear();
			E[i+200].clear();
		}
	}
	
	return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值