문제 URL : https://www.acmicpc.net/problem/1865
문제 접근법 :
벨만포드의 기초적인 문제입니다. 다익스트라의 모든경우의수를 구하면서
싸이클이 생성되는지 확인하는문제입니다.
소스코드 :
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
|
//By 콩순이냉장고
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> p;
vector<vector<p>> v;
int n, m, w;
void input() {
cin >> n >> m >> w;
v = vector<vector<p>>(n + 1);
int a, b, c;
for (int i = 0; i < m; i++) {
cin >> a >> b >> c;
v[a].push_back({ b,c });
v[b].push_back({ a,c });
}
for (int i = 0; i < w; i++) {
cin >> a >> b >> c;
v[a].push_back({ b,-c });
}
}
void solve() {
vector<int> dist(n + 1, 1e9);
dist[1] = 0;
bool flag = false;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 0; k < v[j].size(); k++) {
int a = j;
int b = v[j][k].first;
int c = v[j][k].second;
if (dist[a] + c < dist[b]) {
dist[b] = dist[a] + c;
if (i == n)flag = true;
}
}
}
}
cout << (flag ? "YES" : "NO") << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("input.txt", "r", stdin);
int test;
cin >> test;
while (test--) {
input();
solve();
}
}
|
cs |
모르는점 혹은 궁금한점 댓글은 언제나 환영입니다.
'백준' 카테고리의 다른 글
백준 1948 임계경로 (0) | 2021.08.04 |
---|---|
백준 3111 검열 (0) | 2021.08.04 |
백준 1520 내리막 길 (0) | 2021.07.26 |
백준 2671 잠수함식별 (0) | 2021.07.26 |
백준 1013 Contact (0) | 2021.07.23 |