# Arctic Network（POJ 2349）（最小生成树Kruskal）

http://acm.hust.edu.cn/vjudge/problem/17341

(From Waterloo University 2002)

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <queue>

using namespace std;

struct point{
int x, y;
};

point Point[510];

double Distance(point a, point b)
{
double d2 = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
return sqrt (d2);
}

struct Edge
{
int s, e;
double w;
Edge(int ss, int ee, double ww) : s(ss), e(ee), w(ww) {}
Edge() {}
bool operator < (const Edge & e1) const {
return w < e1.w;
}
};

vector<Edge> edges;
vector<int> parent;

int GetRoot(int a)
{
if (parent[a] == a) return a;
parent[a] = GetRoot (parent[a]);
return parent[a];
}

void Merge(int a, int b)
{
int p1 = GetRoot (a);
int p2 = GetRoot (b);
if (p1 == p2) return;
parent[p2] = p1;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen ("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int n;
scanf ("%d", &n);
while (n--) {
int s, p;
scanf ("%d%d", &s, &p);
parent.clear();
edges.clear();
for (int i = 0; i < p; i++) parent.push_back(i);
//printf("sdfi");
for (int i = 0; i < p; i++) {
scanf ("%d%d", &Point[i].x, &Point[i].y);
for (int j = 0; j < i; j++) {
//cout << Distance (Point[i], Point[j]) << endl;
edges.push_back(Edge(i, j, Distance (Point[i], Point[j])));
edges.push_back(Edge(j, i, Distance (Point[i], Point[j])));
}
}
sort (edges.begin(), edges.end());
int done = 0;
double d = 0;
for (int i = 0; i < edges.size(); i++) {
if (GetRoot (edges[i].s) != GetRoot (edges[i].e)) {
Merge (edges[i].s, edges[i].e);
done++;
d = edges[i].w;
}
if (done == p - s) break;
}
printf ("%.2f\n", d);
}
return 0;
}


©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客