diff --git a/H0ngJu/README.md b/H0ngJu/README.md index 6ac80a4..ca2eabf 100644 --- a/H0ngJu/README.md +++ b/H0ngJu/README.md @@ -29,5 +29,5 @@ | 25차시 | 2024.08.17 | DFS | [친구비](https://www.acmicpc.net/problem/16562) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/234 | | 26차시 | 2024.08.24 | 그리디 | [신입사원](https://www.acmicpc.net/problem/1946) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/237 | | 27차시 | 2024.08.27 | DFS | [트리의 지름](https://www.acmicpc.net/problem/1967) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/240 | - +| 28차시 | 2024.09.04 | 벨만포드 | [타임머신](https://www.acmicpc.net/problem/11657) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/244 | --- diff --git "a/H0ngJu/\353\262\250\353\247\214\355\217\254\353\223\234/\355\203\200\354\236\204\353\250\270\354\213\240.py" "b/H0ngJu/\353\262\250\353\247\214\355\217\254\353\223\234/\355\203\200\354\236\204\353\250\270\354\213\240.py" new file mode 100644 index 0000000..5fad938 --- /dev/null +++ "b/H0ngJu/\353\262\250\353\247\214\355\217\254\353\223\234/\355\203\200\354\236\204\353\250\270\354\213\240.py" @@ -0,0 +1,36 @@ +import sys + +INF = 1e8 + +def input() : return sys.stdin.readline().rstrip() + +N, M = map(int, input().split()) +bus = [[] for _ in range(N+1)] +distance = [INF] * (N+1) + +for i in range(M): + A, B, C = map(int, input().split()) + bus[A].append([B, C]) + +def BellmanFord(start): + distance[start] = 0 + + for i in range(N): + for j in range(1, N+1): + for n, w in bus[j]: + if distance[j] != INF and distance[n] > distance[j] + w: + distance[n] = distance[j] + w + if i == N-1: + return True + return False + +check_negative = BellmanFord(1) + +if check_negative: + print("-1") +else: + for i in range(2, N+1): + if distance[i] == INF: + print("-1") + else: + print(distance[i])