-
Notifications
You must be signed in to change notification settings - Fork 3
/
882.cpp
38 lines (38 loc) · 1.44 KB
/
882.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
typedef pair<int, int> P; // distance, node
class Solution {
public:
int reachableNodes(vector<vector<int>>& edges, int maxMoves, int n) {
unordered_map<int, unordered_map<int, int>> distances;
for (auto& edge : edges) {
distances[edge[0]][edge[1]] = edge[2];
distances[edge[1]][edge[0]] = edge[2];
}
unordered_map<int, unordered_map<int, int>> node2nodeVisited;
vector<int> visited(n, INT_MAX);
priority_queue<P, vector<P>, greater<P>> pq;
pq.push({0, 0});
visited[0] = 0;
int res = 0;
while (!pq.empty()) {
auto [dist, node] = pq.top();
pq.pop();
for (auto& [neighbor, length] : distances[node]) {
node2nodeVisited[node][neighbor] = max(node2nodeVisited[node][neighbor], maxMoves - dist);
if (dist + length + 1 <= maxMoves) {
if (dist + length + 1 < visited[neighbor]) {
visited[neighbor] = dist + length + 1;
pq.push({dist + length + 1, neighbor});
}
}
}
}
for (auto& node : visited) {
if (node != INT_MAX) res++;
}
for (auto& edge : edges) {
int plus = min(distances[edge[0]][edge[1]], node2nodeVisited[edge[0]][edge[1]] + node2nodeVisited[edge[1]][edge[0]]);
res += plus;
}
return res;
}
};