Skip to content

Commit 2f59564

Browse files
authored
Create 8.cpp
just standard dijkastra algorithms
1 parent 1ddbb95 commit 2f59564

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

cses graph/8.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// JAI BAJARANG BALI
2+
3+
// manitianajay45
4+
5+
// give me some sunshine, give me some rain, give me another chance to grow up once again....
6+
7+
// sab moh maya hai....
8+
9+
#include <bits/stdc++.h>
10+
using namespace std;
11+
12+
#define ll long long
13+
14+
ll n, m;
15+
16+
vector<pair<ll,ll>> graph[100005];
17+
vector<ll> shortest(100005,-1);
18+
19+
int main()
20+
{
21+
ios_base::sync_with_stdio(false);
22+
cin.tie(NULL);
23+
cin >> n >> m;
24+
for (ll i = 0; i < m; i++)
25+
{
26+
ll u,v,c;
27+
cin>>u>>v>>c;
28+
graph[u].push_back({v,c});
29+
30+
}
31+
priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> pq;
32+
pq.push({0,1});
33+
34+
while (!pq.empty())
35+
{
36+
pair<ll,ll> p = pq.top();
37+
pq.pop();
38+
if(shortest[p.second]==-1){
39+
shortest[p.second]=p.first;
40+
for(auto i:graph[p.second]){
41+
if(shortest[i.first]==-1){
42+
pq.push({i.second+p.first,i.first});
43+
}
44+
}
45+
}
46+
47+
}
48+
49+
for(ll i=1;i<=n;i++){
50+
cout<<shortest[i]<<" ";
51+
}
52+
cout<<endl;
53+
54+
return 0;
55+
}
56+
57+

0 commit comments

Comments
 (0)