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; 
    } 
    

      

  • 相关阅读:
    spark 随意笔记
    c#读取输入字符串,从数据源中查找以该字符串开头的所有字符串(使用正则表达式)
    我的收藏链接地址
    SQL查询时,遇到用到关键词作的字段。将该字段用一对中括号括起来[]
    SQL数据类型相互转换
    Javascript获取系统当前时间
    节点类型nodeType的取值
    混合布局编程挑战
    Webstorm破解方法
    二列布局
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/7989028.html
Copyright © 2011-2022 走看看