LoginSignup
0
1

More than 1 year has passed since last update.

Swift で Floyd-Warshall Algorithm (ワーシャル・フロイド法)使ってAPSP:All Pair Shortest Path(全点対最短経路問題)

Last updated at Posted at 2021-09-07

備忘のメモ

NOTE

  • 実装:2次元配列のDP

コード

// init
let INF = 1_000_000
var dp = [[Int]](repeating: [Int](repeating: INF, count: N), count: N) // set inf
for i in 0..<N { dp[i][i] = 0 } // zero reset
for (from, to, cost) in list { dp[from][to] = cost } // set initial val

// floyd-warshall
for k in 0..<N {
    for i in 0..<N {
        for j in 0..<N {
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
        }
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1