码迷,mamicode.com
首页 > 编程语言 > 详细

二分图匹配(匈牙利算法) UVA 10080 Gopher II

时间:2015-07-16 21:53:08      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

 

题目传送门

 1 /*
 2     匈牙利算法:这题比UVA_670简单,注意是要被吃的鼠的最少个数,套模板
 3 */
 4 #include <cstdio>
 5 #include <algorithm>
 6 #include <cstring>
 7 #include <cmath>
 8 #include <vector>
 9 using namespace std;
10 
11 const int MAXN = 1e2 + 10;
12 const int INF = 0x3f3f3f3f;
13 struct P
14 {
15     double x, y;
16 }a[MAXN], b[MAXN];
17 bool vis[MAXN];
18 int lk[MAXN];
19 vector<int> G[MAXN];
20 
21 double get_dis(double x1, double y1, double x2, double y2)
22 {
23     return sqrt ((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2));
24 }
25 
26 bool DFS(int u)
27 {
28     for (int i=0; i<G[u].size (); ++i)
29     {
30         int v = G[u][i];
31         if (!vis[v])
32         {
33             vis[v] = true;
34             if (lk[v] == -1 || DFS (lk[v]))
35             {
36                 lk[v] = u;    return true;
37             }
38         }
39     }
40 
41     return false;
42 }
43 
44 int hungary(int n)
45 {
46     int res = 0;    memset (lk, -1, sizeof (lk));
47     for (int i=1; i<=n; ++i)
48     {
49         memset (vis, false, sizeof (vis));
50         if (DFS (i))    res++;
51     }
52 
53     return res;
54 }
55 
56 int main(void)        //UVA 10080 Gopher II
57 {
58 //    freopen ("UVA_10080.in", "r", stdin);
59 
60     int n, m, s, v;
61     while (scanf ("%d%d%d%d", &n, &m, &s, &v) == 4)
62     {
63         for (int i=1; i<=n; ++i)    scanf ("%lf%lf", &a[i].x, &a[i].y);
64         for (int i=1; i<=m; ++i)    scanf ("%lf%lf", &b[i].x, &b[i].y);
65         for (int i=1; i<=n; ++i)    G[i].clear ();
66         for (int i=1; i<=n; ++i)
67         {
68             for (int j=1; j<=m; ++j)
69             {
70                 if (get_dis (a[i].x, a[i].y, b[j].x, b[j].y) <= s * v)    G[i].push_back (j);
71             }
72         }
73 
74         printf ("%d\n", n - hungary (n));
75     }
76 
77     return 0;
78 }

 

二分图匹配(匈牙利算法) UVA 10080 Gopher II

标签:

原文地址:http://www.cnblogs.com/Running-Time/p/4652307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!