首页 > 代码库 > HDU 2389 Rain on your Parade

HDU 2389 Rain on your Parade

http://acm.hdu.edu.cn/showproblem.php?pid=2389

题意:给暴风雨到来的时刻,m个人的坐标和单位速度,和n个救生衣的坐标。每个救生衣只能匹配一个人,求最多有多少人可以得到救生衣。

题解:典型二分图最大匹配题型。因为点比较多,使用Hopcroft-Karp算法。讲解:http://blog.csdn.net/wall_f/article/details/8248373

        http://www.cnblogs.com/-sunshine/archive/2012/08/30/2664242.html

  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <cmath>
  6 #include <string>
  7 #include <vector>
  8 #include <list>
  9 #include <map>
 10 #include <queue>
 11 #include <stack>
 12 #include <bitset>
 13 #include <algorithm>
 14 #include <numeric>
 15 #include <functional>
 16 #include <set>
 17 #include <fstream>
 18 
 19 using namespace std;
 20 const int maxn=3010;
 21 const int INF=1<<28;
 22 bool used[maxn];
 23 struct guest{
 24     int x;
 25     int y;
 26     int speed;
 27 }gu[maxn];
 28 struct umbrellas{
 29     int x;
 30     int y;
 31 }um[maxn];
 32 int mymap[maxn][maxn];
 33 int dis;
 34 int cx[maxn],cy[maxn],dx[maxn],dy[maxn];
 35 int V;
 36 int ca,t,m,n;
 37 
 38 bool searchpath()
 39 {
 40     queue<int> Q;
 41     dis=INF;
 42     memset(dx, -1, sizeof(dx));
 43     memset(dy, -1, sizeof(dy));
 44     for (int i=0; i<V; i++) {
 45         if (cx[i]==-1) {
 46             Q.push(i);
 47             dx[i]=0;
 48         }
 49     }
 50     while (!Q.empty()) {
 51         int u=Q.front();
 52         Q.pop();
 53         if (dx[u]>dis) {
 54             break;
 55         }
 56         for (int v=0; v<V; v++) {
 57             if (mymap[u][v]&&dy[v]==-1) {
 58                 dy[v]=dx[u]+1;
 59                 if (cy[v]==-1) {
 60                     dis=dy[v];
 61                 }
 62                 else {
 63                     dx[cy[v]]=dy[v]+1;
 64                     Q.push(cy[v]);
 65                 }
 66             }
 67         }
 68     }
 69     return dis!=INF;
 70 }
 71 
 72 int findpath(int u)
 73 {
 74     for (int v=0; v<n; v++) {
 75         if (!used[v]&&mymap[u][v]&&dy[v]==dx[u]+1) {
 76             used[v]=1;
 77             if (cy[v]!=-1&&dy[v]==dis) {
 78                 continue;
 79             }
 80             if (cy[v]==-1||findpath(cy[v])) {
 81                 cy[v]=u;
 82                 cx[u]=v;
 83                 return 1;
 84             }
 85         }
 86     }
 87     return 0;
 88 }
 89 
 90 void binary_match()
 91 {
 92     int res=0;
 93     memset(cx, -1, sizeof(cx));
 94     memset(cy, -1, sizeof(cy));
 95     while (searchpath()) {
 96         memset(used,0,sizeof(used));
 97         for (int i=0; i<V; i++) {
 98             if (cx[i]==-1) {
 99                 res+=findpath(i);
100             }
101         }
102     }
103     printf("%d\n",res);
104 }
105 int main()
106 {
107     //freopen("/Users/apple/Desktop/hdu 2389/hdu 2389/in", "r", stdin);
108     //freopen("/Users/apple/Desktop/hdu 2389/hdu 2389/out", "w", stdout);
109 
110     scanf("%d",&ca);
111     for (int num=1; num<=ca; num++) {
112         scanf("%d",&t);
113         scanf("%d",&m);
114         for (int i=0; i<m; i++) {
115             scanf("%d%d%d",&gu[i].x,&gu[i].y,&gu[i].speed);
116         }
117         scanf("%d",&n);
118         for (int i=0; i<n; i++) {
119             scanf("%d%d",&um[i].x,&um[i].y);
120         }
121         memset(mymap, 0, sizeof(mymap));
122         V=m;
123         for (int i=0; i<m; i++) {
124             for (int j=0; j<n; j++) {
125                 if (pow(gu[i].x-um[j].x,2)+pow(gu[i].y-um[j].y,2)<=t*t*gu[i].speed*gu[i].speed) {
126                     mymap[i][j]=1;
127                 }
128             }
129         }
130         printf("Scenario #%d:\n",num);
131         binary_match();
132         puts("");
133     }
134     return 0;
135 }