forked from divik544/AlgoHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra.cpp
76 lines (74 loc) · 1.45 KB
/
dijkstra.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
#include<iostream>
#include<vector>
#include<set>
#include<stack>
using namespace std;
const int INF = 0x3f3f3f3f;
void dijkstra(int src, vector< vector< pair<int,int> > >& graph, vector<int> &prev)
{
int V = graph.size();
set< pair<int,int> > ds; //first val is weight second is destination
vector<int> dis(V,INF);
dis[src] = 0;
ds.insert({dis[src],src});
while(!ds.empty())
{
pair<int,int> p = *(ds.begin());
ds.erase(ds.begin());
int u = p.second;
for(auto &i : graph[u])
{
int v = i.second;
int w = i.first;
if(dis[v] > dis[u]+w)
{
if(dis[v] != INF)
ds.erase(ds.find({dis[v], v}));
dis[v] = dis[u] + w;
prev[v] = u;
ds.insert({dis[v],v});
}
}
}
for(int i = 0; i < V; i++)
{
cout << "Distance[" << i << "] = " << dis[i] << '\n';
}
}
int main()
{
int V;
cout << "Enter No. of Vertices: ";
cin >> V;
vector< vector< pair<int,int> > > graph(V);
cout << "Enter Edges(src destination weight)(-1 -1 -1 to terminate)\n";
int u,v,w;
while(true)
{
cin >> u >> v >> w;
if(u == -1)
break;
graph[u].push_back({w,v});
// graph[v].push_back({w,u}); //uncomment this line if graph is directed
}
cout << "Enter src: ";
int src;
cin >> src;
vector<int> prev(V,0);
dijkstra(src, graph, prev);
cout << "Enter Destination: ";
int des;
cin >> des;
stack<int> s;
s.push(des);
while(prev[des] != des)
{
s.push(prev[des]);
des = prev[des];
}
while(!s.empty())
{
cout << s.top() << "->";
s.pop();
}
}