-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimizeddijktras.cpp
More file actions
56 lines (46 loc) · 1.44 KB
/
Copy pathoptimizeddijktras.cpp
File metadata and controls
56 lines (46 loc) · 1.44 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
#include <iostream>
#include <vector>
#include <queue>
#include <limits>
using namespace std;
const int INF = numeric_limits<int>::max();
void dijkstra(int source, vector<vector<pair<int, int>>>& graph, vector<int>& distances) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
distances[source] = 0;
pq.push({0, source});
while (!pq.empty()) {
int dist = pq.top().first;
int node = pq.top().second;
pq.pop();
if (dist > distances[node])
continue;
for (auto& edge : graph[node]) {
int nextNode = edge.first;
int weight = edge.second;
if (distances[node] + weight < distances[nextNode]) {
distances[nextNode] = distances[node] + weight;
pq.push({distances[nextNode], nextNode});
}
}
}
}
int main() {
int V = 5;
vector<vector<pair<int, int>>> graph(V);
graph[0].push_back({1, 10});
graph[0].push_back({2, 5});
graph[1].push_back({2, 2});
graph[1].push_back({3, 1});
graph[2].push_back({1, 3});
graph[2].push_back({3, 9});
graph[2].push_back({4, 2});
graph[3].push_back({4, 4});
graph[4].push_back({3, 6});
vector<int> distances(V, INF);
dijkstra(0, graph, distances);
for (int i = 0; i < V; i++) {
if (distances[i] == INF) cout << "INF ";
else cout << distances[i] << " ";
}
return 0;
}