hdu 5046 Airport (重复覆盖)
2015-10-09 15:33
369 查看
Airport
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1481 Accepted Submission(s): 469
Problem Description
The country of jiuye composed by N cites. Each city can be viewed as a point in a two- dimensional plane with integer coordinates (x,y). The distance between city i and city j is defined by dij = |xi - xj| + |yi -
yj|. jiuye want to setup airport in K cities among N cities. So he need your help to choose these K cities, to minimize the maximum distance to the nearest airport of each city. That is , if we define di(1 ≤ i ≤ N ) as the distance from
city i to the nearest city with airport. Your aim is to minimize the value max{di|1 ≤ i ≤ N }. You just output the minimum.
Input
The first line of the input is T (1 ≤ T ≤ 100), which stands for the number of test cases you need to solve.
The first line of each case contains two integers N ,K (1 ≤ N ≤ 60,1 ≤ K ≤ N ),as mentioned above.
The next N lines, each lines contains two integer xi and yi (-109 ≤ xi, yi ≤ 109), denote the coordinates of city i.
Output
For each test case, print a line “Case #t: ”(without quotes, t means the index of the test case) at the beginning. Then a single integer means the minimum.
Sample Input
2 3 2 0 0 4 0 5 1 4 2 0 3 1 0 3 0 8 9
Sample Output
Case #1: 2 Case #2: 4
Source
2014 ACM/ICPC Asia Regional Shanghai Online
二分 + 重复覆盖
/*====================================================== # Author: whai # Last modified: 2015-10-09 11:30 # Filename: hdu5046.cpp ======================================================*/ #include <iostream> #include <cstdio> #include <vector> #include <algorithm> #include <cstring> #include <string> #include <cmath> #include <set> #include <map> using namespace std; #define LL __int64 #define PB push_back #define P pair<int, int> #define X first #define Y second const int MAXM = 65; const int MAXN = 65; const int N = MAXN * MAXM; const int INF = 0x3f3f3f3f; struct DLX { int n, m, size; int U , D , R , L , row , col ; int H[MAXN], S[MAXM]; int ans[MAXN], ans_cnt; void init(int _n, int _m) { ans_cnt = INF; n = _n; m = _m; for (int i = 0; i <= m; i++) { S[i] = 0; U[i] = D[i] = i; L[i] = i - 1; R[i] = i + 1; } R[m] = 0; L[0] = m; size = m; for (int i = 1; i <= n; i++)H[i] = -1; } void link(int r, int c) { ++S[col[++size] = c]; row[size] = r; D[size] = D[c]; U[D[c]] = size; U[size] = c; D[c] = size; if (H[r] < 0)H[r] = L[size] = R[size] = size; else { R[size] = R[H[r]]; L[R[H[r]]] = size; L[size] = H[r]; R[H[r]] = size; } } void remove(int c) { for (int i = D[c]; i != c; i = D[i]) L[R[i]] = L[i], R[L[i]] = R[i]; } void resume(int c) { for (int i = U[c]; i != c; i = U[i]) L[R[i]] = R[L[i]] = i; } bool v[MAXM]; int f() { int ret = 0; for (int c = R[0]; c != 0; c = R[c])v[c] = true; for (int c = R[0]; c != 0; c = R[c]) if (v[c]) { ret++; v[c] = false; for (int i = D[c]; i != c; i = D[i]) for (int j = R[i]; j != i; j = R[j]) v[col[j]] = false; } return ret; } //注意这些可以加入剪枝优化的 /*void dance(int d) { if (d + f() >= ans_cnt) return; if (R[0] == 0) { if (d < ans_cnt) ans_cnt = d; return ; } int c = R[0]; for (int i = R[0]; i != 0; i = R[i]) if (S[i] < S[c]) c = i; for (int i = D[c]; i != c; i = D[i]) { remove(i); //ans[d] = row[i]; for (int j = R[i]; j != i; j = R[j]) remove(j); dance(d + 1); for (int j = L[i]; j != i; j = L[j]) resume(j); resume(i); } }*/ bool dance(int d, int limit) { if (d + f() > limit) return false; if (R[0] == 0) return d <= limit; int c = R[0]; for (int i = R[0]; i != 0; i = R[i]) if (S[i] < S[c]) c = i; for (int i = D[c]; i != c; i = D[i]) { remove(i); for (int j = R[i]; j != i; j = R[j]) remove(j); if(dance(d + 1, limit)) return true; for (int j = L[i]; j != i; j = L[j]) resume(j); resume(i); } return false; } } dlx; P a ; LL _abs(int x) { if(x < 0) return -x; return x; } LL dis(P a, P b) { return _abs(a.X - b.X) + _abs(a.Y - b.Y); } bool ok(LL d, int n, int k) { dlx.init(n, n); for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { if(dis(a[i], a[j]) <= d) { dlx.link(i + 1, j + 1); } } } return dlx.dance(0, k); } void gao(int n, int k) { LL L = 0, R = 4 * 1e9, ans = 4 * 1e9; while(L < R) { LL mid = (L + R) >> 1; if(!ok(mid, n, k)) { L = mid + 1; } else { ans = min(ans, mid); R = mid; } } if(ok(L, n, k)) ans = min(ans, L); if(ok(R, n, k)) ans = min(ans, R); printf("%I64d\n", ans); } int main() { int T; int cas = 0; scanf("%d", &T); while(T--) { int n, k; scanf("%d%d", &n, &k); for(int i = 0; i < n; ++i) { scanf("%d%d", &a[i].X, &a[i].Y); } printf("Case #%d: ", ++cas); gao(n, k); } return 0; }
相关文章推荐
- sendmail邮件服务器
- Epic Fail of a Genie
- 等待执行:time.sleep()和implicitly_wait()
- Scala学习笔记12【继承、抽象类、trait实战】
- 游戏中AI的行为树(Behaviour Tree in Game AI)
- maven编译时出现 There are test failures
- 【CF】323 Div2. D. Once Again...
- GridBagConstraints(网格组布局管理器的使用)
- CodeForces - 463E Caisa and Tree (dfs+素因子分解)
- HDU 4300 Clairewd’s message
- GridBagConstraints(网格组布局管理器的使用)
- HDU 2674 N!Again
- Could not reliably determine the server's fully qualified domain name
- AIX 6.1 硬件管理
- 5029 Relief grain 树链剖分+离线标记法
- 深入解读Quartz的原理(创建Scheduler对象容器,并配置JobDetail和Trigger对象;配置强大时间调度策略,可以研究专门的CronTrigger )
- 新版fai批量安装部署debian
- 进程——wait函数
- grails之二级联动后的文本框内容的切换
- Jenkins系列之—04 email-ext邮件通知模板