|
1 | 1 | class Solution {
|
2 | 2 | vector<vector<vector<int>>> adj;
|
3 | 3 | vector<int> sol;
|
4 |
| - priority_queue<pair<int,int> ,vector<pair<int,int>>,greater<>> pq; |
5 |
| - void pushNeighbours(int node,int curr){ |
6 |
| - for(auto it : adj[node]){ |
7 |
| - int temp = it[0] , start = it[1],end = it[2],newTime = curr+1; |
8 |
| - if(curr<start) newTime = start+1; |
9 |
| - if(newTime < sol[temp] && newTime-1<=end){ |
10 |
| - pq.push({newTime,temp}); |
| 4 | + priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq; |
| 5 | + void pushNeighbours(int node, int curr) { |
| 6 | + for (auto it : adj[node]) { |
| 7 | + int temp = it[0], start = it[1], end = it[2], newTime = curr + 1; |
| 8 | + if (curr < start) newTime = start + 1; |
| 9 | + if (newTime < sol[temp] && newTime - 1 <= end) { |
| 10 | + pq.push({newTime, temp}); |
11 | 11 | sol[temp] = newTime;
|
12 | 12 | }
|
13 | 13 | }
|
14 | 14 | }
|
| 15 | + |
15 | 16 | public:
|
16 | 17 | int minTime(int n, vector<vector<int>>& edges) {
|
17 | 18 | adj = vector<vector<vector<int>>>(n);
|
18 |
| - for(auto it: edges) |
19 |
| - adj[it[0]].push_back({it[1],it[2],it[3]}); |
20 |
| - sol = vector<int> (n,INT_MAX); |
21 |
| - sol[0]=0; |
22 |
| - for(pq.push({0,0});!pq.empty();pq.pop()) |
23 |
| - pushNeighbours(pq.top().second,pq.top().first); |
24 |
| - if(sol[n-1] == INT_MAX) return -1; |
25 |
| - return sol[n-1]; |
| 19 | + for (auto it : edges) |
| 20 | + adj[it[0]].push_back({it[1], it[2], it[3]}); |
| 21 | + sol = vector<int>(n, INT_MAX); |
| 22 | + sol[0] = 0; |
| 23 | + for (pq.push({0, 0}); !pq.empty(); pq.pop()) |
| 24 | + pushNeighbours(pq.top().second, pq.top().first); |
| 25 | + if (sol[n - 1] == INT_MAX) return -1; |
| 26 | + return sol[n - 1]; |
26 | 27 | }
|
27 | 28 | };
|
28 | 29 | const auto __ = []() {
|
|
0 commit comments