poj-2536-Gopher II

本文解析了POJ 2536 Gopher II问题,这是一个典型的二分图匹配问题,通过匈牙利算法实现解决方案。文章详细介绍了如何根据每个地鼠到洞穴的距离和时间限制来确定哪些地鼠可以安全到达洞穴。

poj-2536-Gopher II

 

Gopher II
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9268 Accepted: 3847

Description

The gopher family, having averted the canine threat, must face a new predator. 

The are n gophers and m gopher holes, each at distinct (x, y) coordinates. A hawk arrives and if a gopher does not reach a hole in s seconds it is vulnerable to being eaten. A hole can save at most one gopher. All the gophers run at the same velocity v. The gopher family needs an escape strategy that minimizes the number of vulnerable gophers.

Input

The input contains several cases. The first line of each case contains four positive integers less than 100: n, m, s, and v. The next n lines give the coordinates of the gophers; the following m lines give the coordinates of the gopher holes. All distances are in metres; all times are in seconds; all velocities are in metres per second.

Output

Output consists of a single line for each case, giving the number of vulnerable gophers.

Sample Input

2 2 5 10
1.0 1.0
2.0 2.0
100.0 100.0
20.0 20.0

Sample Output

1

Source

 

17922621 2536Accepted448K32MSG++1179B2017-12-05 19:47:12

 

二分图匹配问题。

使用匈牙利算法解决。

 

 

/// poj-2536  

#include <cstdio>  
#include <cstring> 
const int MAXN = 100 + 10; 

struct Node{
	double x, y; 
}; 

Node gopher[MAXN], hole[MAXN]; 

int n, m, s, v, num[MAXN], gh[MAXN][MAXN], match[MAXN], vis[MAXN]; 

double DistSqu(const Node &a, const Node &b){
	return ( (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) ); 
}

bool dfs(int x){
	for(int i=0; i<num[x]; ++i){
		int y = gh[x][i]; 
		if(vis[y] == 0){
			vis[y] = 1; 
			if(match[y] < 0 || dfs(match[y])){
				match[y] = x; 
				return true; 
			}
		}
	}
	return false; 
}

int main(){
	freopen("in.txt", "r", stdin);  

	int ans; 
	while(scanf("%d %d %d %d", &n, &m, &s, &v) != EOF){
		for(int i=0; i<n; ++i){
			scanf("%lf %lf", &gopher[i].x, &gopher[i].y); 
		} 
		for(int i=0; i<m; ++i){
			scanf("%lf %lf", &hole[i].x, &hole[i].y); 
		} 
		int max_len_sq = s*s*v*v; 

		for(int i=0; i<n; ++i){
			num[i] = 0; 
			for(int j=0; j<m; ++j){
				if(DistSqu(gopher[i], hole[j]) <= max_len_sq ){
					gh[i][ num[i] ] = j; 
					++num[i]; 
				} 
			}
		} 
		memset(match, -1, sizeof(match)); 
		ans = 0; 
		for(int i=0; i<n; ++i){
			memset(vis, 0, sizeof(vis)); 
			if(dfs(i)){
				++ans; 
			}
		}
		printf("%d\n", n - ans );
	} 
	return 0; 
} 

  

 

转载于:https://www.cnblogs.com/zhang-yd/p/7989028.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值