From ffd815c95d738fb6ce89e1325e77d95dc441cff4 Mon Sep 17 00:00:00 2001 From: yubin Date: Fri, 12 Jan 2024 23:10:12 +0900 Subject: [PATCH] =?UTF-8?q?solve(BOJ):=20G4=5F11657=5F=ED=83=80=EC=9E=84?= =?UTF-8?q?=EB=A8=B8=EC=8B=A0=5Fkt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\354\236\204\353\250\270\354\213\240.kt" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 "src/boj/G4_11657_\355\203\200\354\236\204\353\250\270\354\213\240.kt" diff --git "a/src/boj/G4_11657_\355\203\200\354\236\204\353\250\270\354\213\240.kt" "b/src/boj/G4_11657_\355\203\200\354\236\204\353\250\270\354\213\240.kt" new file mode 100644 index 0000000..8e92bd5 --- /dev/null +++ "b/src/boj/G4_11657_\355\203\200\354\236\204\353\250\270\354\213\240.kt" @@ -0,0 +1,54 @@ +package boj + +import kotlin.math.min + +class BOJ11657() { + data class Edge(val start: Int, val end: Int, val weight: Int) + + val INF = Long.MAX_VALUE / 2 + fun solve() { + val (N, M) = readln().split(" ").map { it.toInt() } + + val map = Array(N + 1) { mutableListOf() } + for (i in 0 until M) { + val (start, end, weight) = readln().split(" ").map { it.toInt() } + map[start].add(Edge(start, end, weight)) + } + + val dist = LongArray(N + 1) { INF } + dist[1] = 0 + + for (i in 2..N) { + for (j in 1..N) { + if (dist[j] == INF) continue + map[j].forEach { + dist[it.end] = min(dist[it.start] + it.weight, dist[it.end]) + } + } + } + + for (i in 1..N) { + if (dist[i] == INF) continue + map[i].forEach { + if (dist[it.end] != min(dist[it.start] + it.weight, dist[it.end])) { + println(-1) + return + } + } + } + + with(StringBuilder()) { + for (i in 2..N) { + if (dist[i] != INF) append(dist[i]) + else append(-1) + append("\n") + } + + print(this) + } + } +} + +fun main() { + BOJ11657().solve() +} \ No newline at end of file