-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResgate.cc
185 lines (156 loc) · 4.91 KB
/
Resgate.cc
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* José Guilherme de Castro Rodrigues
* Matrícula: 651201
* Trabalho Prático 2: Questão 2.
* Cada pessoa é um vértice. Toda pessoa é ligada a outra por uma possível aresta cujo peso é a distância.
*/
#include <iostream>
#include <cstdint>
#include <vector>
#include <exception>
#include <cmath>
#include <queue>
#include <unordered_map>
#include <limits>
#include <iomanip>
struct Coord {
uint32_t x;
uint32_t y;
double Distance(const Coord& coords) const {
return std::sqrt((coords.x - x) * (coords.x - x) + (coords.y - y) * (coords.y - y));
}
};
class Graph {
private:
uint32_t m_n;
std::vector<uint32_t> m_matrix;
public:
Graph(uint32_t nodeNumber) noexcept;
Graph(std::vector<uint32_t>) noexcept;
void ConnectNodes(uint32_t n1, uint32_t n2);
uint32_t GetNodeNumber() const noexcept;
std::vector<uint32_t> GetAdjacentNodes(uint32_t) const noexcept;
void PrintMatrix() const noexcept;
enum class Color : std::uint8_t {
White,
Gray,
Black
};
};
// Constructs an empty graph with nodeNumber nodes and no edges.
Graph::Graph(uint32_t nodeNumber) noexcept
:
m_n(nodeNumber),
m_matrix(nodeNumber* nodeNumber, 0)
{
}
// Constructs a graph with the NxN node adjacency matrix given.
Graph::Graph(std::vector<uint32_t> matrix) noexcept
:
m_n(static_cast<uint32_t>(std::sqrt(matrix.size()))),
m_matrix(matrix)
{
}
// Connects node n1 to node n2. Throws an exception if one of the nodes doesn't exist.
void Graph::ConnectNodes(uint32_t n1, uint32_t n2)
{
if (n1 < m_n && n2 < m_n)
{
// Matrix(n1, n2) = Matrix(n1, n2) = 1.
m_matrix.at(n1 * m_n + n2) = 1;
m_matrix.at(n2 * m_n + n1) = 1;
}
else
throw std::runtime_error("Invalid nodes received on ConnectNodes.");
}
// Returns the number of nodes the graph has.
uint32_t Graph::GetNodeNumber() const noexcept
{
return m_n;
}
// Returns a vector that contains all nodes that are adjacent to the given node.
std::vector<uint32_t> Graph::GetAdjacentNodes(uint32_t node) const noexcept
{
const uint32_t rowStartingIndex = node * m_n;
std::vector<uint32_t> adjacentNodes;
for (uint32_t i = 0; i != m_n; ++i)
{
if (m_matrix.at(i + rowStartingIndex) != 0)
adjacentNodes.push_back(i);
}
return adjacentNodes;
}
// Prints a matrix that represents the graph.
void Graph::PrintMatrix() const noexcept
{
for (decltype(m_matrix.size()) i = 0; i != m_matrix.size(); ++i)
{
std::cout << m_matrix[i] << ' ';
if ((i + 1) % m_n == 0)
std::cout << '\n';
}
}
double Prim(const Graph& graph, const std::vector<Coord>& coords)
{
// O Heap guardará dois elementos em cada posição. O primeiro indica o vértice e o segundo a distância.
using NodeAndDist = std::pair<uint32_t, double>;
constexpr uint32_t STARTING_NODE = 0;
double distance = 0;
// No heap, vamos considerar que as distâncias menores devem ficar no topo. Por isso, fazemos uma comparação especial.
// O número do vértice nem entra na comparação.
auto heapCmp = [](const NodeAndDist& l, const NodeAndDist& r) { return l.second > r.second; };
// Inicializa os heaps.
std::vector<NodeAndDist> minHeap;
for (uint32_t i = 1; i != graph.GetNodeNumber(); ++i)
{
minHeap.push_back({ i, std::numeric_limits<double>::infinity() });
}
minHeap.push_back({ STARTING_NODE, 0 });
std::make_heap(minHeap.begin(), minHeap.end(), heapCmp);
while (!minHeap.empty())
{
// Pega o topo.
const NodeAndDist currentNodeAndDist = minHeap.front();
// Coloca o fim no início.
minHeap.at(0) = minHeap.back();
// Retira o fim.
minHeap.pop_back();
// Arranja o heap novamente.
std::make_heap(minHeap.begin(), minHeap.end(), heapCmp);
distance += currentNodeAndDist.second;
// Para cada outro vértice adjacente, checa se a distância
// salva no heap é maior do que a nova distância calculada.
for (NodeAndDist& adjacent : minHeap)
{
const Coord& currentCoord = coords.at(currentNodeAndDist.first);
const Coord& adjacentCoord = coords.at(adjacent.first);
double dis = currentCoord.Distance(adjacentCoord);
if (adjacent.second > dis)
{
adjacent.second = dis;
std::make_heap(minHeap.begin(), minHeap.end(), heapCmp);
}
}
}
return distance;
}
int main()
{
uint32_t testNumber;
std::cin >> testNumber;
for (uint32_t i = 0; i != testNumber; ++i)
{
uint32_t peopleNumber;
std::cin >> peopleNumber;
Graph graph(peopleNumber);
// Ler as coordenadas de todos.
std::vector<Coord> peopleCoords;
for (uint32_t person = 0; person != peopleNumber; ++person)
{
uint32_t x, y;
std::cin >> x >> y;
peopleCoords.push_back(Coord{ x, y });
}
std::cout << std::fixed << std::setprecision(2) << Prim(graph, peopleCoords) / 100.0 << '\n' << '\n';
}
return 0;
}