-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra_SortestPathAlgo.cpp
86 lines (83 loc) · 2.85 KB
/
dijkstra_SortestPathAlgo.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <bits/stdc++.h>
#define ll long long
#define mx 1000010
using namespace std;
/***************************************************************************************/
bool visited[mx];
ll dis[mx];
ll path[mx];
void dijkstra(vector<pair<ll, ll>> graph[], ll source, ll destination)
{
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> p; // its actually priority_queue<vector<pair<ll, ll>>>p but in decending oder
p.push({ 0, source });
dis[source] = 0;
while (!p.empty())
{
pair<ll, ll> current_node = p.top();
p.pop();
ll vertex = current_node.second;
if (visited[vertex])
{
continue;
}
else
{
visited[vertex] = true;
ll sz = graph[vertex].size();
for (ll i = 0; i < sz; i++)
{
ll child = graph[vertex][i].first;
ll weight = graph[vertex][i].second;
if (dis[vertex] + weight < dis[child])
{
dis[child] = dis[vertex] + weight;
p.push({ dis[child], child });
path[child] = vertex; //in the child node always store its parent node for sortest path//
}
}
}
}
for (int i = 1; i <= destination; i++) {
cout<<"sortest distance of node "<<i<<" is :"<<dis[i]<<endl;
cout << "sortest path is :";
vector<ll>ans;
ans.push_back(i);
ll temp = i;
while (path[temp] != -1)
{
ans.push_back(path[temp]);
temp = path[temp];
}
reverse(ans.begin(), ans.end());
for (ll i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout<<endl<<endl;
}
}
void solve()
{
vector<ll>ans;
ll numberOfTown, numberOfRoad;
cin >> numberOfTown >> numberOfRoad;
vector<pair<ll, ll>> graph[numberOfTown + 10];
for (ll i = 0; i <= numberOfTown; i++)
{
dis[i] = 1e16;
visited[i] = false;
path[i] = -1;
}
for (ll i = 0; i < numberOfRoad; i++)
{
ll u, v, wt;
cin >> u >> v >> wt;
graph[u].push_back({ v, wt });
graph[v].push_back({ u, wt });
}
dijkstra(graph, 1, numberOfTown);
}
int main()
{
solve();
return 0;
}