Skip to content

Commit aff2334

Browse files
authored
Merge pull request #187 from AlgorithmWithGod/suyeun84
[20250226] BOJ / 골드3 / 웜홀 / 김수연
2 parents f86d460 + b0f39c7 commit aff2334

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class boj1865 {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
10+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
11+
static int nextInt() {return Integer.parseInt(st.nextToken());}
12+
static void bwEnd() throws Exception {bw.flush(); bw.close();}
13+
static ArrayList<Road> road;
14+
static int N;
15+
16+
public static void main(String[] args) throws Exception {
17+
nextLine();
18+
int TC = nextInt();
19+
for (int tc = 0; tc < TC; tc++) {
20+
nextLine();
21+
N = nextInt(); //지점의 수
22+
int M = nextInt(); //도로의 갯수
23+
int W = nextInt(); //웜홀의 갯수
24+
road = new ArrayList<>(N+1);
25+
26+
int S, E, T;
27+
for (int i = 0 ; i < M+W; i++) {
28+
nextLine();
29+
S = nextInt();
30+
E = nextInt();
31+
T = nextInt();
32+
if (i < M) {
33+
road.add(new Road(S, E, T));
34+
road.add(new Road(E, S, T));
35+
} else {
36+
road.add(new Road(S, E, -1*T));
37+
}
38+
}
39+
40+
if (bfs()) bw.write("YES"+"\n");
41+
else bw.write("NO"+"\n");
42+
}
43+
bwEnd();
44+
}
45+
46+
// 음수 사이클 존재하면 true 반환
47+
static boolean bfs() {
48+
int[] time = new int[N+1];
49+
Arrays.fill(time, 10001);
50+
time[1] = 0;
51+
52+
for (int i = 0; i < N; i++) { //N-1번 반복 + N번째 처리
53+
for (int j = 0; j < road.size(); j++) { //간선 확인
54+
Road r = road.get(j);
55+
if (i == N-1) {
56+
if (time[r.e] > time[r.s]+r.t) return true;
57+
continue;
58+
}
59+
time[r.e] = Math.min(time[r.e], time[r.s]+r.t);
60+
}
61+
}
62+
return false;
63+
}
64+
65+
static class Road {
66+
int s;
67+
int e;
68+
int t;
69+
public Road(int s, int e, int t) {
70+
this.s = s;
71+
this.e = e;
72+
this.t = t;
73+
}
74+
}
75+
}
76+
77+
```

0 commit comments

Comments
 (0)