This repository was archived by the owner on May 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1552.cpp
More file actions
92 lines (88 loc) · 1.79 KB
/
Copy path1552.cpp
File metadata and controls
92 lines (88 loc) · 1.79 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <bits/stdc++.h>
using namespace std;
typedef struct vertice
{
int val;
double x,y;
vertice(double a, double b, int c):
x(a),y(b),val(c){}
}pessoa;
typedef struct aresta
{
int p1,p2;
double dist;
aresta(int a, int b, double c):
p1(a), p2(b), dist(c){}
//Usado para função de ordenação
bool operator<(const aresta &e) const {
return dist < e.dist;
}
}Aresta;
vector<pessoa> V;
vector<Aresta> E;
int find( int i)
{
int conj=i;
//acha topo que representa conj.Dij. que contem i
while(V[conj].val>-1 )
{
conj = V[conj].val;
}
/* Se o conj. encontrado nao for topo direto de i, e i tambem nao for topo de conj
atualiza topo de i para o conj encontrado*/
if(conj != V[i].val && conj != i) V[i].val=conj;
// printf(" %d Esta no conj. %d\n",i,conj );
return conj;
}
void uniao(int i, int j)
{
if(V[i].val <= V[j].val)
{
V[j].val = i;
if(V[i].val==V[j].val) V[i].val--;
}else
{
V[i].val = j;
}
}
int main(){
//como grafo eh completo, num de arestas - n(n-1)/2
int casos, numPessoas,numArestas;
double dist, distMinTotal=0,posX,posY;
cin >> casos;
while(casos--)
{
cin >> numPessoas;
numArestas = numPessoas*(numPessoas-1)/2;
for (int i = 0; i < numPessoas; ++i)
{
cin >>posX>>posY;
V.push_back(vertice(posX,posY,-1));
}
for (int i = 0; i < numPessoas; ++i)
{
for (int j = 0; j < numPessoas; ++j)
{
if(i!=j)
{
dist = sqrt(pow(V[j].x - V[i].x,2) + pow(V[j].y - V[i].y,2));
E.push_back(aresta(i,j,dist));
}
}
}
sort(E.begin(), E.end());
numPessoas--;
//////////Kruskal
for (int i = 0; i < numArestas && numPessoas > 0; ++i)
{
if(find(E[i].p1) != find(E[i].p2) )
{
uniao(E[i].p1,E[i].p2);
distMinTotal += E[i].dist;
numPessoas--;
}
}
distMinTotal /= 100;
printf("%.2lf\n",distMinTotal);
}
}