【HDOJ】2389 Rain on your Parade

读题显然是二分图匹配,看成guest与umbrella的匹配。匈牙利果断TLE了,其实时间卡的相当紧。HK过的,750ms。

  1 /* 2389 */
  2 #include <iostream>
  3 #include <string>
  4 #include <map>
  5 #include <queue>
  6 #include <set>
  7 #include <stack>
  8 #include <vector>
  9 #include <algorithm>
 10 #include <cstdio>
 11 #include <cmath>
 12 #include <ctime>
 13 #include <cstring>
 14 #include <climits>
 15 #include <cctype>
 16 using namespace std;
 17 
 18 typedef struct {
 19     int v, next;
 20 } Edge_t;
 21 
 22 const int maxn = 3005;
 23 
 24 Edge_t E[maxn*maxn];
 25 int head[maxn], L;
 26 int dx[maxn], dy[maxn];
 27 int xpre[maxn], ypre[maxn];
 28 int gx[maxn], gy[maxn], gs[maxn];
 29 bool visit[maxn];
 30 int rt, n, m;
 31 int ans, dis;
 32     
 33 void init() {
 34     L = 0;
 35     memset(head, -1, sizeof(head));
 36     memset(xpre, -1, sizeof(xpre));
 37     memset(ypre, -1, sizeof(ypre));
 38 }
 39 
 40 void addEdge(int u, int v) {
 41     E[L].v = v;
 42     E[L].next = head[u];
 43     head[u] = L++;
 44 }
 45 
 46 bool bfs() {
 47     int i, j, k;
 48     int u, v;
 49     queue<int> Q;
 50     
 51     memset(dx, -1, sizeof(dx));
 52     memset(dy, -1, sizeof(dy));
 53     dis = INT_MAX;
 54     
 55     for (i=0; i<m; ++i) {
 56         if (xpre[i] == -1) {
 57             Q.push(i);
 58             dx[i] = 0;
 59         }
 60     }
 61     
 62     while (!Q.empty()) {
 63         u = Q.front();
 64         Q.pop();
 65         if (dx[u] > dis)
 66             break;
 67         for (i=head[u]; i!=-1; i=E[i].next) {
 68             v = E[i].v;
 69             if (dy[v] == -1) {
 70                 dy[v] = dx[u] + 1;
 71                 if (ypre[v] == -1) {
 72                     dis = dy[v];
 73                 } else {
 74                     dx[ypre[v]] = dy[v] + 1;
 75                     Q.push(ypre[v]);
 76                 }
 77             }
 78         }
 79     }
 80     
 81     return dis!=INT_MAX;
 82 }
 83 
 84 int dfs(int u) {
 85     int i, v;
 86     // Edge_t e;
 87     
 88     for (i=head[u]; i!=-1; i=E[i].next) {
 89         v = E[i].v;
 90         if (!visit[v] && dy[v]==dx[u]+1) {
 91             visit[v] = true;
 92             if (ypre[v]!=-1 && dy[v]==dis)
 93                 continue;
 94             if (ypre[v]==-1 || dfs(ypre[v])) {
 95                 xpre[u] = v;
 96                 ypre[v] = u;
 97                 return 1;
 98             }
 99         }
100     }
101     
102     return 0;
103 }
104 
105 int HK() {
106     int ret = 0;
107     
108     while (bfs()) {
109         memset(visit, false, sizeof(visit));
110         for (int i=0; i<m; ++i)
111             if (xpre[i] == -1)
112                 ret += dfs(i);
113     }
114     return ret;
115 }
116 
117 int main() {
118     int i, j, k;
119     int t, tt;
120     int x, y;
121     
122     #ifndef ONLINE_JUDGE
123         freopen("data.in", "r", stdin);
124         freopen("data.out", "w", stdout);
125     #endif
126     
127     scanf("%d", &tt);
128     for (t=1; t<=tt; ++t) {
129         scanf("%d", &rt);
130         scanf("%d", &m);
131         init();
132         for (i=0; i<m; ++i)
133             scanf("%d %d %d", &gx[i], &gy[i], &gs[i]);
134         scanf("%d", &n);
135         for (i=0; i<n; ++i) {
136             scanf("%d %d", &x, &y);
137             for (j=0; j<m; ++j) {
138                 dis = gs[i] * rt;
139                 if ((x-gx[j])*(x-gx[j])+(y-gy[j])*(y-gy[j]) <= dis*dis)
140                     addEdge(j, i);
141             }
142         }
143         ans = HK();
144         printf("Scenario #%d:\n%d\n\n", t, ans);
145     }
146     
147     #ifndef ONLINE_JUDGE
148         printf("%d\n", (int)clock());
149     #endif
150     
151     return 0;
152 }

 

posted on 2015-04-16 16:01  Bombe  阅读(212)  评论(0编辑  收藏  举报

导航