-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1865.java
More file actions
84 lines (78 loc) · 2.67 KB
/
Copy path1865.java
File metadata and controls
84 lines (78 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// https://www.acmicpc.net/problem/1865
// 웜홀
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
private static final int INF = Integer.MAX_VALUE;
private static int n, m, w;
private static List<Edge> edges = new ArrayList<>();
private static List<Integer> list = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int testsize = Integer.parseInt(reader.readLine());
for(int T=0; T<testsize; T++){
StringTokenizer st = new StringTokenizer(reader.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
w = Integer.parseInt(st.nextToken());
init();
int s, e, t, answer = 0;
for(int i=0; i<m; i++){
st = new StringTokenizer(reader.readLine());
s = Integer.parseInt(st.nextToken()) - 1;
e = Integer.parseInt(st.nextToken()) - 1;
t = Integer.parseInt(st.nextToken());
edges.add(new Edge(s, e, t));
edges.add(new Edge(e, s, t));
}
for(int i=0; i<w; i++){
st = new StringTokenizer(reader.readLine());
s = Integer.parseInt(st.nextToken()) - 1;
e = Integer.parseInt(st.nextToken()) - 1;
t = Integer.parseInt(st.nextToken()) * -1;
edges.add(new Edge(s, e, t));
list.add(s);
}
for(int start : list){
if(bellmanFord(start)){
answer = 1;
break;
}
}
if(answer == 1)
sb.append("YES");
else sb.append("NO");
sb.append('\n');
}
System.out.println(sb);
}
private static int[] distance;
private static boolean bellmanFord(int start){
Arrays.fill(distance, INF);
distance[start] = 0;
for(int i=0; i<n; i++){
for(Edge edge : edges){
if(distance[edge.s] != INF && distance[edge.e] > distance[edge.s] + edge.t){
distance[edge.e] = distance[edge.s] + edge.t;
if(i == n-1) return true;
}
}
}
return false;
}
private static void init(){
edges.clear();
list.clear();
distance = new int[n];
}
}
class Edge{
int s, e, t;
public Edge(int s, int e, int t){
this.s = s;
this.e = e;
this.t = t;
}
}