Gopher II(匈牙利算法模板)

描述

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 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 consists of a single line for each case, giving the number of vulnerable gophers.

样例输入

2 2 5 10
1.0 1.0
2.0 2.0
100.0 100.0
20.0 20.0

样例输出

 1

 

鹰抓老鼠,n只老鼠,m个洞,每个洞只能容纳一只老鼠,速度为v,安全距离是s*v,求被吃掉的老鼠,用匈牙利算法

 

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cmath>
 5 using namespace std;
 6 
 7 int n,m,s,v;
 8 struct dis
 9 {
10     double x,y;
11 }gop[105],hol[105];//老鼠,洞的位置
12 int flag[105],par[105];//洞是否匹配,洞匹配的老鼠
13 
14 int match(int u)
15 {
16     double len;
17     for(int i=1;i<=m;i++){
18         len=sqrt((gop[u].x-hol[i].x)*(gop[u].x-hol[i].x)+(gop[u].y-hol[i].y)*(gop[u].y-hol[i].y));
19         if(len-s*v*1.0<1e-6&&!flag[i]){//能在鹰到达前进洞,且在本次匹配中未匹配
20             flag[i]=1;//匹配
21             if(!par[i]||match(par[i])){//该洞没有老鼠或者可以将该洞的老鼠安全转移至另一个洞
22                 par[i]=u;//进洞
23                 return 1;
24             }
25         }
26     }
27     return 0;
28 }
29 
30 int main()
31 {
32     while(scanf("%d%d%d%d",&n,&m,&s,&v)!=EOF){
33         for(int i=1;i<=n;i++){
34             scanf("%lf%lf",&gop[i].x,&gop[i].y);
35         }
36         for(int i=1;i<=m;i++){
37             scanf("%lf%lf",&hol[i].x,&hol[i].y);
38         }
39         int sum=0;
40         memset(par,0,sizeof(par));
41         for(int i=1;i<=n;i++){
42             memset(flag,0,sizeof(flag));//每次都初始化
43             sum+=match(i);
44         }
45         printf("%d
",n-sum);
46     }
47     return 0;
48 }

 

原文地址:https://www.cnblogs.com/ChangeG1824/p/9510630.html