@@ -23,6 +23,9 @@ public final class YensKShortestPaths {
2323 private YensKShortestPaths () {
2424 }
2525
26+ private static final int NO_EDGE = -1 ;
27+ private static final long INF_COST = Long .MAX_VALUE / 4 ;
28+
2629 /**
2730 * Compute up to k loopless shortest paths from src to dst using Yen's algorithm.
2831 *
@@ -66,7 +69,7 @@ public static List<List<Integer>> kShortestPaths(int[][] weights, int src, int d
6669 if (startsWith (p .nodes , rootPath ) && p .nodes .size () > i + 1 ) {
6770 int u = p .nodes .get (i );
6871 int v = p .nodes .get (i + 1 );
69- modifiedWeights [u ][v ] = - 1 ; // remove edge
72+ modifiedWeights [u ][v ] = NO_EDGE ; // remove edge
7073 }
7174 }
7275 // Prevent revisiting nodes in rootPath (loopless constraint), except spurNode itself
@@ -117,7 +120,7 @@ private static void validate(int[][] weights, int src, int dst, int k) {
117120 }
118121 for (int j = 0 ; j < n ; j ++) {
119122 int val = weights [i ][j ];
120- if (val < - 1 ) {
123+ if (val < NO_EDGE ) {
121124 throw new IllegalArgumentException ("Weights must be -1 (no edge) or >= 0" );
122125 }
123126 }
@@ -159,18 +162,18 @@ private static long pathCost(int[][] weights, List<Integer> nodes) {
159162 for (int i = 0 ; i + 1 < nodes .size (); i ++) {
160163 int u = nodes .get (i );
161164 int v = nodes .get (i + 1 );
162- int c = weights [u ][v ];
163- if (c < 0 ) {
164- return Long . MAX_VALUE / 4 ; // invalid
165+ int edgeCost = weights [u ][v ];
166+ if (edgeCost < 0 ) {
167+ return INF_COST ; // invalid
165168 }
166- cost += c ;
169+ cost += edgeCost ;
167170 }
168171 return cost ;
169172 }
170173
171174 private static Path dijkstra (int [][] weights , int src , int dst , boolean [] blocked ) {
172175 int n = weights .length ;
173- final long inf = Long . MAX_VALUE / 4 ;
176+ final long inf = INF_COST ;
174177 long [] dist = new long [n ];
175178 int [] parent = new int [n ];
176179 Arrays .fill (dist , inf );
@@ -244,13 +247,13 @@ String key() {
244247 }
245248 @ Override
246249 public int compareTo (Path o ) {
247- int c = Long .compare (this .cost , o .cost );
248- if (c != 0 ) {
249- return c ;
250+ int costCmp = Long .compare (this .cost , o .cost );
251+ if (costCmp != 0 ) {
252+ return costCmp ;
250253 }
251254 // tie-break lexicographically on nodes
252- int m = Math .min (this .nodes .size (), o .nodes .size ());
253- for (int i = 0 ; i < m ; i ++) {
255+ int minLength = Math .min (this .nodes .size (), o .nodes .size ());
256+ for (int i = 0 ; i < minLength ; i ++) {
254257 int aNode = this .nodes .get (i );
255258 int bNode = o .nodes .get (i );
256259 if (aNode != bNode ) {
0 commit comments