-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1865.cpp
89 lines (81 loc) · 1.65 KB
/
1865.cpp
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
85
86
87
88
89
#include <string>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <climits>
#include <set>
#define p(a, b) make_pair(a, b)
#define SWAP(a, b, type) do { \
type temp; \
temp = a; \
a = b; \
b = temp; \
} while (0)
#define INF 1000000000
#define endl '\n'
#define ll long long
using namespace std;
int cost[501][501];
int ans[501][501];
int N,M,W;
void input() {
int S,E,T;
cin>>N>>M>>W;
//도로 정보
for(int i=0;i<M;i++){
cin>>S>>E>>T;
if(cost[S][E] > T) cost[S][E] = cost[E][S] = T;
}
//웜홀 정보
for(int i=0; i<W;i++){
cin>>S>>E>>T;
cost[S][E] = -T;
}
}
void solution() {
// 플로이드 와샬, 복제
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
ans[i][j] = cost[i][j];
}
}
// k = 거쳐가는 노드
for(int k=1;k<=N;k++){
// i = 출발 노드
for(int i=1;i<=N;i++){
// j = 도착 노드
for(int j=1;j<=N;j++){
if(ans[i][k]+ans[k][j]<ans[i][j]) ans[i][j] = ans[i][k] + ans[k][j];
}
}
}
for(int i=1;i<=N;i++){
if(ans[i][i]<0){
cout<<"YES"<<'\n';
return;
}
}
cout<<"NO"<<'\n';
}
void init(){
fill(&cost[0][0],&cost[500][501],INF);
fill(&ans[0][0],&ans[500][501],INF);
for(int i=1;i<=500;i++) cost[i][i] = 0;
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T;
cin>>T;
while(T--){
init();
input();
solution();
}
return 0;
}