← Back

Floyd-Warshall (All-Pairs Shortest Paths)

Shortest paths between every pair of nodes with three nested loops.

1 / 1
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like math.MaxInt64/2 so k-path sums don't overflow).
// floydWarshall relaxes it in place into all-pairs shortest distances.
func floydWarshall(dist [][]int) {

}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like math.MaxInt64/2 so k-path sums don't overflow).
// floydWarshall relaxes it in place into all-pairs shortest distances.
func floydWarshall(dist [][]int) {

    // Core idea: grow the set of allowed intermediate vertices one at a time.
    // After iteration k, dist[i][j] is the best path using only 0..k as stops.
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like math.MaxInt64/2 so k-path sums don't overflow).
// floydWarshall relaxes it in place into all-pairs shortest distances.
func floydWarshall(dist [][]int) {
    n := len(dist)

    // Core idea: grow the set of allowed intermediate vertices one at a time.
    // After iteration k, dist[i][j] is the best path using only 0..k as stops.
    for k := 0; k < n; k++ {
    }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like math.MaxInt64/2 so k-path sums don't overflow).
// floydWarshall relaxes it in place into all-pairs shortest distances.
func floydWarshall(dist [][]int) {
    n := len(dist)

    // Core idea: grow the set of allowed intermediate vertices one at a time.
    // After iteration k, dist[i][j] is the best path using only 0..k as stops.
    for k := 0; k < n; k++ {
        // With k now allowed as a waypoint, retry every source/target pair.
    }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like math.MaxInt64/2 so k-path sums don't overflow).
// floydWarshall relaxes it in place into all-pairs shortest distances.
func floydWarshall(dist [][]int) {
    n := len(dist)

    // Core idea: grow the set of allowed intermediate vertices one at a time.
    // After iteration k, dist[i][j] is the best path using only 0..k as stops.
    for k := 0; k < n; k++ {
        // With k now allowed as a waypoint, retry every source/target pair.
        for i := 0; i < n; i++ {
            for j := 0; j < n; j++ {
            }
        }
    }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like math.MaxInt64/2 so k-path sums don't overflow).
// floydWarshall relaxes it in place into all-pairs shortest distances.
func floydWarshall(dist [][]int) {
    n := len(dist)

    // Core idea: grow the set of allowed intermediate vertices one at a time.
    // After iteration k, dist[i][j] is the best path using only 0..k as stops.
    for k := 0; k < n; k++ {
        // With k now allowed as a waypoint, retry every source/target pair.
        for i := 0; i < n; i++ {
            for j := 0; j < n; j++ {
                // Is detouring i -> k -> j shorter than the best i -> j so far?
            }
        }
    }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like math.MaxInt64/2 so k-path sums don't overflow).
// floydWarshall relaxes it in place into all-pairs shortest distances.
func floydWarshall(dist [][]int) {
    n := len(dist)

    // Core idea: grow the set of allowed intermediate vertices one at a time.
    // After iteration k, dist[i][j] is the best path using only 0..k as stops.
    for k := 0; k < n; k++ {
        // With k now allowed as a waypoint, retry every source/target pair.
        for i := 0; i < n; i++ {
            for j := 0; j < n; j++ {
                // Is detouring i -> k -> j shorter than the best i -> j so far?
                if dist[i][k]+dist[k][j] < dist[i][j] {
                    dist[i][j] = dist[i][k] + dist[k][j]
                }
            }
        }
    }
}
# dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
# edge" (use something like 10**18 so k-path sums stay comparable).
# floyd_warshall relaxes it in place into all-pairs shortest distances.
def floyd_warshall(dist):

    pass
# dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
# edge" (use something like 10**18 so k-path sums stay comparable).
# floyd_warshall relaxes it in place into all-pairs shortest distances.
def floyd_warshall(dist):

    # Core idea: grow the set of allowed intermediate vertices one at a time.
    # After iteration k, dist[i][j] is the best path using only 0..k as stops.
    pass
# dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
# edge" (use something like 10**18 so k-path sums stay comparable).
# floyd_warshall relaxes it in place into all-pairs shortest distances.
def floyd_warshall(dist):
    n = len(dist)

    # Core idea: grow the set of allowed intermediate vertices one at a time.
    # After iteration k, dist[i][j] is the best path using only 0..k as stops.
    for k in range(n):
        pass
# dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
# edge" (use something like 10**18 so k-path sums stay comparable).
# floyd_warshall relaxes it in place into all-pairs shortest distances.
def floyd_warshall(dist):
    n = len(dist)

    # Core idea: grow the set of allowed intermediate vertices one at a time.
    # After iteration k, dist[i][j] is the best path using only 0..k as stops.
    for k in range(n):
        # With k now allowed as a waypoint, retry every source/target pair.
        pass
# dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
# edge" (use something like 10**18 so k-path sums stay comparable).
# floyd_warshall relaxes it in place into all-pairs shortest distances.
def floyd_warshall(dist):
    n = len(dist)

    # Core idea: grow the set of allowed intermediate vertices one at a time.
    # After iteration k, dist[i][j] is the best path using only 0..k as stops.
    for k in range(n):
        # With k now allowed as a waypoint, retry every source/target pair.
        for i in range(n):
            for j in range(n):
                pass
# dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
# edge" (use something like 10**18 so k-path sums stay comparable).
# floyd_warshall relaxes it in place into all-pairs shortest distances.
def floyd_warshall(dist):
    n = len(dist)

    # Core idea: grow the set of allowed intermediate vertices one at a time.
    # After iteration k, dist[i][j] is the best path using only 0..k as stops.
    for k in range(n):
        # With k now allowed as a waypoint, retry every source/target pair.
        for i in range(n):
            for j in range(n):
                # Is detouring i -> k -> j shorter than the best i -> j so far?
                pass
# dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
# edge" (use something like 10**18 so k-path sums stay comparable).
# floyd_warshall relaxes it in place into all-pairs shortest distances.
def floyd_warshall(dist):
    n = len(dist)

    # Core idea: grow the set of allowed intermediate vertices one at a time.
    # After iteration k, dist[i][j] is the best path using only 0..k as stops.
    for k in range(n):
        # With k now allowed as a waypoint, retry every source/target pair.
        for i in range(n):
            for j in range(n):
                # Is detouring i -> k -> j shorter than the best i -> j so far?
                if dist[i][k] + dist[k][j] < dist[i][j]:
                    dist[i][j] = dist[i][k] + dist[k][j]
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like Number.MAX_SAFE_INTEGER / 2 so k-path sums stay exact).
// floydWarshall relaxes it in place into all-pairs shortest distances.
function floydWarshall(dist) {

}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like Number.MAX_SAFE_INTEGER / 2 so k-path sums stay exact).
// floydWarshall relaxes it in place into all-pairs shortest distances.
function floydWarshall(dist) {

  // Core idea: grow the set of allowed intermediate vertices one at a time.
  // After iteration k, dist[i][j] is the best path using only 0..k as stops.
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like Number.MAX_SAFE_INTEGER / 2 so k-path sums stay exact).
// floydWarshall relaxes it in place into all-pairs shortest distances.
function floydWarshall(dist) {
  const n = dist.length;

  // Core idea: grow the set of allowed intermediate vertices one at a time.
  // After iteration k, dist[i][j] is the best path using only 0..k as stops.
  for (let k = 0; k < n; k++) {
  }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like Number.MAX_SAFE_INTEGER / 2 so k-path sums stay exact).
// floydWarshall relaxes it in place into all-pairs shortest distances.
function floydWarshall(dist) {
  const n = dist.length;

  // Core idea: grow the set of allowed intermediate vertices one at a time.
  // After iteration k, dist[i][j] is the best path using only 0..k as stops.
  for (let k = 0; k < n; k++) {
    // With k now allowed as a waypoint, retry every source/target pair.
  }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like Number.MAX_SAFE_INTEGER / 2 so k-path sums stay exact).
// floydWarshall relaxes it in place into all-pairs shortest distances.
function floydWarshall(dist) {
  const n = dist.length;

  // Core idea: grow the set of allowed intermediate vertices one at a time.
  // After iteration k, dist[i][j] is the best path using only 0..k as stops.
  for (let k = 0; k < n; k++) {
    // With k now allowed as a waypoint, retry every source/target pair.
    for (let i = 0; i < n; i++) {
      for (let j = 0; j < n; j++) {
      }
    }
  }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like Number.MAX_SAFE_INTEGER / 2 so k-path sums stay exact).
// floydWarshall relaxes it in place into all-pairs shortest distances.
function floydWarshall(dist) {
  const n = dist.length;

  // Core idea: grow the set of allowed intermediate vertices one at a time.
  // After iteration k, dist[i][j] is the best path using only 0..k as stops.
  for (let k = 0; k < n; k++) {
    // With k now allowed as a waypoint, retry every source/target pair.
    for (let i = 0; i < n; i++) {
      for (let j = 0; j < n; j++) {
        // Is detouring i -> k -> j shorter than the best i -> j so far?
      }
    }
  }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like Number.MAX_SAFE_INTEGER / 2 so k-path sums stay exact).
// floydWarshall relaxes it in place into all-pairs shortest distances.
function floydWarshall(dist) {
  const n = dist.length;

  // Core idea: grow the set of allowed intermediate vertices one at a time.
  // After iteration k, dist[i][j] is the best path using only 0..k as stops.
  for (let k = 0; k < n; k++) {
    // With k now allowed as a waypoint, retry every source/target pair.
    for (let i = 0; i < n; i++) {
      for (let j = 0; j < n; j++) {
        // Is detouring i -> k -> j shorter than the best i -> j so far?
        if (dist[i][k] + dist[k][j] < dist[i][j]) {
          dist[i][j] = dist[i][k] + dist[k][j];
        }
      }
    }
  }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like Number.MAX_SAFE_INTEGER / 2 so k-path sums stay exact).
// floydWarshall relaxes it in place into all-pairs shortest distances.
function floydWarshall(dist: number[][]): void {

}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like Number.MAX_SAFE_INTEGER / 2 so k-path sums stay exact).
// floydWarshall relaxes it in place into all-pairs shortest distances.
function floydWarshall(dist: number[][]): void {

  // Core idea: grow the set of allowed intermediate vertices one at a time.
  // After iteration k, dist[i][j] is the best path using only 0..k as stops.
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like Number.MAX_SAFE_INTEGER / 2 so k-path sums stay exact).
// floydWarshall relaxes it in place into all-pairs shortest distances.
function floydWarshall(dist: number[][]): void {
  const n = dist.length;

  // Core idea: grow the set of allowed intermediate vertices one at a time.
  // After iteration k, dist[i][j] is the best path using only 0..k as stops.
  for (let k = 0; k < n; k++) {
  }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like Number.MAX_SAFE_INTEGER / 2 so k-path sums stay exact).
// floydWarshall relaxes it in place into all-pairs shortest distances.
function floydWarshall(dist: number[][]): void {
  const n = dist.length;

  // Core idea: grow the set of allowed intermediate vertices one at a time.
  // After iteration k, dist[i][j] is the best path using only 0..k as stops.
  for (let k = 0; k < n; k++) {
    // With k now allowed as a waypoint, retry every source/target pair.
  }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like Number.MAX_SAFE_INTEGER / 2 so k-path sums stay exact).
// floydWarshall relaxes it in place into all-pairs shortest distances.
function floydWarshall(dist: number[][]): void {
  const n = dist.length;

  // Core idea: grow the set of allowed intermediate vertices one at a time.
  // After iteration k, dist[i][j] is the best path using only 0..k as stops.
  for (let k = 0; k < n; k++) {
    // With k now allowed as a waypoint, retry every source/target pair.
    for (let i = 0; i < n; i++) {
      for (let j = 0; j < n; j++) {
      }
    }
  }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like Number.MAX_SAFE_INTEGER / 2 so k-path sums stay exact).
// floydWarshall relaxes it in place into all-pairs shortest distances.
function floydWarshall(dist: number[][]): void {
  const n = dist.length;

  // Core idea: grow the set of allowed intermediate vertices one at a time.
  // After iteration k, dist[i][j] is the best path using only 0..k as stops.
  for (let k = 0; k < n; k++) {
    // With k now allowed as a waypoint, retry every source/target pair.
    for (let i = 0; i < n; i++) {
      for (let j = 0; j < n; j++) {
        // Is detouring i -> k -> j shorter than the best i -> j so far?
      }
    }
  }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like Number.MAX_SAFE_INTEGER / 2 so k-path sums stay exact).
// floydWarshall relaxes it in place into all-pairs shortest distances.
function floydWarshall(dist: number[][]): void {
  const n = dist.length;

  // Core idea: grow the set of allowed intermediate vertices one at a time.
  // After iteration k, dist[i][j] is the best path using only 0..k as stops.
  for (let k = 0; k < n; k++) {
    // With k now allowed as a waypoint, retry every source/target pair.
    for (let i = 0; i < n; i++) {
      for (let j = 0; j < n; j++) {
        // Is detouring i -> k -> j shorter than the best i -> j so far?
        if (dist[i][k] + dist[k][j] < dist[i][j]) {
          dist[i][j] = dist[i][k] + dist[k][j];
        }
      }
    }
  }
}
class Solution {
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like Long.MAX_VALUE / 2 so k-path sums don't overflow).
    // floydWarshall relaxes it in place into all-pairs shortest distances.
    public void floydWarshall(long[][] dist) {

    }
}
class Solution {
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like Long.MAX_VALUE / 2 so k-path sums don't overflow).
    // floydWarshall relaxes it in place into all-pairs shortest distances.
    public void floydWarshall(long[][] dist) {

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
    }
}
class Solution {
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like Long.MAX_VALUE / 2 so k-path sums don't overflow).
    // floydWarshall relaxes it in place into all-pairs shortest distances.
    public void floydWarshall(long[][] dist) {
        int n = dist.length;

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
        for (int k = 0; k < n; k++) {
        }
    }
}
class Solution {
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like Long.MAX_VALUE / 2 so k-path sums don't overflow).
    // floydWarshall relaxes it in place into all-pairs shortest distances.
    public void floydWarshall(long[][] dist) {
        int n = dist.length;

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
        for (int k = 0; k < n; k++) {
            // With k now allowed as a waypoint, retry every source/target pair.
        }
    }
}
class Solution {
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like Long.MAX_VALUE / 2 so k-path sums don't overflow).
    // floydWarshall relaxes it in place into all-pairs shortest distances.
    public void floydWarshall(long[][] dist) {
        int n = dist.length;

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
        for (int k = 0; k < n; k++) {
            // With k now allowed as a waypoint, retry every source/target pair.
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                }
            }
        }
    }
}
class Solution {
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like Long.MAX_VALUE / 2 so k-path sums don't overflow).
    // floydWarshall relaxes it in place into all-pairs shortest distances.
    public void floydWarshall(long[][] dist) {
        int n = dist.length;

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
        for (int k = 0; k < n; k++) {
            // With k now allowed as a waypoint, retry every source/target pair.
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    // Is detouring i -> k -> j shorter than the best i -> j so far?
                }
            }
        }
    }
}
class Solution {
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like Long.MAX_VALUE / 2 so k-path sums don't overflow).
    // floydWarshall relaxes it in place into all-pairs shortest distances.
    public void floydWarshall(long[][] dist) {
        int n = dist.length;

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
        for (int k = 0; k < n; k++) {
            // With k now allowed as a waypoint, retry every source/target pair.
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    // Is detouring i -> k -> j shorter than the best i -> j so far?
                    if (dist[i][k] + dist[k][j] < dist[i][j]) {
                        dist[i][j] = dist[i][k] + dist[k][j];
                    }
                }
            }
        }
    }
}
public class Solution {
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like long.MaxValue / 2 so k-path sums don't overflow).
    // FloydWarshall relaxes it in place into all-pairs shortest distances.
    public void FloydWarshall(long[][] dist) {

    }
}
public class Solution {
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like long.MaxValue / 2 so k-path sums don't overflow).
    // FloydWarshall relaxes it in place into all-pairs shortest distances.
    public void FloydWarshall(long[][] dist) {

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
    }
}
public class Solution {
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like long.MaxValue / 2 so k-path sums don't overflow).
    // FloydWarshall relaxes it in place into all-pairs shortest distances.
    public void FloydWarshall(long[][] dist) {
        int n = dist.Length;

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
        for (int k = 0; k < n; k++) {
        }
    }
}
public class Solution {
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like long.MaxValue / 2 so k-path sums don't overflow).
    // FloydWarshall relaxes it in place into all-pairs shortest distances.
    public void FloydWarshall(long[][] dist) {
        int n = dist.Length;

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
        for (int k = 0; k < n; k++) {
            // With k now allowed as a waypoint, retry every source/target pair.
        }
    }
}
public class Solution {
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like long.MaxValue / 2 so k-path sums don't overflow).
    // FloydWarshall relaxes it in place into all-pairs shortest distances.
    public void FloydWarshall(long[][] dist) {
        int n = dist.Length;

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
        for (int k = 0; k < n; k++) {
            // With k now allowed as a waypoint, retry every source/target pair.
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                }
            }
        }
    }
}
public class Solution {
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like long.MaxValue / 2 so k-path sums don't overflow).
    // FloydWarshall relaxes it in place into all-pairs shortest distances.
    public void FloydWarshall(long[][] dist) {
        int n = dist.Length;

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
        for (int k = 0; k < n; k++) {
            // With k now allowed as a waypoint, retry every source/target pair.
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    // Is detouring i -> k -> j shorter than the best i -> j so far?
                }
            }
        }
    }
}
public class Solution {
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like long.MaxValue / 2 so k-path sums don't overflow).
    // FloydWarshall relaxes it in place into all-pairs shortest distances.
    public void FloydWarshall(long[][] dist) {
        int n = dist.Length;

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
        for (int k = 0; k < n; k++) {
            // With k now allowed as a waypoint, retry every source/target pair.
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    // Is detouring i -> k -> j shorter than the best i -> j so far?
                    if (dist[i][k] + dist[k][j] < dist[i][j]) {
                        dist[i][j] = dist[i][k] + dist[k][j];
                    }
                }
            }
        }
    }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like LLONG_MAX / 2 so k-path sums don't overflow).
