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 c83b4c0..5ee4e7f 100644 --- a/kangrae-jo/README.md +++ b/kangrae-jo/README.md @@ -35,3 +35,4 @@ | 31차시 | 2024.07.31 | Prefix Sum | [두 배열의 합](https://www.acmicpc.net/problem/2143)|[#122](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/122)| | 32차시 | 2024.08.10 | BFS | [퍼즐 조각 채우기](https://school.programmers.co.kr/learn/courses/30/lessons/84021)|[#130](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/130)| | 33차시 | 2024.08.18 | UnionFind | [카드 게임](https://www.acmicpc.net/problem/16566)|[#133](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/133)| +| 34차시 | 2024.08.28 | Graph | [웜홀](https://www.acmicpc.net/problem/1865)|[#122](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/136)|