POJ2536(二分图最大匹配)
Gopher II
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 8504 | Accepted: 3515 |
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
思路:若gopher能到达hole,则在两者之间建一条边。答案为 n-二分图最大匹配。
#include <cstdio> #include <cmath> #include <vector> #include <cstring> using namespace std; const int MAXN=205; struct Node{ double x,y; }gopher[MAXN],hole[MAXN]; int n,m,s,v; double dist(double x1,double y1,double x2,double y2) { return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } vector<int> arc[MAXN]; int match[MAXN],vis[MAXN]; bool dfs(int u) { for(int i=0;i<arc[u].size();i++) { int to=arc[u][i]; if(!vis[to]) { vis[to]=1; int w=match[to]; if(w==-1||dfs(w)) { match[u]=to; match[to]=u; return true; } } } return false; } int max_flow() { int ans=0; memset(match,-1,sizeof(match)); for(int i=1;i<=n;i++) { if(match[i]==-1) { memset(vis,0,sizeof(vis)); if(dfs(i)) ans++; } } return ans; } int main() { while(scanf("%d%d%d%d",&n,&m,&s,&v)!=EOF) { for(int i=1;i<MAXN;i++) arc[i].clear(); for(int i=1;i<=n;i++) { scanf("%lf%lf",&gopher[i].x,&gopher[i].y); } for(int i=1;i<=m;i++) { scanf("%lf%lf",&hole[i].x,&hole[i].y); } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { double d=dist(gopher[i].x,gopher[i].y,hole[j].x,hole[j].y); if(v*s>=d) { int u=i,v=j+n; arc[u].push_back(v); arc[v].push_back(u); } } } int res=max_flow(); printf("%d\n",n-res); } return 0; }