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
解题思路:
这是一道二分匹配的题目,题意大概是将有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;
}