zoukankan      html  css  js  c++  java
  • 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; 
    } 
    

      

  • 相关阅读:
    【276】◀▶ Python 字符串函数说明
    Spring事务配置的五种方式 巨全!不看后悔,一看必懂!
    Android Developers:两个视图渐变
    《Linux命令行与shell脚本编程大全》 第二十七章 学习笔记
    Android的TextView与Html相结合的用法
    嵌入式C语言优化小技巧
    vxworks获取系统时间编程
    【算法与数据结构】在n个数中取第k大的数(基础篇)
    字符集转换 字符类型转换 utf-8 gb2312 url
    java 从零开始,学习笔记之基础入门<Oracle_基础>(三十三)
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/7989028.html
Copyright © 2011-2022 走看看