1. 程式人生 > >Gopher II(匈牙利算法模板)

Gopher II(匈牙利算法模板)

() inpu 距離 NPU ret eve ase tin edi

描述

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",n-sum); 46 } 47 return 0; 48 }

Gopher II(匈牙利算法模板)