-
Notifications
You must be signed in to change notification settings - Fork 0
/
4386.cpp
61 lines (55 loc) · 1.09 KB
/
4386.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
int node[101];
vector<pair<float, pair <int, int> > >v;
vector<pair<float, float> >s;
int find_set(int x)
{
if(node[x] == x)
return x;
else
node[x] = find_set(node[x]);
return node[x];
}
void union_node(int u, int v)
{
int x = find_set(u);
int y = find_set(v);
node[x] = y;
}
int main()
{
int star;
float distance, length = 0;
cin >> star;
for (int i = 0; i < star; i++)
{
float x, y, distance;
cin >> x >> y;
s.push_back(make_pair(x, y));
}
for (int i = 0; i < s.size(); i++)
node[i] = i;
for (int i = 0; i < s.size(); i++)
{
for (int j = i + 1; j < s.size(); j++)
{
distance = sqrt(pow(s[i].first - s[j].first, 2) + pow(s[i].second - s[j].second, 2));
v.push_back(make_pair(distance, make_pair(i , j)));
}
}
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++)
{
if (find_set(v[i].second.first) != find_set(v[i].second.second))
{
union_node(v[i].second.first, v[i].second.second);
length += v[i].first;
}
}
printf("%0.2f", length);
return 0;
}