-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDijktras_wilsonfurtado2000.cpp
More file actions
45 lines (42 loc) · 909 Bytes
/
Dijktras_wilsonfurtado2000.cpp
File metadata and controls
45 lines (42 loc) · 909 Bytes
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
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
const int inf = 1e9;
int n,m;
cin>>n>>m;
vector<int> dist(n+1,inf);
vector<vector<pair<int,int>>> adj(n+1);
for(int i = 0;i<m;i++){
int u,v,w;
cin>>u>>v>>w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
int k;
cin>>k;
set<pair<int,int>> s;
dist[k] = 0;
s.insert({0,k});
while(!s.empty()){
auto x = *(s.begin());
s.erase(x);
for(auto it:adj[x.second]){
if(dist[it.first]>it.second+dist[x.second]){
s.erase({dist[it.first],it.first});
dist[it.first] = it.second+dist[x.second];
s.insert({dist[it.first],it.first});
}
}
}
for(int i =1;i<=n;i++){
if(dist[i]<inf){
cout<<dist[i]<<" ";
}else{
cout<<-1<<" ";
}
}
cout<<endl;
return 0;
}