uva 10369 - Arctic Network(最小生成树)

来源:互联网 发布:mysql 创建联合索引 编辑:程序博客网 时间:2024/06/05 02:38

题目链接:10369 - Arctic Network


题目大意:有n个前哨站,现在有两种通信技术去连接哨站,卫星技术和无线电技术,通过卫星技术需要一个卫星信道,现在已知有m个信道,问说连接所有哨站时,通过无线电技术连接的最长距离的最小值。(每个哨站已经设立好了接受装置,卫星通信无需考虑距离)


解题思路:最短路问题,无需考虑联通整张图的最小距离,只需要记录下第n - m - 1个加入的边,输出保留两位小数。


#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;const int N = 505;const int M = 250005;struct state {double dis;int l;int r;}s[M];;int n, m,  cnt, f[N], x[N], y[N];double ans;int getfather(int c) {return c == f[c] ? c : f[c] = getfather(f[c]);}void init() {cnt = 0;scanf("%d%d", &m, &n);for (int i = 0; i < n; i++) {f[i] = i; // Init f;scanf("%d%d", &x[i], &y[i]);for (int j = 0; j < i; j++) {s[cnt].dis = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * ( y[i] - y[j]));s[cnt].l = i;s[cnt].r = j;cnt++;}}}bool cmp(const state& a, const state& b) {return b.dis - a.dis > 1e-9;}void kruskal() {sort(s, s + cnt, cmp);;int t = n - m - 1;for (int i = 0; i < cnt; i++) {int p = getfather(s[i].l), q = getfather(s[i].r);if (p != q) {ans = s[i].dis;if (t) {t--;f[q] = p;}elsereturn ;}}}int main () {int cas;scanf("%d", &cas);while (cas--) {init();kruskal();printf("%.2lf\n", ans);}return 0;}


原创粉丝点击