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   2536 Accepted 448K 32MS G++ 1179B 2017-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 - ans );
	} 
	return 0; 
} 

  

原文地址:https://www.cnblogs.com/zhang-yd/p/7989028.html