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