Posts 1865 웜홀
Post
Cancel

1865 웜홀

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <bits/stdc++.h>

using namespace std;

int tc, N, M, W, road[501][501];
long long d[501];
bool check[501][501];
vector<int> connect[501];

bool belmanford();

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> tc;

	while (tc--) {
		cin >> N >> M >> W;
		int a, b, c;

		for (int i = 1; i <= N; i++) {
			d[i] = 2000000000000;
			connect[i].clear();
		}
		d[1] = 0;
		memset(check, 0, sizeof(check));

		for (int i = 0; i < M; i++) {
			cin >> a >> b >> c;
			connect[a].push_back(b);
			connect[b].push_back(a);
			if (check[a][b])
				road[a][b] = min(road[a][b], c);
			else
				road[a][b] = c;
			road[b][a] = road[a][b];
			check[a][b] = true;
			check[b][a] = true;
		}
		for (int i = 0; i < W; i++) {
			cin >> a >> b >> c;
			connect[a].push_back(b);
			road[a][b] = min(road[a][b], -c);
		}
		if (belmanford())
			cout << "YES\n";
		else
			cout << "NO\n";
	}
}

bool belmanford() {
	for (int i = 0; i < N - 1; i++) {
		for (int j = 1; j <= N; j++) {
			for (int k = 0; k < connect[j].size(); k++)
				d[connect[j][k]] = min(d[connect[j][k]], d[j] + road[j][connect[j][k]]);
		}
	}

	for (int j = 1; j <= N; j++) {
		for (int k = 0; k < connect[j].size(); k++)
			if (d[connect[j][k]] > d[j] + road[j][connect[j][k]])
				return true;
	}
	return false;
}
This post is licensed under CC BY 4.0 by the author.

1806 partial sum

19235 모노미노도미노

Comments powered by Disqus.