-
Notifications
You must be signed in to change notification settings - Fork 0
/
1552.cpp
77 lines (57 loc) · 1.18 KB
/
1552.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//1552
#include <bits/stdc++.h>
using namespace std;
#define DIST first
#define VERTEX second
typedef pair<double,int> ii;
double dist(int x1, int y1, int x2, int y2)
{
return sqrt((pow((x2 - x1), 2) + pow((y2 - y1),2)));
}
double prim(vector<ii> graph[], int N)
{
priority_queue<ii, vector<ii>, greater<ii>> next;
vector<bool> visited(N, false);
double cost = 0;
next.push(ii(0, 0));
while(!next.empty())
{
ii u = next.top();
next.pop();
if(visited[u.VERTEX])
continue;
cost += u.DIST;
visited[u.VERTEX] = true;
for(int j = 0; j < (int)graph[u.VERTEX].size(); j++)
{
ii v = graph[u.VERTEX][j];
if(!visited[v.VERTEX])
next.push(v);
}
}
return cost;
}
int main()
{
int casos;
cin >> casos;
while(casos--)
{
int pessoas, x[502] = {}, y[502] = {};
cin >> pessoas;
for(int i = 0; i < pessoas; i++)
cin >> x[i] >> y[i];
vector<ii> graph[pessoas];
for(int u = 0; u < pessoas; u++)
for(int v = 0; v < pessoas; v++)
{
if(u != v)
{
double d = dist(x[u], y[u], x[v], y[v]);
graph[u].push_back(ii(d, v));
}
}
double comp = prim(graph, pessoas)/100.0;
printf("%.2lf\n",comp);
}
}