From 78e53811e6953c8686e31e828a5bca171e1c066b Mon Sep 17 00:00:00 2001 From: kangrae Date: Thu, 28 Aug 2025 11:40:34 +0900 Subject: [PATCH] =?UTF-8?q?2025-08-28=20=EC=9B=9C=ED=99=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kangrae-jo/Graph/34-kangrae-jo.cpp | 56 ++++++++++++++++++++++++++++++ kangrae-jo/README.md | 3 +- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 kangrae-jo/Graph/34-kangrae-jo.cpp diff --git a/kangrae-jo/Graph/34-kangrae-jo.cpp b/kangrae-jo/Graph/34-kangrae-jo.cpp new file mode 100644 index 0000000..f7c13b2 --- /dev/null +++ b/kangrae-jo/Graph/34-kangrae-jo.cpp @@ -0,0 +1,56 @@ +#include +#include + +using namespace std; + +const int INF = 1e9; + +bool solution(vector>>& graph, int N) { + vector dist(N + 1, 0); + for (int i = 1; i <= N; i++) { + bool updated = false; + for (int from = 1; from <= N; from++) { + for (auto [to, cost] : graph[from]) { + if (dist[to] > dist[from] + cost) { + dist[to] = dist[from] + cost; + updated = true; + if (i == N) return true; + } + } + } + if (!updated) break; + } + + return false; +} + +int main() { + int TC; + cin >> TC; + + while (TC--) { + int N, M, W; + cin >> N >> M >> W; + + vector>> graph(N + 1); + for (int i = 0; i < M; i++) { + int S, E, T; + cin >> S >> E >> T; + + graph[S].push_back({E, T}); + graph[E].push_back({S, T}); + } + + for (int i = 0; i < W; i++) { + int S, E, T; + cin >> S >> E >> T; + + graph[S].push_back({E, -T}); + } + + if (solution(graph, N)) cout << "YES\n"; + else cout << "NO\n"; + } + + return 0; +} diff --git a/kangrae-jo/README.md b/kangrae-jo/README.md index d9a3e9c..591a80a 100644 --- a/kangrae-jo/README.md +++ b/kangrae-jo/README.md @@ -32,4 +32,5 @@ | 28차시 | 2024.06.10 | BFS | [경주로 건설](https://school.programmers.co.kr/learn/courses/30/lessons/67259)|[#110](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/110)| | 29차시 | 2024.06.19 | TRIE | [[3차] 자동완성](https://school.programmers.co.kr/learn/courses/30/lessons/17685)|[#114](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/114)| | 30차시 | 2024.07.18 | BFS | [치즈](https://www.acmicpc.net/problem/2638)|[#117](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/117)| -| 31차시 | 2024.07.31 | Prefix Sum | [두 배열의 합](https://www.acmicpc.net/problem/2143)|[#122](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/122)| \ No newline at end of file +| 31차시 | 2024.07.31 | Prefix Sum | [두 배열의 합](https://www.acmicpc.net/problem/2143)|[#122](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/122)| +| 34차시 | 2024.08.28 | Graph | [웜홀](https://www.acmicpc.net/problem/1865)|[#122](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/136)| \ No newline at end of file