-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_Y.cpp
More file actions
223 lines (203 loc) · 4.7 KB
/
task_Y.cpp
File metadata and controls
223 lines (203 loc) · 4.7 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
class Edge
{
public:
Edge(size_t from, size_t to, int64_t weight)
{
from_ = from;
to_ = to;
weight_ = weight;
}
bool operator <(const Edge& second) const
{
return weight_ > second.weight_;
}
bool operator ==(const Edge& second) const
{
return(from_ == second.from_ && to_ == second.to_ && weight_ == second.weight_);
}
bool operator !=(const Edge& second) const
{
return !(*this == second);
}
bool operator >(const Edge& second) const
{
return weight_ < second.weight_;;
}
size_t getTo() const
{
return to_;
}
size_t getFrom() const
{
return from_;
}
int64_t getWeight() const
{
return weight_;
}
private:
size_t from_, to_;
int64_t weight_;
};
class Graph
{
public:
typedef size_t Vertex;
Graph(size_t vertex_count, bool is_directed = false)
{
vertex_count_ = vertex_count;
is_directed_ = is_directed;
}
size_t getVertexCount() const
{
return vertex_count_;
}
size_t geEdgeCount() const
{
return is_directed_ ? edge_count_ : edge_count_;
};
virtual std::vector<Edge> getNeighbors(const Vertex& v) const = 0;
virtual size_t getNeighborsCount(const Vertex& v) const = 0;
virtual void addEdge(const Vertex& start, const Vertex& finish, const int64_t weight = 0)
{
edge_count_++;
}
virtual std::vector<Edge> getEdges() const = 0;
protected:
bool is_directed_;
size_t vertex_count_,
edge_count_;
};
class GraphAdjList : public Graph
{
public:
GraphAdjList(const size_t vertex_count, bool is_directed) :
Graph(vertex_count, is_directed)
{
adj_list_ = std::vector<std::vector<Edge>>(vertex_count);
}
virtual void addEdge(const Vertex& start, const Vertex& finish, const int64_t weight = 1)
{
Graph::addEdge(start, finish);
adj_list_[start].push_back({ start, finish, weight });
edges[{start, finish}] = weight;
if (!is_directed_)
{
adj_list_[finish].push_back({ finish, start, weight });
edges[{finish, start}] = weight;
}
}
std::vector<Edge> getNeighbors(const Vertex& v) const override
{
return adj_list_[v];
}
size_t getNeighborsCount(const Vertex& v) const override
{
return adj_list_[v].size();
}
std::vector<Edge> getEdges() const override
{
std::vector<Edge> edges;
for (Vertex v = 0; v < getVertexCount(); v++)
{
edges.insert(edges.end(), adj_list_[v].begin(), adj_list_[v].end());
}
return edges;
}
private:
std::vector <std::vector<Edge>> adj_list_;
std::map<std::pair<Vertex, Vertex>, int64_t> edges;
};
struct distance_estimate
{
distance_estimate(const Graph::Vertex& vertex, const int64_t& estimate)
{
vertex_ = vertex;
estimate_ = estimate;
}
Graph::Vertex vertex_;
int64_t estimate_;
bool operator<(const distance_estimate& second) const
{
return estimate_ > second.estimate_;
}
};
namespace GraphProcessing
{
namespace
{
const int64_t INF = 2e12;
void Relax(const Graph& g, const Graph::Vertex& vertex, std::vector<int64_t>& distance, std::priority_queue<distance_estimate>& current_distances, std::vector<bool>& used)
{
for (Edge edge : g.getNeighbors(vertex))
{
if (distance[edge.getTo()] > distance[edge.getFrom()] + edge.getWeight())
{
distance[edge.getTo()] = distance[edge.getFrom()] + edge.getWeight();
if (!used[edge.getTo()])
{
current_distances.push({ edge.getTo(), distance[edge.getTo()] });
}
}
}
}
}
std::vector<int64_t> findDistances(const Graph& g, const Graph::Vertex& source)
{
std::vector<bool> used(g.getVertexCount(), false);
std::vector<int64_t> distance(g.getVertexCount(), INF);
std::priority_queue<distance_estimate> current_distances;
distance[source] = 0;
used[source] = true;
current_distances.push({ source, 0 });
Relax(g, source, distance, current_distances, used);
for (size_t i = 0; i < g.getVertexCount() - 1; i++)
{
Graph::Vertex curr = current_distances.top().vertex_;
current_distances.pop();
while (used[curr] && !current_distances.empty())
{
curr = current_distances.top().vertex_;
current_distances.pop();
}
if (current_distances.empty())
{
break;
}
used[curr] = true;
Relax(g, curr, distance, current_distances, used);
}
return distance;
}
}
int main()
{
size_t num;
std::cin >> num;
for (size_t step = 0; step < num; step++)
{
size_t n, m;
std::cin >> n >> m;
GraphAdjList g(n, false);
for (size_t i = 0; i < m; i++)
{
size_t from, to;
int64_t weight;
std::cin >> from >> to >> weight;
g.addEdge(from, to, weight);
}
size_t source;
std::cin >> source;
for (int64_t distance : GraphProcessing::findDistances(g, source))
{
std::cout << (distance == GraphProcessing::INF ? 2009000999 : distance) << " ";
}
std::cout << std::endl;
}
return 0;
}