-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra_adjList.cpp
64 lines (59 loc) · 1.65 KB
/
Dijkstra_adjList.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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
class Compare{
public:
bool operator()(pair<int, int> a, pair<int, int> b){
return (a.second<b.second);
}
};
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t, nodes, edges, u, v, w, s;
cin>>t;
for(int l=0;l<t;l++){
cin>>nodes>>edges;
vector<int> distance(nodes+1, -1);
vector<vector<pair<int, int> > > adj(nodes+1);
for(int i=0;i<edges;i++){
cin>>u>>v>>w;
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
priority_queue<pair<int, int>, vector<pair<int, int> >, Compare> q;
cin>>s;
distance[s]=0;
q.push(make_pair(s, 0));
int temp;
while(!q.empty()){
u = q.top().first;
//temp = q.front().second;
q.pop();
for(int i=0;i<adj[u].size();i++){
v = get<0>(adj[u][i]);
int d = distance[u] + get<1>(adj[u][i]);
if(distance[v]==-1){
distance[v] = d;
q.push(make_pair(v, d));
}else if(d<distance[v]){
distance[v] = d;
q.push(make_pair(v, d));
}
}
}
for(int i=1;i<=nodes;i++){
if(i==s){
//do nothing
}else{
cout<<distance[i]<<" ";
}
}
cout<<endl;
}
return 0;
}