From 31121c03c178f102326a992d6642cf21935fd82a Mon Sep 17 00:00:00 2001 From: Jinyeong Seol Date: Tue, 1 Jul 2025 23:58:28 +0900 Subject: [PATCH] =?UTF-8?q?[20250701]=20BOJ=20/=20G3=20/=20=EC=9B=9C?= =?UTF-8?q?=ED=99=80=20/=20=EC=84=A4=EC=A7=84=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1 BOJ G3 \354\233\234\355\231\200.md" | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 "Seol-JY/202507/1 BOJ G3 \354\233\234\355\231\200.md" diff --git "a/Seol-JY/202507/1 BOJ G3 \354\233\234\355\231\200.md" "b/Seol-JY/202507/1 BOJ G3 \354\233\234\355\231\200.md" new file mode 100644 index 00000000..7277f198 --- /dev/null +++ "b/Seol-JY/202507/1 BOJ G3 \354\233\234\355\231\200.md" @@ -0,0 +1,102 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int TC = nextInt(new StringTokenizer(br.readLine())); + + while (TC-- > 0) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = nextInt(st); + int M = nextInt(st); + int W = nextInt(st); + + List[] graph = new ArrayList[N + 1]; + for (int i = 1; i <= N; i++) { + graph[i] = new ArrayList<>(); + } + + for (int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + int s = nextInt(st); + int e = nextInt(st); + int t = nextInt(st); + + graph[s].add(new Edge(e, t)); + graph[e].add(new Edge(s, t)); + } + + for (int i = 0; i < W; i++) { + st = new StringTokenizer(br.readLine()); + int s = nextInt(st); + int e = nextInt(st); + int t = nextInt(st); + + graph[s].add(new Edge(e, -t)); + } + + if (hasNegativeCycle(graph, N)) { + System.out.println("YES"); + } else { + System.out.println("NO"); + } + } + } + + static boolean hasNegativeCycle(List[] graph, int N) { + long[] dist = new long[N + 1]; + final long INF = 987654321 + Arrays.fill(dist, INF); + + for (int start = 1; start <= N; start++) { + if (dist[start] != INF) continue; + + dist[start] = 0; + + boolean updated = false; + for (int i = 0; i < N - 1; i++) { + updated = false; + for (int j = 1; j <= N; j++) { + if (dist[j] == INF) continue; + + for (Edge edge : graph[j]) { + if (dist[j] + edge.cost < dist[edge.to]) { + dist[edge.to] = dist[j] + edge.cost; + updated = true; + } + } + } + if (!updated) break; + } + + for (int j = 1; j <= N; j++) { + if (dist[j] == INF) continue; + + for (Edge edge : graph[j]) { + if (dist[j] + edge.cost < dist[edge.to]) { + return true; + } + } + } + } + + return false; + } + + static class Edge { + int to, cost; + + Edge(int to, int cost) { + this.to = to; + this.cost = cost; + } + } + + static int nextInt(StringTokenizer st) { + return Integer.parseInt(st.nextToken()); + } +} +```