您的位置:首页 > 其它

洛谷 1991 无线通讯网 kruskal

2016-11-09 20:35 260 查看
一本正经的刷水题
传送门

题解

一本正经的刷水题,,

传送门

题解:

题目给定了卫星的数量,也就是说,最后整张图形成的联通块的数量要小于等于该数,手动连边,kruskal加边,满足条件时输出当前边并且exit

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#define y1 ajksch
struct edge{
int from;
int to;
double val;
};

const int maxn = 800000;
int s, p;
edge t[maxn];
int x1[maxn], y1[maxn];
int father[1000];
int tot = 0;
int getfather(int x) {
if (father[x] == x) return (x);
return (father[x] = getfather(father[x]));
}
bool cmp(edge aa, edge bb) {
return (aa.val < bb.val);
}
int main () {
scanf("%d %d", &s, &p);
for (int i = 1; i <= p; i++) {
scanf("%d %d", &x1[i], &y1[i]);
for (int j = 1; j < i; j++) {
tot++;
t[tot].from = i;
t[tot].to = j;
t[tot].val = (x1[i] - x1[j]) * (x1[i] - x1[j]) + (y1[i] - y1[j]) * (y1[i] - y1[j]);
}
}
std :: sort(t + 1, t + tot + 1, cmp);
for (int i = 1; i <= p; i++) father[i] = i;
int cur = p;
for (int i = 1; i <= tot; i++) {
int tx = getfather(t[i].from);
int ty = getfather(t[i].to);
if (tx == ty) continue;
cur--;
if (cur == s) {
printf("%.2lf", sqrt(t[i].val));
exit(0);
}
father[tx] = ty;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: