-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alex Travelling.cpp
37 lines (30 loc) · 1.11 KB
/
Alex Travelling.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
typedef long long ll;
class Solution {
public:
int minimumCost(vector<vector<int>>& fl, int n, int k) {
vector<pair<ll,ll>> adj[n+1];
ll u,v,wt;
for(auto it : fl) {
u = it[0], v = it[1], wt = it[2];
adj[u].push_back({v,wt}); // because it is directed graph
}
priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> pq;
vector<ll> dist(n+1, LLONG_MAX);
pq.push({0, k}); dist[k] = 0;
while(!pq.empty()) {
ll d = pq.top().first;
ll curr = pq.top().second;
pq.pop();
for(auto it : adj[curr]) {
ll next_node = it.first;
ll next_dist = it.second;
if(dist[next_node] > dist[curr] + next_dist) {
dist[next_node] = dist[curr] + next_dist;
pq.push({dist[next_node], next_node});
}
}
}
ll maxi = *max_element(dist.begin()+1, dist.end());
return maxi == LLONG_MAX ? -1 : maxi;
}
};