@@ -112,10 +112,14 @@ class Solution2 {
112
112
const auto & floydWarshall = [](auto & dist) {
113
113
for (int k = 0 ; k < size (dist); ++k) {
114
114
for (int i = 0 ; i < size (dist); ++i) {
115
+ if (dist[i][k] == INF) {
116
+ continue ;
117
+ }
115
118
for (int j = 0 ; j < size (dist[0 ]); ++j) {
116
- if (dist[i][k] != INF && dist[k][ j] ! = INF) {
117
- dist[i][j] = min (dist[i][j], dist[i][k] + dist[k][j]) ;
119
+ if (dist[k][ j] = = INF) {
120
+ continue ;
118
121
}
122
+ dist[i][j] = min (dist[i][j], dist[i][k] + dist[k][j]);
119
123
}
120
124
}
121
125
}
@@ -288,10 +292,14 @@ class Solution4 {
288
292
const auto & floydWarshall = [](auto & dist) {
289
293
for (const auto & [k, _] : dist) {
290
294
for (const auto & [i, _] : dist) {
295
+ if (dist[i][k] == INF) {
296
+ continue ;
297
+ }
291
298
for (const auto & [j, _] : dist) {
292
- if (dist[i][k] != INF && dist[k][ j] ! = INF) {
293
- dist[i][j] = min (dist[i][j], dist[i][k] + dist[k][j]) ;
299
+ if (dist[k][ j] = = INF) {
300
+ continue ;
294
301
}
302
+ dist[i][j] = min (dist[i][j], dist[i][k] + dist[k][j]);
295
303
}
296
304
}
297
305
}
@@ -564,10 +572,14 @@ class Solution6 {
564
572
const auto & floydWarshall = [](auto & dist) {
565
573
for (const auto & [k, _] : dist) {
566
574
for (const auto & [i, _] : dist) {
575
+ if (dist[i][k] == INF) {
576
+ continue ;
577
+ }
567
578
for (const auto & [j, _] : dist) {
568
- if (dist[i][k] != INF && dist[k][ j] ! = INF) {
569
- dist[i][j] = min (dist[i][j], dist[i][k] + dist[k][j]) ;
579
+ if (dist[k][ j] = = INF) {
580
+ continue ;
570
581
}
582
+ dist[i][j] = min (dist[i][j], dist[i][k] + dist[k][j]);
571
583
}
572
584
}
573
585
}
0 commit comments