-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreachableNodesInSubdividedGraph.cpp
More file actions
38 lines (37 loc) · 1.25 KB
/
reachableNodesInSubdividedGraph.cpp
File metadata and controls
38 lines (37 loc) · 1.25 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
// Source: https://leetcode.com/problems/reachable-nodes-in-subdivided-graph/
// Author: Miao Zhang
// Date: 2021-03-21
class Solution {
public:
int reachableNodes(vector<vector<int>>& edges, int maxMoves, int n) {
vector<vector<pair<int, int>>> graph(n);
for (const auto& e: edges) {
graph[e[0]].emplace_back(e[1], e[2]);
graph[e[1]].emplace_back(e[0], e[2]);
}
// moves num, node
priority_queue<pair<int, int>> q;
unordered_map<int, int> moves;
q.push({maxMoves, 0});
while (!q.empty()) {
int move = q.top().first;
int cur = q.top().second;
q.pop();
if (moves.count(cur)) continue;
moves[cur] = move;
for (const auto& g: graph[cur]) {
int nxt = g.first;
int nxt_move = move - g.second - 1;
if (moves.count(nxt) || nxt_move < 0) continue;
q.push({nxt_move, nxt});
}
}
int res = moves.size();
for (auto& e: edges) {
int uv = moves.count(e[0]) ? moves[e[0]] : 0;
int vu = moves.count(e[1]) ? moves[e[1]] : 0;
res += min(e[2], uv + vu);
}
return res;
}
};