Skip to content

Commit

Permalink
solve(BOJ): G4_11657_타임머신_kt
Browse files Browse the repository at this point in the history
  • Loading branch information
gogumaC committed Jan 12, 2024
1 parent 7a0c1f2 commit ffd815c
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/boj/G4_11657_타임머신.kt
Original file line number Diff line number Diff line change
@@ -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<Edge>() }
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()
}

0 comments on commit ffd815c

Please sign in to comment.