HDU 3585 Maximum Shortest Distance 最大团 二分答案

来源:互联网 发布:word文档打开是空白mac 编辑:程序博客网 时间:2024/06/08 06:32

%%% http://www.cnblogs.com/zhj5chengfeng/archive/2013/07/29/3224092.html

平面上有N个点,确定k个点使其中各点对距离的最小值最大。有多组数据。

这个最优化问题本身似乎不好解决,所以还是二分一下吧。。

如果已知距离的最小值mid,判定是否存在k个点且两两距离均超过mid。

将距离超过mid的点连起来求一下最大团即可。


二分的时候跪了。。

判断二分结束的条件r - l > 1e-4,写1e-3就WA了。。

这个故事告诉我们不要吝啬。。开够保证正确性为好。。

人生第一次vim & g++ & gdb 全成就达成。

#include <cstdio>using namespace std;const int N = 65;int ans, f[N], set[N][N], a[N][N];bool dfs(int sz, int dep) {if (!sz) if (dep > ans) return ans = dep, 1;else return 0;for (int i = 1; i <= sz; i++) {if (dep + sz - i + 1 <= ans) return 0;int u = set[dep][i];if (dep + f[u] <= ans) return 0;int num = 0;for (int j = i + 1; j <= sz; j++)if (a[u][set[dep][j]])set[dep + 1][++num] = set[dep][j];if (dfs(num, dep + 1)) return 1;}return 0;}struct Point {double x, y;friend double dis(const Point &a, const Point &b) {return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}} p[N];int main() {int n, m, i, j, sz;while (scanf("%d%d", &n, &m) == 2) {for (i = 1; i <= n; i++)scanf("%lf%lf", &p[i].x, &p[i].y);double l = 0, r = 20000;while (r - l > 1e-4) {double mid = (l + r) / 2;ans = 0;for (i = 1; i < n; i++)for (j = i + 1; j <= n; j++)a[i][j] = a[j][i] = (dis(p[i], p[j]) >= mid * mid);for (i = n; i; i--) {sz = 0;for (j = i + 1; j <= n; j++)if (a[i][j])set[1][++sz] = j;dfs(sz, 1);f[i] = ans;}if (ans >= m) l = mid;else r = mid;}printf("%.2lf\n", l);}return 0;}


maximum shortest distance

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1234    Accepted Submission(s): 403


Problem Description
There are n points in the plane. Your task is to pick k points (k>=2), and make the closest points in these k points as far as possible.
 

Input
For each case, the first line contains two integers n and k. The following n lines represent n points. Each contains two integers x and y. 2<=n<=50, 2<=k<=n, 0<=x,y<10000.
 

Output
For each case, output a line contains a real number with precision up to two decimal places.

 

Sample Input
3 20 010 00 20
 

Sample Output
22.36

0 0