-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijikstra.cpp
85 lines (75 loc) · 1.53 KB
/
dijikstra.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream>
#include<vector>
#include<bits/stdc++.h>
#define lli long long int
#define mod 1000000007
#define pb push_back
#define mk make_pair
#define fastio ios_base::sync_with_stdio(false); cout.tie(NULL);
using namespace std;
int main()
{
fastio;
int t;
cin>>t;
while(t--)
{
lli n,m,i,a,b,w;
cin>>n>>m;
vector<pair<lli,lli>> ar[n+1];
vector<lli> dist(n+1,mod);
for(i=0;i<m;i++)
cin>>a>>b>>w , ar[a].pb({b,w}) , ar[b].pb({a,w});
priority_queue<pair<lli,lli>,vector<pair<lli,lli>>,greater<pair<lli,lli>>> p; // mean heap...smallest element stays in top in pair
// priority_queue< T , vector<T> , Compare = std::less<typename Container::value_type> >
// template of priority_queue
//
// template<
// class T,
// class Container = std::vector<T>,
// class Compare = std::less<typename Container::value_type>
// > class priority_queue;
cin>>a; // source node
p.push({0,a}); //due to sorting according to first value in pri.queue...we are storing pair like (dist,node)
dist[a]=0;
while(!p.empty())
{
b=p.top().second;
w=p.top().first;
p.pop();
for(pair<lli,lli> edge : ar[b])
{
if(w+edge.second < dist[edge.first])
{
dist[edge.first]=w+edge.second;
p.push({dist[edge.first],edge.first});
}
}
}
for(i=1;i<=n;i++)
if(i!=a)
{
if(dist[i]==mod)
cout<<-1<<" ";
else
cout<<dist[i]<<" ";
}
cout<<"\n";
}
return 0;
}
/*
1
6 9
1 2 4
1 6 2
2 3 5
2 6 1
6 3 8
6 5 10
3 4 6
3 5 3
5 4 5
1
ans : 0 3 8 14 11 2
*/