tzoj 2380: Gopher II(二分图最大匹配)
原题链接:http://www.tzcoder.cn/acmhome/problemdetail.do?method=showdetail&id=2380
描述
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,老鼠必须在规定时间内躲到洞中。
题解:二分图最大匹配问题,用匈牙利算法解决,也可用网络流解决,我这里用匈牙利算法
 
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=110; 4 struct node 5 { 6 double x,y; 7 }G[N],H[N]; 8 int n,m,s,v; 9 int linker[N]; 10 bool used[N]; 11 vector<int> graphy[N]; 12 void init() 13 { 14 for(int i=0;i<n;i++) 15 graphy[i].clear(); 16 } 17 double getdis(double x1,double y1, double x2, double y2) 18 { 19 return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 20 } 21 bool dfs(int u) 22 { 23 for(int i=0;i<graphy[u].size();i++) 24 { 25 int v=graphy[u][i]; 26 if(!used[v]) 27 { 28 used[v]=1; 29 if(linker[v]==-1||dfs(linker[v])) 30 { 31 linker[v]=u; 32 return 1; 33 } 34 } 35 } 36 return 0; 37 } 38 int hungary() 39 { 40 int res=0; 41 memset(linker,-1,sizeof(linker)); 42 for(int i=0;i<n;i++) 43 { 44 memset(used,0,sizeof(used)); 45 if(dfs(i))res++; 46 } 47 return res; 48 } 49 int main() 50 { 51 int ans; 52 while(cin>>n>>m>>s>>v) 53 { 54 for(int i=0;i<n;i++) 55 cin>>G[i].x>>G[i].y; 56 for(int i=0;i<m;i++) 57 cin>>H[i].x>>H[i].y; 58 init(); 59 for(int i=0;i<n;i++) 60 for(int j=0;j<m;j++) 61 if(getdis(G[i].x,G[i].y,H[j].x,H[j].y)<=v*s)graphy[i].push_back(j); 62 ans=hungary(); 63 cout<<n-ans<<endl; 64 } 65 return 0; 66 }
 
                    
                
 
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号