-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
80 lines (59 loc) · 1.66 KB
/
dijkstra.cpp
File metadata and controls
80 lines (59 loc) · 1.66 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
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
// Dijkstra Algorithm for Directed/Undirected and Weighted(positive)/Unweighted Graph (General) -> O(E + Elog(v))
void dijkstra(int source, vector<bool>& vis, vector<int>& dist, vector<pair<int, int>> adj[]) {
// min heap of pair (distance, node) (using priority queue)
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, source});
dist[source] = 0;
while (!pq.empty()) {
int node = pq.top().second; // Top element of priority_queue (with least distance)
pq.pop();
if (vis[node]) continue;
vis[node] = true;
for (auto &v : adj[node]) {
int child = v.first, wt = v.second;
if (dist[node] + wt < dist[child]) {
dist[child] = dist[node] + wt;
pq.push({dist[child], child});
}
}
}
}
int main() {
int n, m, src;
cout << "Enter number of vertex, edges and source : ";
cin >> n >> m >> src;
vector<pair<int, int>> adj[n];
// directed graph
cout << "Enter vertex, vertex, weight : ";
for (int i = 0; i < m; i++) {
int u, v, wt;
cin >> u >> v >> wt;
adj[u].push_back({v, wt});
}
vector<bool> vis(n, false);
vector<int> dist(n, INF);
dijkstra(src, vis, dist, adj);
for(int i=0; i<n; i++){
cout << "Shortest distance from "<< src <<" to " << i << " is : " << dist[i] << endl;
}
return 0;
}
/*
graph :
from -> (to, weight)
0 -> (1, 4), (2, 1)
1 -> (3, 2)
2 -> (1, 1), (3, 5)
3 -> (4, 3)
input formate:
5 6 0
0 1 4
0 2 1
1 3 2
2 1 1
2 3 5
3 4 3
*/