From c500c5f0024ae6ca64368cccf7d9878338ba6aeb Mon Sep 17 00:00:00 2001 From: yuyu0830 Date: Mon, 11 Mar 2024 16:31:11 +0900 Subject: [PATCH] =?UTF-8?q?2024-03-11=20=EC=9B=9C=ED=99=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "yuyu0830/\355\203\220\354\203\211/1865.cpp" | 70 ++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 "yuyu0830/\355\203\220\354\203\211/1865.cpp" diff --git "a/yuyu0830/\355\203\220\354\203\211/1865.cpp" "b/yuyu0830/\355\203\220\354\203\211/1865.cpp" new file mode 100644 index 0000000..dbc1058 --- /dev/null +++ "b/yuyu0830/\355\203\220\354\203\211/1865.cpp" @@ -0,0 +1,70 @@ +// 최단 경로 골드 3 웜홀 https://www.acmicpc.net/problem/1865 +#include +#include +#include +#include + +#define MAX 9999999 +#define SIZE 502 +#define fastio ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); + +using namespace std; + +struct edge { + int s, e, v; +}; + +bool f() { + int n, road, wormhole; cin >> n >> road >> wormhole; + + int s, e, v; + vector edges; + + // input + while (road--) { + cin >> s >> e >> v; // start, end, value + edges.push_back({s, e, v}); + edges.push_back({e, s, v}); + } + + while (wormhole--) { + cin >> s >> e >> v; + edges.push_back({s, e, -v}); + } + + // Search all nodes n time + vector dist(n + 1, MAX); + dist[1] = 0; + + for (int node = 1; node <= n; node++) { + for (auto next : edges) { + s = next.s; + e = next.e; + v = next.v; + + if (dist[e] > dist[s] + v) + dist[e] = dist[s] + v; + } + } + + // Search one more time + for (auto next : edges) { + s = next.s; + e = next.e; + v = next.v; + + if (dist[e] > dist[s] + v) + return true; + } + + return false; +} + +int main() { + fastio; + int testCase; cin >> testCase; + while (testCase--) { + if (f()) printf("YES\n"); + else printf("NO\n"); + } +} \ No newline at end of file