-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTGraph.cpp
60 lines (47 loc) · 1.31 KB
/
TGraph.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
#include "TGraph.h"
TGraph::TGraph(std::istream &is, int n, int m) {
this->n = n + 1;
this->m = m + 1;
nodes.resize(this->n);
int v, u;
long long int w;
while (is >> v >> u >> w) {
edge first, second;
first.to = u;
second.to = v;
first.weight = w;
second.weight = w;
nodes[v].push_back(first);
nodes[u].push_back(second);
}
}
void TGraph::Print() {
for (int i = 1; i < nodes.size(); i++) {
std::cout << "From node: " << i << std::endl;
for (edge e: nodes[i])
std::cout << "to: " << e.to << " weight: " << e.weight << std::endl;
}
}
long long int TGraph::Dijkstra(int start, int finish) {
std::priority_queue < std::pair <int, int>,
std::vector <std::pair <int, int>>,
std::greater <std::pair <int, int>>
> PQ;
distance.assign(n, std::numeric_limits<long long int>::max());
distance[start] = 0;
PQ.push({0, start});
while (!PQ.empty()) {
int v = PQ.top().second;
PQ.pop();
for (edge tmp: nodes[v]) {
if (distance[tmp.to] > distance[v] + tmp.weight) {
distance[tmp.to] = distance[v] + tmp.weight;
PQ.push({distance[tmp.to], tmp.to});
}
}
}
if (distance[finish] == std::numeric_limits<long long int>::max()) {
return -1;
}
return distance[finish];
}