// floydWarshall relaxes the n x n matrix in place into all-pairs shortest
// distances.
void floydWarshall(long long** dist, int n) {

}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like LLONG_MAX / 2 so k-path sums don't overflow).
// floydWarshall relaxes the n x n matrix in place into all-pairs shortest
// distances.
void floydWarshall(long long** dist, int n) {

    // Core idea: grow the set of allowed intermediate vertices one at a time.
    // After iteration k, dist[i][j] is the best path using only 0..k as stops.
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like LLONG_MAX / 2 so k-path sums don't overflow).
// floydWarshall relaxes the n x n matrix in place into all-pairs shortest
// distances.
void floydWarshall(long long** dist, int n) {

    // Core idea: grow the set of allowed intermediate vertices one at a time.
    // After iteration k, dist[i][j] is the best path using only 0..k as stops.
    for (int k = 0; k < n; k++) {
    }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like LLONG_MAX / 2 so k-path sums don't overflow).
// floydWarshall relaxes the n x n matrix in place into all-pairs shortest
// distances.
void floydWarshall(long long** dist, int n) {

    // Core idea: grow the set of allowed intermediate vertices one at a time.
    // After iteration k, dist[i][j] is the best path using only 0..k as stops.
    for (int k = 0; k < n; k++) {
        // With k now allowed as a waypoint, retry every source/target pair.
    }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like LLONG_MAX / 2 so k-path sums don't overflow).
// floydWarshall relaxes the n x n matrix in place into all-pairs shortest
// distances.
void floydWarshall(long long** dist, int n) {

    // Core idea: grow the set of allowed intermediate vertices one at a time.
    // After iteration k, dist[i][j] is the best path using only 0..k as stops.
    for (int k = 0; k < n; k++) {
        // With k now allowed as a waypoint, retry every source/target pair.
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
            }
        }
    }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like LLONG_MAX / 2 so k-path sums don't overflow).
// floydWarshall relaxes the n x n matrix in place into all-pairs shortest
// distances.
void floydWarshall(long long** dist, int n) {

    // Core idea: grow the set of allowed intermediate vertices one at a time.
    // After iteration k, dist[i][j] is the best path using only 0..k as stops.
    for (int k = 0; k < n; k++) {
        // With k now allowed as a waypoint, retry every source/target pair.
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                // Is detouring i -> k -> j shorter than the best i -> j so far?
            }
        }
    }
}
// dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
// edge" (use something like LLONG_MAX / 2 so k-path sums don't overflow).
// floydWarshall relaxes the n x n matrix in place into all-pairs shortest
// distances.
void floydWarshall(long long** dist, int n) {

    // Core idea: grow the set of allowed intermediate vertices one at a time.
    // After iteration k, dist[i][j] is the best path using only 0..k as stops.
    for (int k = 0; k < n; k++) {
        // With k now allowed as a waypoint, retry every source/target pair.
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                // Is detouring i -> k -> j shorter than the best i -> j so far?
                if (dist[i][k] + dist[k][j] < dist[i][j]) {
                    dist[i][j] = dist[i][k] + dist[k][j];
                }
            }
        }
    }
}
#include <vector>
using namespace std;

