-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
85 lines (50 loc) · 1.55 KB
/
Dijkstra.cpp
File metadata and controls
85 lines (50 loc) · 1.55 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
// Sample code to perform I/O:
cin >> name; // Reading input from STDIN
cout << "Hi, " << name << ".\n"; // Writing output to STDOUT
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
*/
// Write your code here
#include<bits/stdc++.h>
using namespace std;
vector< pair <int, int> > adjW[100005];
bool vis[100005];
int dist[10000000];
void Dijkstra(int src,int V){
for(int i=1;i<=V;i++){
dist[i]=INT_MAX;
vis[i]=false;
}
multiset<pair<int, int>> mul;
dist[src] = 0;
mul.insert({0,src});
while( !mul.empty()){
pair<int,int> frnt = *mul.begin();
int u = frnt.second;
mul.erase(mul.begin());
if( !vis[u]){
vis[u]=1;
for(auto i:adjW[u]){
int v = i.first;
int w = i.second;
if(dist[u]+w<dist[v]){
dist[v]=dist[u]+w;
mul.insert({dist[v],v});
}
}
}
}
for(int i=2;i<=V;i++) {
cout<<dist[i]<<" ";
}
}
int main(){
int vertex,edges,u,v,w;
cin>>vertex>>edges;
for(int i=0;i<edges;i++){
cin>>u>>v>>w;
adjW[u].push_back({v,w});
// adjW[v].push_back({u,w});
}
Dijkstra(1,vertex);
}