-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
59 lines (50 loc) · 1.45 KB
/
Dijkstra.cpp
File metadata and controls
59 lines (50 loc) · 1.45 KB
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
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <limits>
#include <numeric>
#include <climits>
#define int long long
using namespace std;
class Solution {
public:
vector<int> dijkstra(int n, vector<vector<int>> &edges, int src) {
vector<int> ans(n , INT_MAX);
vector<vector<pair<int,int>>> adj(n , vector<pair<int,int>>());
for(int i=0 ; i<edges.size() ; i++){
int u = edges[i][0] , v = edges[i][1] , w = edges[i][2];
adj[u].push_back({v , w});
adj[v].push_back({u , w});
}
priority_queue< pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>> > pq; //dist , node
pq.push({0 , src});
ans[src] = 0;
while(!pq.empty()){
int dist = pq.top().first;
int curr = pq.top().second;
pq.pop();
for(pair<int,int> p : adj[curr]){
int nb = p.first;
int weight = p.second;
if(ans[nb] > dist+weight){
ans[nb] = dist+weight;
pq.push({ans[nb] , nb});
}
}
}
return ans;
}
};