-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
60 lines (57 loc) · 1.05 KB
/
Copy pathDijkstra.cpp
File metadata and controls
60 lines (57 loc) · 1.05 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
#include "bits/stdc++.h"
using namespace std;
#define pii pair<int,int>
#define f first
#define s second
vector<pii> adj[100001];
int dist[100001];
bool vis[100001];
void init(int n){
for(int i=0;i<n+1;i++){
dist[i] = 1e9;
vis[i] = false;
}
}
void dijkstra(int s){
priority_queue<pii> Q;
Q.push({0,s});
dist[s] = 0;
while(!Q.empty()){
pii p = Q.top();
Q.pop();
int n = p.s;
if(vis[n]) continue;
vis[n] = true;
for(int i=0;i<adj[n].size();i++){
int e = adj[n][i].s;
int w = adj[n][i].f;
if(dist[n] + w < dist[e]){
dist[e] = dist[n] + w;
Q.push({-dist[e],e});
}
}
}
}
int main(int argc, char const *argv[])
{
int n,m;
cin>>n>>m;
init(n);
for(int i=0;i<m;i++){
int x,y,w;
cin>>x>>y>>w;
adj[x].push_back({w,y});
adj[y].push_back({w,x});
}
// Bellman Ford's algorithm do the same work in O(V.E), But this
// will do that in only O(V + ElogV) due to use of min_priority Queue
for(int j=1;j<=n;j++){
cout<<j<<"--> ";
init(n);
dijkstra(j);
for(int i=0;i<n;i++)
cout<<dist[i+1]<<" ";
cout<<endl;
}
return 0;
}