-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDijastra.cpp
50 lines (39 loc) · 1007 Bytes
/
Dijastra.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
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void Dijastra(int v, map<int,set<pair<int,int>>> adj){
int src =0;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
vector<int> distTo(v,INT_MAX);
distTo[src]=0;
pq.push(make_pair(0,src));
while(!pq.empty()){
int frontdist = pq.top().first;
int frontnode = pq.top().second;
pq.pop();
vector<pair<int,int>>::iterator it;
for(auto it: adj[frontnode]){
int nextnode=it.first;
int nextdist=it.second;
if(distTo[nextnode]>frontdist+nextdist){
distTo[nextnode] = distTo[frontnode]+nextdist;
pq.push(make_pair(distTo[nextnode],nextnode));
}
}
}
for(auto i:distTo){
cout<<i<<",";
}
}
int main(){
int n,m;
cin>>n>>m;
map<int, set<pair<int,int>>>adj;
for(int i=0;i<m;i++){
int u,v,wt;
cin>>u>>v>>wt;
adj[u].insert({v,wt});
}
Dijastra(n, adj);
return 0;
}