HDU Problem- 4289 Control (网络流_最小割)

HDU Problem- 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

题目大意:

给定一个n个点m条边的图,给定起点和终点。
每个有一个值(注意不是边),阻塞一些点让起始点到终点不连通。问最小花费。

解题思路:

题目意思很明显就是求最小割,如果给的不是点值,而是边值,那么就是裸的网络流的板子。
但是题目给的是点的值,怎么办呢? 想办法把点值变成边值。
把一个点拆分成两个点 i i+n,**连接这俩点,流量为这个点的权值。**那么如果有一条边是连接i -j 那么我们只要 将 i+n 和 j 相连就可以了,流量设为多少呢?设为inf,因为这边是可以无限走的,没有任何流量限制。
建好图跑Dinic 就是所求的最小割了。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <algorithm> 
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e4+1000;
int head[1000],cnt = 0;
int val[1000];
int n,m;
int sp,tp;
struct node{
	int to;
	int w;
	int next;
}side[maxn*10];
int deep[1000];
int cur[1000];
void init(){
	memset(head,-1,sizeof(head));
	cnt = 0;
}
void add(int x,int y,int w){
	side[cnt].to = y;
	side[cnt].w = w;
	side[cnt].next = head[x];
	head[x] = cnt++;
}
bool bfs(){
	memset(deep,-1,sizeof(deep));
	queue<int> q;
	q.push(sp);
	deep[sp] = 0;
	while(q.size()){
		int now = q.front();
		q.pop();
		for(int i=head[now];i!=-1;i=side[i].next){
			int to = side[i].to;
			if(deep[to]==-1&&side[i].w>0){
				deep[to] = deep[now]+1;
				q.push(to);
				if(to == tp) break;
			}
		}
	}
	return deep[tp]!=-1;
}
int dfs(int u,int cap){
	if(u ==tp||cap ==0) return cap;
	int res = 0;
	int f;
	for(int &i=cur[u];i!=-1;i=side[i].next){
		int to = side[i].to;
		if(deep[to] ==deep[u]+1&&(f=dfs(to,min(cap-res,side[i].w)))>0){
			side[i].w-=f;
			side[i^1].w+=f;
			res+=f;
			if(res == cap) return cap;
		}
	}
	if(!res) deep[u]=-1;
	return res;
}
int Dinic(){
	int ans = 0;
	while(bfs()){
		for(int i=1;i<=2*n;i++){
			cur[i] = head[i];
		}
		ans +=dfs(sp,inf);
	}
	return ans;
}
int main(){
	while(~scanf("%d%d",&n,&m)){
		init();
		int ss,tt;
		scanf("%d%d",&ss,&tt);
		sp = ss;
		tp = tt+n;
		for(int i=1;i<=n;i++){
			scanf("%d",&val[i]);
			add(i,i+n,val[i]);
			add(i+n,i,0); 
		}
		int u,v;
		while(m--){
			int x,y;
			scanf("%d%d",&x,&y);
		/// x+n -> y
			add(x+n,y,inf);
			add(y,x+n,0);
		// y+n -> x 
			add(y+n,x,inf);
			add(x,y+n,0);
		}
		printf("%d\n",Dinic());
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值