From bfd887e37c29802688d0feae6a70ad4f1ec13621 Mon Sep 17 00:00:00 2001 From: H0ngJu Date: Wed, 4 Sep 2024 14:13:21 +0900 Subject: [PATCH] 2024-09-04 --- H0ngJu/README.md | 10 +++--- ...00\354\236\204\353\250\270\354\213\240.py" | 36 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 "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" diff --git a/H0ngJu/README.md b/H0ngJu/README.md index 957950e..3546be9 100644 --- a/H0ngJu/README.md +++ b/H0ngJu/README.md @@ -24,9 +24,11 @@ | 20차시 | 2024.06.03 | 백트래킹 | [스타트와 링크](https://www.acmicpc.net/problem/14889) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/206 | | 21차시 | 2024.06.07 | 그리디 | [행복 유치원](https://www.acmicpc.net/problem/13164) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/208 | | 22차시 | 2024.08.06 | 해시 | [의상](https://school.programmers.co.kr/learn/courses/30/lessons/42578) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/224 | -| 23차시 | 2024.08.10 | 해시 | [베스트앨범](https://school.programmers.co.kr/learn/courses/30/lessons/42579) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/227 | -| 24차시 | 2024.08.17 | BFS | [아기상어](https://www.acmicpc.net/problem/16236) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/233 | -| 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 | +| 23차시 | 2024.08.10 | 해시 | [베스트앨범](https://school.programmers.co.kr/learn/courses/30/lessons/42579) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/227 | +| 24차시 | 2024.08.17 | BFS | [아기상어](https://www.acmicpc.net/problem/16236) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/233 | +| 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 | + +| 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])