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
| class Solution { private static class Node { private int id; private int distance;
private static Node of(int id, int distance) { Node node = new Node(); node.id = id; node.distance = distance; return node; } }
public int[] minimumTime(int n, int[][] edges, int[] disappear) { List<List<Node>> grid = new ArrayList<>(n); for (int i = 0; i < n; i++) { grid.add(new ArrayList<>()); } for (int[] edge : edges) { grid.get(edge[0]).add(Node.of(edge[1], edge[2])); grid.get(edge[1]).add(Node.of(edge[0], edge[2])); } int[] ans = new int[n]; Arrays.fill(ans, -1); ans[0] = 0; PriorityQueue<Node> priorityQueue = new PriorityQueue<>((node1, node2) -> node1.distance - node2.distance); priorityQueue.add(Node.of(0, 0)); while (!priorityQueue.isEmpty()) { Node node = priorityQueue.poll(); if (node.distance > ans[node.id]) { continue; } for (Node nextNode : grid.get(node.id)) { if (node.distance + nextNode.distance < disappear[nextNode.id] && (ans[nextNode.id] == -1 || node.distance + nextNode.distance < ans[nextNode.id])) { ans[nextNode.id] = node.distance + nextNode.distance; priorityQueue.offer(Node.of(nextNode.id, ans[nextNode.id])); } } } return ans; } }
|