class Solution {
public:
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like LLONG_MAX / 2 so k-path sums don't overflow).
    // floydWarshall relaxes it in place into all-pairs shortest distances.
    void floydWarshall(vector<vector<long long>>& dist) {

    }
};
#include <vector>
using namespace std;

class Solution {
public:
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like LLONG_MAX / 2 so k-path sums don't overflow).
    // floydWarshall relaxes it in place into all-pairs shortest distances.
    void floydWarshall(vector<vector<long long>>& dist) {

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like LLONG_MAX / 2 so k-path sums don't overflow).
    // floydWarshall relaxes it in place into all-pairs shortest distances.
    void floydWarshall(vector<vector<long long>>& dist) {
        int n = (int)dist.size();

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
        for (int k = 0; k < n; k++) {
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like LLONG_MAX / 2 so k-path sums don't overflow).
    // floydWarshall relaxes it in place into all-pairs shortest distances.
    void floydWarshall(vector<vector<long long>>& dist) {
        int n = (int)dist.size();

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
        for (int k = 0; k < n; k++) {
            // With k now allowed as a waypoint, retry every source/target pair.
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like LLONG_MAX / 2 so k-path sums don't overflow).
    // floydWarshall relaxes it in place into all-pairs shortest distances.
    void floydWarshall(vector<vector<long long>>& dist) {
        int n = (int)dist.size();

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
        for (int k = 0; k < n; k++) {
            // With k now allowed as a waypoint, retry every source/target pair.
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                }
            }
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like LLONG_MAX / 2 so k-path sums don't overflow).
    // floydWarshall relaxes it in place into all-pairs shortest distances.
    void floydWarshall(vector<vector<long long>>& dist) {
        int n = (int)dist.size();

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
        for (int k = 0; k < n; k++) {
            // With k now allowed as a waypoint, retry every source/target pair.
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    // Is detouring i -> k -> j shorter than the best i -> j so far?
                }
            }
        }
    }
};
#include <vector>
using namespace std;

class Solution {
public:
    // dist[i][j] holds the edge weight from i to j, or a large sentinel for "no
    // edge" (use something like LLONG_MAX / 2 so k-path sums don't overflow).
    // floydWarshall relaxes it in place into all-pairs shortest distances.
    void floydWarshall(vector<vector<long long>>& dist) {
        int n = (int)dist.size();

        // Core idea: grow the set of allowed intermediate vertices one at a time.
        // After iteration k, dist[i][j] is the best path using only 0..k as stops.
        for (int k = 0; k < n; k++) {
            // With k now allowed as a waypoint, retry every source/target pair.
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    // Is detouring i -> k -> j shorter than the best i -> j so far?
                    if (dist[i][k] + dist[k][j] < dist[i][j]) {
                        dist[i][j] = dist[i][k] + dist[k][j];
                    }
                }
            }
        }
    }
};

Tap the left half to go back, the right half to go forward.

Arrow keys or space to navigate · Esc to exit.