-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dijkshtra.cpp
64 lines (54 loc) · 1.01 KB
/
Dijkshtra.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
/*
@author Sukeesh
Dijkshtra Algorithm
*/
#include "bits/stdc++.h"
using namespace std;
#define N 20001
const int inf = 1000000000;
vector< int > adj[N];
int grad[N][N];
int d[N];
int i, n;
inline void pre(){
for( i = 0 ; i <= n ; i++ ){
d[i]=inf;
}
}
inline void dijkstra(int node){
pre();
d[node] = 0;
priority_queue< pair< int, int > > Q;
Q.push(make_pair(0,node));
while(!Q.empty()){
pair< int, int > top = Q.top();
Q.pop();
int u = top.second;
if(top.first>d[u]) continue;
for(i=0;i< adj[u].size();i++){
int to = adj[u][i];
int len = grad[u][to];
if(d[u] + len < d[to]){
d[to] = d[u] + len;
Q.push(make_pair(-d[to],to));
}
}
}
}
int main(){
ios::sync_with_stdio(false);
int q;
cin >> n >> q ;
for( i = 0 ; i < q ; i++ ){
int x,y,l;
cin >> x >> y >> l;
adj[x].push_back(y);
adj[y].push_back(x);
grad[y][x]=l;
grad[x][y]=l;
}
dijkstra(0);
for( i = 0 ; i < n ; i++ ){
cout << "Shortest Distance from " << 0 << " to " << i << " is " << d[i] << "\n";
}
}