-
Notifications
You must be signed in to change notification settings - Fork 2
/
1865_josueyeon.cpp
84 lines (67 loc) · 1.8 KB
/
1865_josueyeon.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
#include <iostream>
#include <vector>
using namespace std;
// 1865 웜홀
long dist[501];
void bellmanFord(vector<pair<int, int>> graph[], int N, int start, bool &cycle)
{
cycle = false;
dist[start] = 0;
for(int count = 1;count <= N;count++)
{
for(int i = 1;i <= N;i++)
{
for(int j = 0;j < graph[i].size();j++)
{
int next = graph[i][j].first;
int w = graph[i][j].second;
if (dist[next] > w + dist[i])
{
// N번째에서도 업데이트가 발생하면 음의 순환 발생
if (count == N)
{
cycle = true;
return;
}
dist[next] = w + dist[i];
}
}
}
}
}
int main()
{
cin.tie(0);
cout.tie(0);
//ios::sync_with_stdio(false);
int T;
cin>>T;
while (T-- > 0)
{
// N(node), M(edge), W(웜홀)
int N, M, W;
cin>>N>>M>>W;
vector<pair<int, int>> graph[501];
fill_n(dist, N + 1, 1e9);
for(int i = 0;i < M;i++)
{
int a, b, c;
cin>>a>>b>>c;
graph[a].push_back({b, c});
graph[b].push_back({a, c});
}
for(int i = 0;i < W;i++)
{
int a, b, c;
cin>>a>>b>>c;
graph[a].push_back({b, c * -1});
}
bool cycle = false;
bellmanFord(graph, N, 1, cycle);
if (cycle == false)
cout<<"NO\n";
else
cout<<"YES\n";
}
return 0;
}