-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1865.cpp
More file actions
64 lines (64 loc) · 1.72 KB
/
Copy path1865.cpp
File metadata and controls
64 lines (64 loc) · 1.72 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
// 벡터 초기화 안해서 많이 틀렸던 문제 초기화는 늘 언제나 생각하자.
#include <cstdio>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
typedef pair<int,int> P;
const int INF = 1000000000;
const int MAX = 501;
int tc, n, m, w, s, e, t, dist[MAX], visited[MAX];
vector<P> adj[MAX];
bool bellman(int start){
fill(dist, dist+n+1, INF);
dist[start] = 0;
visited[start] = true;
for(int i = 0; i< n;i++){
for(int j=1;j<=n;j++){
for(auto &p: adj[j]){
int next = p.first, d = p.second;
if(dist[j] != INF && dist[next] > dist[j] +d){
dist[next] = dist[j] + d;
visited[next] = true;
if(i == n-1)
return true;
}
}
}
}
return false;
}
int main(){
scanf("%d", &tc);
for(int i = 0; i < tc;i++){
if( i != 0){
printf("\n");
}
scanf("%d %d %d", &n, &m, &w);
for(int j =1; j<=n;j++)
adj[j].clear();
for(int j= 0; j<m;j++){
int s,e,t;
scanf("%d %d %d", &s,&e,&t);
adj[s].push_back(P(e,t));
adj[e].push_back(P(s,t));
}
for(int j = 0; j<w; j++){
scanf("%d %d %d", &s,&e,&t);
adj[s].push_back(P(e,-t));
}
bool minusCycle= false;
fill(visited, visited+n+1, false);
for(int j=1;j<=n;j++){
if(visited[j] == false){
minusCycle = bellman(j);
}
if(minusCycle){
break;
}
}
if(minusCycle) printf("YES");
else
printf("NO");
}
}