poj_2536 Gopher II

Gopher II

Time Limit: 2000MS

Memory Limit: 65536K

Total Submissions: 5341

Accepted: 2216

题目链接:http://poj.org/problem?id=2536

Description

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

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

Input

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

Output

Output consists of a single line for eachcase, 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

Waterloo local 2001.01.27

解题思路:

      这是一道二分匹配的题目,题意大概是将有n只老鼠要跑到m个洞穴中来躲避老鹰的追捕,每个洞穴只能藏一只老鼠,在时间s内,老鼠的速度v,问最后有几只老鼠会遭到攻击。根据坐标来确定距离,计算每个洞穴与每一只老鼠之间的距离,判断是否大于s*v,如果小于的话表示该老鼠能到达这个洞穴,这样来建议二分图,在用匈牙利算法进行匹配

代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<math.h>
#define maxn 104
using namespace std;
struct coordinates
{
    double x;
    double y;
};

coordinates coor[maxn];
int map[maxn][maxn];
int visited[maxn];
int mak[maxn];
int n,m,s,v;//老鼠,洞,时间,速度

//得到两点之间的距离
double getDist(double x1,double y1,double x2,double y2)
{
    return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}

bool bfs(int x)
{
	int i;
	for(i=1;i<=m;i++)
	{
		if(map[x][i] && !visited[i])
		{
			visited[i]=1;
			if(!mak[i] || bfs(mak[i]))
			{
				mak[i]=x;
				return true;
			}
		}
	}
	return false;
}

void hungary()
{
	int i;
	int count=0;
	memset(mak,0,sizeof(mak));
	for(i=1;i<=n;i++)
	{
		memset(visited,0,sizeof(visited));
		if(bfs(i))
		{
			count++;
		}
	}
	printf("%d\n",n-count);//可能受伤害的老鼠
}

int main()
{
    int i,j;
    double x,y;
    while(scanf("%d%d%d%d",&n,&m,&s,&v)!=EOF)
    {
        memset(map,0,sizeof(map));
        memset(coor,0,sizeof(coor));

		int length=s*v;//速度*时间
        //老鼠的位置
        for(i=1;i<=n;i++)
        {
            scanf("%lf%lf",&x,&y);
            coor[i].x=x;
            coor[i].y=y;
        }

        //老鼠洞的位置
        for(i=1;i<=m;i++)
        {
            scanf("%lf%lf",&x,&y);
            for(j=1;j<=n;j++)
            {//每个老鼠洞与老鼠i的距离
                double dist=getDist(x,y,coor[j].x,coor[j].y);
				//计算老鼠j能否在时间s,速度v内跑到洞中
				if(length>=dist)
					map[j][i]=1;//表示老鼠j能跑到洞i中
            }
        }
		hungary();

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯的世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值