-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathDihkstras-using-STL.cpp
57 lines (51 loc) · 1.01 KB
/
Dihkstras-using-STL.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
// Author - Yash Deshpande
// (yvdyash)
#include<bits/stdc++.h>
using namespace std;
vector<vector<pair<int,int> > > g(100005);
//vector<bool> vis(100005);
vector<long long> dist(100005,LLONG_MAX);
void shortestpath(int src)
{
set<pair<long long,int> > s;
s.insert({0,src});
dist[src]=0;
while(!s.empty())
{
pair<long long,int> cur_node=*(s.begin());
s.erase(s.begin());
int parent=cur_node.second;
for(int i=0;i<g[parent].size();i++)
{
int child=g[parent][i].first;
long long weight=g[parent][i].second;
if(dist[child]>dist[parent]+weight)
{
if(dist[child]!=LLONG_MAX)
s.erase(s.find({dist[child],child}));
dist[child]=dist[parent]+weight;
s.insert({dist[child],child});
}
}
}
}
int main()
{
int tc;cin>>tc;
while(tc--){
int n,e,src,dest;
cin>>n>>e>>src>>dest;
for(int i=0;i<=n;i++){
g[i].clear();
dist[i]=LLONG_MAX;
}
while(e--)
{
int a,b,c;
cin>>a>>b>>c;
g[a].push_back({b,c});
g[b].push_back({a,c});
}
shortestpath(src);
}
}