poj 2536 Gopher II(二分图最大匹配,构图)

Gopher II
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8347 Accepted: 3444

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
题意:有n只地鼠,m个洞穴,老鹰要抓地鼠,地鼠有s秒的时间逃跑,地鼠的速度是每秒v米,两点间的距离就是直线距离

先给出地鼠和洞穴的坐标,问最少有几只地鼠可以逃脱?

思路:最少有几只地鼠可以逃脱其实就是最多有几只地鼠可以进洞里,那么就是二分图最大匹配的问题,可是边怎么办呢?

题目给你s和v,其实就是说地鼠与洞穴间的距离不能超过s*v,所以我们把所有<=s*v的边连接起来,那么就是一个二分图了。

直接跑一遍匈牙利算法即可

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 150
int n,m,s,v;
struct Node
{
    double x,y;
} p[N];
int ma[N][N];
int vis[N],line[N];
double dist(Node a,Node b)
{
    return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
}
int can(int t)
{
    for(int i=1; i<=m; i++)
    {
        if(!vis[i]&&ma[t][i])
        {
            vis[i]=1;
            if(line[i]==-1||can(line[i]))
            {
                line[i]=t;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    Node k;
    while(~scanf("%d %d %d %d",&n,&m,&s,&v))
    {
        memset(line,-1,sizeof(line));
        memset(ma,0,sizeof(ma));
        v=s*v;
        for(int i=1; i<=n; i++)
            scanf("%lf %lf",&p[i].x,&p[i].y);
        for(int i=1; i<=m; i++)
        {
            scanf("%lf %lf",&k.x,&k.y);
            for(int j=1; j<=n; j++)
                if(dist(k,p[j])<=v)
                    ma[j][i]=1;
        }
        int ans=0;
        for(int i=1; i<=n; i++)
        {
            memset(vis,0,sizeof(vis));
            if(can(i))
                ans++;
        }
        printf("%d\n",n-ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值