1212public class Solution {
1313 public int minCost (int maxTime , int [][] edges , int [] passingFees ) {
1414 final PriorityQueue <Tuple > pq =
15- new PriorityQueue <>(
16- (a , b ) -> {
17- if (a .cost != b .cost ) {
18- return a .cost - b .cost ;
19- }
20- return a .time - b .time ;
21- });
15+ new PriorityQueue <>((a , b ) -> a .cost == b .cost ? a .time - b .time : a .cost - b .cost );
2216 final int n = passingFees .length ;
2317 final int [] minTime = new int [n ];
2418 Arrays .fill (minTime , Integer .MAX_VALUE );
@@ -29,22 +23,18 @@ public int minCost(int maxTime, int[][] edges, int[] passingFees) {
2923 pq .offer (new Tuple (0 , passingFees [0 ], 0 ));
3024 while (!pq .isEmpty ()) {
3125 final Tuple curr = pq .poll ();
32- if (curr .time > maxTime || curr .time >= minTime [curr .node ]) {
33- continue ;
34- }
35- minTime [curr .node ] = curr .time ;
36- if (curr .node == n - 1 ) {
37- return curr .cost ;
38- }
39- for (final Edge edge : graph .getEdges (curr .node )) {
40- final int time = curr .time + edge .weight ;
41- if (time > maxTime ) {
42- continue ;
26+ if (curr .time <= maxTime && curr .time < minTime [curr .node ]) {
27+ minTime [curr .node ] = curr .time ;
28+ if (curr .node == n - 1 ) {
29+ return curr .cost ;
4330 }
44- if (time >= minTime [edge .dst ]) {
45- continue ;
31+ for (final Edge edge : graph .getEdges (curr .node )) {
32+ final int time = curr .time + edge .weight ;
33+ if (time > maxTime || time >= minTime [edge .dst ]) {
34+ continue ;
35+ }
36+ pq .offer (new Tuple (edge .dst , curr .cost + passingFees [edge .dst ], time ));
4637 }
47- pq .offer (new Tuple (edge .dst , curr .cost + passingFees [edge .dst ], time ));
4838 }
4939 }
5040 return -1 ;
@@ -54,8 +44,8 @@ private static class Graph {
5444 private final Map <Integer , List <Edge >> edges = new HashMap <>();
5545
5646 private void addEdge (final int src , final int dst , final int weight ) {
57- this .edges .computeIfAbsent (src , k -> new ArrayList <>()).add (new Edge (src , dst , weight ));
58- this .edges .computeIfAbsent (dst , k -> new ArrayList <>()).add (new Edge (dst , src , weight ));
47+ this .edges .computeIfAbsent (src , k -> new ArrayList <>()).add (new Edge (dst , weight ));
48+ this .edges .computeIfAbsent (dst , k -> new ArrayList <>()).add (new Edge (src , weight ));
5949 }
6050
6151 private List <Edge > getEdges (final int node ) {
@@ -64,19 +54,13 @@ private List<Edge> getEdges(final int node) {
6454 }
6555
6656 private static final class Edge {
67- private final int src ;
6857 private final int dst ;
6958 private final int weight ;
7059
71- private Edge (final int src , final int dst , final int weight ) {
72- this .src = src ;
60+ private Edge (final int dst , final int weight ) {
7361 this .dst = dst ;
7462 this .weight = weight ;
7563 }
76-
77- public String toString () {
78- return "(" + src + "," + dst + "," + weight + ")" ;
79- }
8064 }
8165
8266 private static class Tuple {
@@ -89,9 +73,5 @@ private Tuple(final int node, final int cost, final int time) {
8973 this .cost = cost ;
9074 this .time = time ;
9175 }
92-
93- public String toString () {
94- return "(" + node + "," + cost + "," + time + ")" ;
95- }
9676 }
9777}
0 commit comments