| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 10955 | Accepted: 3592 |
Description
Input
Output
Sample Input
1 2 4 0 100 0 300 0 600 150 750
Sample Output
212.13
Source
/*************************************************************************
> File Name: POJ2349.cpp
> Author: ALex
> Mail: 405045132@qq.com
> Created Time: 2015年01月25日 星期日 13时33分36秒
************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int father[555];
struct node
{
int x, y;
}point[555];
int find (int x)
{
if (father[x] == -1)
{
return x;
}
return father[x] = find (father[x]);
}
double dist(node a, node b)
{
return sqrt ((double)((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)));
}
int main ()
{
int t;
scanf("%d", &t);
while (t--)
{
int s, n;
scanf("%d%d", &s, &n);
double l = 0;
double r = 20000;
double mid;
for (int i = 1; i <= n; ++i)
{
scanf("%d%d", &point[i].x, &point[i].y);
}
double ret;
while (l + 1e-6 < r)
{
int ans = n;
memset (father, -1, sizeof(father));
mid = (l + r) / 2;
for (int i = 1; i <= n; ++i)
{
for (int j = i + 1; j <= n; ++j)
{
if (dist(point[i], point[j]) + 1e-6 <= mid)
{
int u = find (i);
int v = find (j);
if (u != v)
{
father[u] = v;
--ans;
}
}
}
}
if (ans <= s)
{
r = mid;
ret = mid;
}
else
{
l = mid;
}
}
printf("%.2f\n", ret);
}
return 0;
}/*************************************************************************
> File Name: poj2349.cpp
> Author: ALex
> Mail: 405045132@qq.com
> Created Time: 2015年01月25日 星期日 13时08分14秒
************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
double mat[555][555];
double lowc[555];
int pre[555];
bool vis[555];
double dist[555];
int n, s;
struct node
{
int x, y;
}point[555];
void prim (int n, int s)
{
memset (vis, 0, sizeof(vis));
for (int i = 2; i <= n; ++i)
{
lowc[i] = mat[1][i];
pre[i] = 1;
}
pre[1] = -1;
vis[1] = 1;
lowc[1] = 0;
int cnt = 0;
for (int i = 2; i <= n; ++i)
{
double minc = 0x3f3f3f3f;
int p;
for (int j = 1; j <= n; ++j)
{
if (!vis[j] && minc > lowc[j])
{
minc = lowc[j];
p = j;
}
}
dist[cnt++] = minc;
vis[p] = 1;
for (int j = 1; j <= n; ++j)
{
if (!vis[j] && mat[p][j] < lowc[j])
{
lowc[j] = mat[p][j];
pre[j] = p;
}
}
}
sort (dist, dist + cnt);
printf("%.2f\n", dist[cnt - s]);
}
double Dist (node a, node b)
{
return sqrt (double ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)));
}
int main ()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &s, &n);
for (int i = 1; i <= n; ++i)
{
scanf("%d%d", &point[i].x, &point[i].y);
}
memset (mat, 0x3f3f3f3f, sizeof(mat));
for (int i = 1; i <= n; ++i)
{
for (int j = i + 1; j <= n; ++j)
{
mat[i][j] = mat[j][i] = Dist(point[i], point[j]);
}
}
prim(n, s);
}
return 0;
}
原文:http://blog.csdn.net/guard_mine/article/details/43114095