-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
67 lines (59 loc) · 1.43 KB
/
Dijkstra.cpp
File metadata and controls
67 lines (59 loc) · 1.43 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
// Implementation of Dijkstra's algorithm using adjacency lists and priority queue.
//
// Running time: O(ElogV)
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define int long long
#define pii pair<int, int>
const int N = 100005;
bool vis[N];
int dist[N];
vector<pii> adj[N];
int V, E;
void Dijkstra(int src)
{
priority_queue<pii, vector<pii>, greater<pii> > pq;
int i, u, v, wt;
pq.push(make_pair(src, 0));
dist[src] = 0;
while (!pq.empty())
{
u = pq.top().first;
vis[u] = true;
pq.pop();
for(i = 0 ; i < adj[u].size() ; i++)
{
v = adj[u][i].first;
wt = adj[u][i].second;
if(!vis[v] and dist[v] > dist[u] + wt)
{
dist[v] = dist[u] + wt;
vis[v] = true;
pq.push(make_pair(v, dist[v]));
}
}
}
cout << "Distance from Source" << endl;
for(i = 0 ; i < V ; i++)
cout << i << "\t" << dist[v] << endl;
}
int32_t main()
{
int x, y, wt, src, i;
memset(vis, false, sizeof(vis));
cout << "Enter the no of Nodes";
cin >> V;
cout << "Enter the no of Edges";
cin >> E;
for(i = 0 ; i < E ; i++)
{
cin >> x >> y >> wt;
adj[x].push_back(make_pair(y, wt));
adj[y].push_back(make_pair(x, wt));
}
cout << "Enter the source node";
cin >> src;
Dijkstra(src);
return 0;
}