-
Notifications
You must be signed in to change notification settings - Fork 14
/
Dijkstra.cpp
207 lines (166 loc) · 4.94 KB
/
Dijkstra.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// CREATED BY DEVIL'S_NE5T AKA HARSH GUPTA
// NOT A PRO CODER XD
// JUST SOMEONE WHO IS INTERESED IN THE WORLD OF CODING
// JUST SO I MENTION THAT ALTHOUGH I LIKE CODING BUT DEBUGGING MAKES ME FEEL SICK
#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define ull unsigned long long
//COMMON DATA STRUCTURES
//----------------------
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpairll vector<pair<ll,ll>>
#define mpll map<ll,ll>
#define mpcl map<char,ll>
#define setll set<ll>
//COMMON OPERATIONS
//-----------------
#define setbits __builtin_popcountll //count number of 1 in binary
#define leadzero __builtin_clz //count leading zeroes in binary
#define tailzero __builtin_ctz //coutn tail zero
#define pb push_back
#define pfrt push_front
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define endl "\n"
#define all(v) v.begin(),v.end()
#define revall(v) v.end(),v.begin()
#define MEM(a, b) memset(a, (b), sizeof(a))
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,a,b) for(ll i=b-1;i>=a;i--)
#define foreach(it,l) for(auto it=l.begin();it!=l.end();it++)
#define reveach(it,l) auto it = l.end();it--;for(it;it!=l.begin();it--)
// ________________________________________________________________________________________________________________
//PBDS
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
// ordered_set;
//find_by_order() return the iterator to the element at xth(0-based) position
//order_of_key() returns number of elements in set which are strictly less than x
#define PI 3.1415926535897932384626433832795
const ll M = 1e9+7;
const ll i1 = 200005;
//________________________________________________________________________________________________________________
//COMMON FUNCTIONS USED
//---------------------
ll gcd(ll a,ll b){
if(b==0) return a;
return gcd(b,a%b);
}
ll lcm(ll a,ll b){
return ((a*b)/gcd(a,b));
}
ll modexp(ll a,ll b){
int res = 1;
while (b){
if (b%2==1)res= (res*a)%M;
b>>=1;
a=(a*a)%M;
}
return res%M;
}
// ll fact[i1];
// void factorial(){
// fact[0] = 1;
// loop(i,1,i1){
// fact[i] = fact[i-1]*i;
// fact[i]%=M;
// }
// }
// ll ncr(ll n, ll r){
// ll x = fact[n]*(modexp(fact[r],M-2)%M*modexp(fact[n-r],M-2)%M)%M;
// return x%M;
// }
// ll prime[i1]={};
// vll prm;
// void seive(){
// prime[0]=prime[1]=1;
// for(ll i=2;i*i<=i1;i++){
// if(prime[i]==0){
// for(ll j=i*i;j<=i1;j+=i){
// prime[j]=1;
// }
// }
// }
// for(ll i=1;i<=i1;i++){
// if(prime[i]==0)prm.pb(i);
// }
// }
// ________________________________________________________________________________________________________________
//CODE HERE........
bool sortbysec(const pair<int,int> &a,const pair<int,int> &b){
return (a.second < b.second);
}
void harsh_op(ll test_case){
ll n,m; cin>>n>>m;
ll temp = m;
vpairll adj[n+1];
while(temp--){
ll a,b,c; cin>>a>>b>>c;
adj[a].pb({b,c});
adj[b].pb({a,c});
}
priority_queue <pll,vpairll,greater<pll>> q;
// set<pair<ll,ll>> q;
ll dis[n+1];
// loop(i,0,n+1)cout<<dis[i]<<" "; cout<<endl;
// memset(dis,LLONG_MAX,sizeof(dis)/sizeof(dis[0]));
loop(i,0,n+1)dis[i] = LLONG_MAX;
dis[1] = 0;
q.push({0,1});
ll st[n+1]={0};
st[1] = -1;
while(!q.empty()){
pll k = q.top();
q.pop();
ll currdis = k.F;
ll node = k.S;
// loop(i,0,n+1)cout<<dis[i]<<" "; cout<<endl;
// if(adj[node].size()!=0)
for(auto it:adj[node]){
ll child = it.F;
ll len = it.S;
if(dis[child] > currdis + len){
dis[child] = currdis + len;
st[child] = node;
q.push({dis[child],child});
}
}
}
// loop(i,0,n+1)cout<<dis[i]<<" ";
// cout<<dis[n]<<endl;
if(dis[n]==LLONG_MAX){
cout<<"-1"<<endl;
return;
}
ll x = n;
vll ans;
ans.pb(n);
while(x!=1){
x = st[x];
ans.pb(x);
}
rev(i,0,ans.size())cout<<ans[i]<<" "; cout<<endl;
}
int main(){
fast;
// seive();
// factorial();
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll ttcc = 1;
// cin>>ttcc;
ll test_case=0;
while(ttcc--){
harsh_op(test_case);
test_case++;
}
}