Poj--2536(二分图最大匹配)

2014-11-04 17:53:19

思路:二分图最大匹配经典模型,根据每个地鼠能到的洞建有向边。

 1 /*************************************************************************
 2     > File Name: 2536.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Tue 04 Nov 2014 05:44:15 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <map>
14 #include <set>
15 #include <stack>
16 #include <queue>
17 #include <iostream>
18 #include <algorithm>
19 using namespace std;
20 #define lp (p << 1)
21 #define rp (p << 1|1)
22 #define getmid(l,r) (l + (r - l) / 2)
23 #define MP(a,b) make_pair(a,b)
24 typedef long long ll;
25 const int INF = 1 << 30;
26 const double eps = 1e-8;
27 const int maxn = 110;
28 
29 int n,m,s,v;
30 double gx[maxn],gy[maxn],hx[maxn],hy[maxn];
31 int first[maxn],next[maxn * maxn],ver[maxn * maxn],ecnt;
32 int used[maxn],mat[maxn];
33 
34 void Init(){
35     memset(first,-1,sizeof(first));
36     ecnt = 0;
37 }
38 
39 void Add_edge(int u,int v){
40     next[++ecnt] = first[u];
41     ver[ecnt] = v;
42     first[u] = ecnt;
43 }
44 
45 double Dis(double x1,double y1,double x2,double y2){
46     return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
47 }
48 
49 void Build_graph(){
50     double d = 1.0 * s * v;
51     for(int i = 1; i <= n; ++i){
52         for(int j = 1; j <= m; ++j){
53             if(d - Dis(gx[i],gy[i],hx[j],hy[j]) > eps){
54                 Add_edge(i,j);
55             }
56         }
57     }
58 }
59 
60 bool find(int p){
61     for(int i = first[p]; i != -1; i = next[i]){
62         int v = ver[i];
63         if(used[v] == 0){
64             used[v] = 1;
65             if(mat[v] == 0 || find(mat[v])){
66                 mat[v] = p;
67                 return true;
68             }
69         }
70     }
71     return false;
72 }
73 
74 int Hungary(){
75     int ans = 0;
76     memset(mat,0,sizeof(mat));
77     for(int i = 1; i <= n; ++i){
78         memset(used,0,sizeof(used));
79         if(find(i)) ++ans;
80     }
81     return ans;
82 }
83 
84 int main(){
85     while(scanf("%d%d%d%d",&n,&m,&s,&v) != EOF){
86         Init();
87         for(int i = 1; i <= n; ++i)
88             scanf("%lf%lf",&gx[i],&gy[i]);
89         for(int i = 1; i <= m; ++i)
90             scanf("%lf%lf",&hx[i],&hy[i]);
91         Build_graph();
92         printf("%d\n",n - Hungary());
93     }
94     return 0;
95 }

 

posted @ 2014-11-04 17:54  Naturain  阅读(162)  评论(0编辑  收藏  举报