forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstraalgo.cpp
More file actions
63 lines (49 loc) · 1.52 KB
/
dijkstraalgo.cpp
File metadata and controls
63 lines (49 loc) · 1.52 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
#include <iostream>
#include <vector>
#include <queue>
#include <limits>
using namespace std;
void dijkstra(int startNode, int numNodes, const vector<vector<pair<int, int>>>& adj) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
vector<int> dist(numNodes + 1, numeric_limits<int>::max());
dist[startNode] = 0;
pq.push({0, startNode});
while (!pq.empty()) {
int d = pq.top().first;
int u = pq.top().second;
pq.pop();
if (d > dist[u]) {
continue;
}
for (auto& edge : adj[u]) {
int v = edge.first;
int weight = edge.second;
if (dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
pq.push({dist[v], v});
}
}
}
for (int i = 1; i <= numNodes; ++i) {
if (dist[i] == numeric_limits<int>::max()) {
cout << "Distance from " << startNode << " to " << i << ": Infinity" << endl;
} else {
cout << "Distance from " << startNode << " to " << i << ": " << dist[i] << endl;
}
}
}
int main() {
int numNodes, numEdges;
cin >> numNodes >> numEdges;
vector<vector<pair<int, int>>> adj(numNodes + 1);
for (int i = 0; i < numEdges; ++i) {
int u, v, weight;
cin >> u >> v >> weight;
adj[u].push_back({v, weight});
adj[v].push_back({u, weight});
}
int startNode;
cin >> startNode;
dijkstra(startNode, numNodes, adj);
return 0;
}