forked from fineanmol/Hacktoberfest2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
76 lines (63 loc) · 1.65 KB
/
dijkstra.cpp
File metadata and controls
76 lines (63 loc) · 1.65 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
#include <bits/stdc++.h>
using namespace std;
vector<int> minpath(int source, int n, const unordered_map<int, list<pair<int, int>>> &adj)
{
vector<int> dis(n, INT_MAX);
set<pair<int, int>> s;
s.insert(make_pair(0, source));
dis[source] = 0;
while (!s.empty())
{
auto top = *(s.begin());
int nodeDistance = top.first;
int node = top.second;
s.erase(s.begin());
for (auto i : adj.at(node))
{
if (dis[i.first] > i.second + nodeDistance)
{
auto record = s.find(make_pair(dis[i.first], i.first));
if (record != s.end())
{
s.erase(record);
}
dis[i.first] = i.second + nodeDistance;
s.insert(make_pair(dis[i.first], i.first));
}
}
}
return dis;
}
int main()
{
int n;
cout << "Enter number of nodes: ";
cin >> n;
// Subtract 1 to align with vector indices
int e;
cout << "Enter number of edges: ";
cin >> e;
unordered_map<int, list<pair<int, int>>> adj;
for (int i = 0; i < e; i++)
{
int u, v, w;
cin >> u >> v >> w;
pair<int, int> p = make_pair(v, w);
pair<int, int> p2 = make_pair(u, w);
adj[u].push_back(p);
adj[v].push_back(p2);
}
vector<int> ans = minpath(0, n, adj);
for (int i = 0; i < ans.size(); i++)
{
if (ans[i] == INT_MAX)
{
cout << "INF ";
}
else
{
cout << "Shortest distance from base to node: " << i << " = "<< ans[i] << endl;
}
}
return 0;
}