HDU2295 Radar —— Dancing Links 可重复覆盖
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2295
Radar
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4106 Accepted Submission(s): 1576
Problem Description
N cities of the Java Kingdom need to be covered by radars for being in a state of war. Since the kingdom has M radar stations but only K operators, we can at most operate K radars. All radars have the same circular coverage with a radius of R. Our goal is to
minimize R while covering the entire city with no more than K radars.
Input
The input consists of several test cases. The first line of the input consists of an integer T, indicating the number of test cases. The first line of each test case consists of 3 integers: N, M, K, representing the number of cities, the number of radar stations
and the number of operators. Each of the following N lines consists of the coordinate of a city.
Each of the last M lines consists of the coordinate of a radar station.
All coordinates are separated by one space.
Technical Specification
1. 1 ≤ T ≤ 20
2. 1 ≤ N, M ≤ 50
3. 1 ≤ K ≤ M
4. 0 ≤ X, Y ≤ 1000
Each of the last M lines consists of the coordinate of a radar station.
All coordinates are separated by one space.
Technical Specification
1. 1 ≤ T ≤ 20
2. 1 ≤ N, M ≤ 50
3. 1 ≤ K ≤ M
4. 0 ≤ X, Y ≤ 1000
Output
For each test case, output the radius on a single line, rounded to six fractional digits.
Sample Input
1
3 3 2
3 4
3 1
5 4
1 1
2 2
3 3
Sample Output
2.236068
Source
题解:
超时方法:
1.对于DLX的矩阵:行代表着雷达与城市的距离, 列代表着城市。矩阵大小250*50。
2.Dancing跳起来,当R[0]==0时, 取当前所选行中,距离的最大值dis(这样才能覆盖掉所有城市),然后再更新答案ans,ans = min(ans, dis)。
3.结果矩阵有点大, 超时了。
4.错误思想分析:把雷达与城市的距离作为行,实际上是太明智的。因为题目说明了每个雷达的接收半径是相同的,而以上方法选出来的每个雷达的接收半径是相异的,然后又再取最大值,那为何不每次都取最大值(相同值)呢? 如果取相同值,那么行就是雷达,列就是城市,矩阵的大小就减少了。但是又怎么确定雷达的接收半径呢?如下:
正确方法:
1.雷达作为行, 城市作为列。
2.二分雷达的接收范围,每次二分都:根据接收半径更新矩阵中所含的元素,然后再进行一次Dance(),如果能覆盖掉所有城市,则缩小半径,否则扩大半径。
超时方法:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const int INF = 2e9; 16 const int MAXN = 50+10; 17 const int MAXM = 50+10; 18 const int maxnode = 1e5+10; 19 20 double city[MAXN][2], radar[MAXN][2]; 21 double r[MAXN*MAXM]; 22 int k; 23 24 struct DLX 25 { 26 int n, m, size; 27 int U[maxnode], D[maxnode], L[maxnode], R[maxnode], Row[maxnode], Col[maxnode]; 28 int H[MAXN*MAXM], S[MAXN*MAXM]; 29 double ansd, ans[MAXN*MAXM]; 30 31 void init(int _n, int _m) 32 { 33 n = _n; 34 m = _m; 35 for(int i = 0; i<=m; i++) 36 { 37 S[i] = 0; 38 U[i] = D[i] = i; 39 L[i] = i-1; 40 R[i] = i+1; 41 } 42 R[m] = 0; L[0] = m; 43 size = m; 44 for(int i = 1; i<=n; i++) H[i] = -1; 45 } 46 47 void Link(int r, int c) 48 { 49 size++; 50 Row[size] = r; 51 Col[size] = c; 52 S[Col[size]]++; 53 D[size] = D[c]; 54 U[D[c]] = size; 55 U[size] = c; 56 D[c] = size; 57 if(H[r]==-1) H[r] = L[size] = R[size] = size; 58 else 59 { 60 R[size] = R[H[r]]; 61 L[R[H[r]]] = size; 62 L[size] = H[r]; 63 R[H[r]] = size; 64 } 65 } 66 67 void remove(int c) 68 { 69 for(int i = D[c]; i!=c; i = D[i]) 70 L[R[i]] = L[i], R[L[i]] = R[i]; 71 } 72 73 bool v[MAXM]; 74 int f() 75 { 76 int ret = 0; 77 for(int c = R[0]; c!=0; c = R[c]) 78 v[c] = true; 79 for(int c = R[0]; c!=0; c = R[c]) 80 if(v[c]) 81 { 82 ret++; 83 v[c] = false; 84 for(int i = D[c]; i!=c; i = D[i]) 85 for(int j = R[i]; j!=i; j = R[j]) 86 v[Col[j]] = false; 87 } 88 return ret; 89 } 90 91 void resume(int c) 92 { 93 for(int i = U[c]; i!=c; i = U[i]) 94 L[R[i]] = R[L[i]] = i; 95 } 96 97 void Dance(int d) 98 { 99 if(d+f()>k) return; 100 if(R[0]==0) 101 { 102 double tmp = -1.0; 103 for(int i = 0; i<d; i++) 104 tmp = max(tmp, ans[i]); 105 ansd = min(tmp, ansd); 106 return; 107 } 108 109 int c = R[0]; 110 for(int i = R[0]; i!=0; i = R[i]) 111 if(S[i]<S[c]) 112 c = i; 113 for(int i = D[c]; i!=c; i = D[i]) 114 { 115 ans[d] = r[Row[i]]; 116 remove(i); 117 for(int j = R[i]; j!=i; j = R[j]) remove(j); 118 Dance(d+1); 119 for(int j = L[i]; j!=i; j = L[j]) resume(j); 120 resume(i); 121 } 122 } 123 }; 124 125 double dis(double x1, double y1, double x2, double y2) 126 { 127 return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 128 } 129 130 DLX dlx; 131 int main() 132 { 133 int T; 134 int n, m; 135 scanf("%d", &T); 136 while(T--) 137 { 138 scanf("%d%d%d", &n, &m, &k); 139 dlx.init(n*m, n); 140 for(int i = 1; i<=n; i++) 141 scanf("%lf%lf",&city[i][0], &city[i][1]); 142 for(int i = 1; i<=m; i++) 143 scanf("%lf%lf",&radar[i][0], &radar[i][1]); 144 145 for(int i = 1; i<=m; i++) 146 for(int j = 1; j<=n; j++) 147 { 148 double tmp = dis(radar[i][0], radar[i][1], city[j][0], city[j][1]); 149 r[(i-1)*n+j] = tmp; 150 for(int t = 1; t<=n; t++) 151 if(dis(radar[i][0], radar[i][1], city[t][0], city[t][1])<=tmp) 152 dlx.Link((i-1)*n+j, t); 153 } 154 dlx.ansd = 1.0*INF; 155 dlx.Dance(0); 156 printf("%.6f\n", dlx.ansd); 157 } 158 return 0; 159 }
正确方法:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const int INF = 2e9; 16 const double EPS = 1e-8; 17 const int MAXN = 50+10; 18 const int MAXM = 50+10; 19 const int maxnode = 1e5+10; 20 21 double city[MAXN][2], radar[MAXN][2]; 22 double r[MAXN*MAXM]; 23 int k; 24 25 struct DLX 26 { 27 int n, m, size; 28 int U[maxnode], D[maxnode], L[maxnode], R[maxnode], Row[maxnode], Col[maxnode]; 29 int H[MAXN*MAXM], S[MAXN*MAXM]; 30 double ansd, ans[MAXN*MAXM]; 31 32 void init(int _n, int _m) 33 { 34 n = _n; 35 m = _m; 36 for(int i = 0; i<=m; i++) 37 { 38 S[i] = 0; 39 U[i] = D[i] = i; 40 L[i] = i-1; 41 R[i] = i+1; 42 } 43 R[m] = 0; L[0] = m; 44 size = m; 45 for(int i = 1; i<=n; i++) H[i] = -1; 46 } 47 48 void Link(int r, int c) 49 { 50 size++; 51 Row[size] = r; 52 Col[size] = c; 53 S[Col[size]]++; 54 D[size] = D[c]; 55 U[D[c]] = size; 56 U[size] = c; 57 D[c] = size; 58 if(H[r]==-1) H[r] = L[size] = R[size] = size; 59 else 60 { 61 R[size] = R[H[r]]; 62 L[R[H[r]]] = size; 63 L[size] = H[r]; 64 R[H[r]] = size; 65 } 66 } 67 68 void remove(int c) 69 { 70 for(int i = D[c]; i!=c; i = D[i]) 71 L[R[i]] = L[i], R[L[i]] = R[i]; 72 } 73 74 void resume(int c) 75 { 76 for(int i = U[c]; i!=c; i = U[i]) 77 L[R[i]] = R[L[i]] = i; 78 } 79 80 bool v[MAXM]; 81 int f() 82 { 83 int ret = 0; 84 for(int c = R[0]; c!=0; c = R[c]) 85 v[c] = true; 86 for(int c = R[0]; c!=0; c = R[c]) 87 if(v[c]) 88 { 89 ret++; 90 v[c] = false; 91 for(int i = D[c]; i!=c; i = D[i]) 92 for(int j = R[i]; j!=i; j = R[j]) 93 v[Col[j]] = false; 94 } 95 return ret; 96 } 97 98 bool Dance(int d) 99 { 100 if(d+f()>k) return false; 101 if(R[0]==0) return true; 102 103 int c = R[0]; 104 for(int i = R[0]; i!=0; i = R[i]) 105 if(S[i]<S[c]) c = i; 106 for(int i = D[c]; i!=c; i = D[i]) 107 { 108 remove(i); 109 for(int j = R[i]; j!=i; j = R[j]) remove(j); 110 if(Dance(d+1))return true; 111 for(int j = L[i]; j!=i; j = L[j]) resume(j); 112 resume(i); 113 } 114 return false; 115 } 116 }; 117 118 double dis(double x1, double y1, double x2, double y2) 119 { 120 return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 121 } 122 123 DLX dlx; 124 int main() 125 { 126 int T; 127 int n, m; 128 scanf("%d", &T); 129 while(T--) 130 { 131 scanf("%d%d%d", &n, &m, &k); 132 for(int i = 1; i<=n; i++) 133 scanf("%lf%lf",&city[i][0], &city[i][1]); 134 for(int i = 1; i<=m; i++) 135 scanf("%lf%lf",&radar[i][0], &radar[i][1]); 136 137 double l = 0.0, r = 2000.0; 138 while(l+EPS<=r) 139 { 140 double mid = (l+r)/2; 141 dlx.init(m, n); 142 for(int i = 1; i<=m; i++) 143 for(int j = 1; j<=n; j++) 144 if(dis(radar[i][0], radar[i][1], city[j][0], city[j][1])<=mid) 145 dlx.Link(i, j); 146 if(dlx.Dance(0)) 147 r = mid - EPS; 148 else 149 l = mid + EPS; 150 } 151 printf("%.6lf\n",l); 152 } 153 return 0; 154 }