【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 }
时间: 2024-10-07 12:20:28

【HDOJ】2389 Rain on your Parade的相关文章

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

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/201

【HDOJ】3509 Buge&#39;s Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl

【HDOJ】1818 It&#39;s not a Bug, It&#39;s a Feature!

状态压缩+优先级bfs. 1 /* 1818 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <algorithm> 8 using namespace std; 9 10 #define MAXM 105 11 12 typedef struct {

【HDOJ】2424 Gary&#39;s Calculator

大数乘法加法,直接java A了. 1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 int n; 8 int i, j, k, tmp; 9 int top; 10 boolean flag; 11 int t

【HDOJ】2425 Hiking Trip

优先级队列+BFS. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 25 8 9 typedef struct node_st { 10 int x, y, t; 11 node_st() {} 12 node_st(int xx, int yy, int tt)

【HDOJ】1686 Oulipo

kmp算法. 1 #include <cstdio> 2 #include <cstring> 3 4 char src[10005], des[1000005]; 5 int next[10005], total; 6 7 void kmp(char des[], char src[]){ 8 int ld = strlen(des); 9 int ls = strlen(src); 10 int i, j; 11 12 total = i = j = 0; 13 while (