BOJ - 11657 - 타임머신

Updated:

import sys
INF = sys.maxsize

def solution():
    N, M = map(int, sys.stdin.readline().split())

    bf = [INF] * (N + 1)
    graph = []
    for _ in range(M):
        graph.append(list(map(int, sys.stdin.readline().split())))

    bf[1] = 0
    minusFlag = False
    for i in range(N):
        for j in range(M):
            v = graph[j][0]
            nv = graph[j][1]
            w = graph[j][2]

            if bf[v] != INF and bf[nv] > bf[v] + w:
                bf[nv] = bf[v] + w
                if i == N-1:
                    minusFlag = True

    if minusFlag: print(-1)
    else:
        for i in range(2, N+1):
            if bf[i] == INF: print(-1)
            else: print(bf[i])

solution()

https://www.acmicpc.net/problem/11657

Categories:

Updated:

Leave a comment