0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00
Baoshuo Ren 2021-09-24 21:47:28 +08:00 committed by Baoshuo Ren
parent 7482822f4c
commit 4370ffb409
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

56
AcWing/904/904.cpp Normal file
View File

@ -0,0 +1,56 @@
#include <bits/stdc++.h>
using namespace std;
int f, n, m, w, u, v, t, dist[505], cnt[505];
vector<pair<int, int>> g[505];
bool vis[505];
bool spfa() {
memset(dist, 0x3f, sizeof(dist));
memset(vis, 0x00, sizeof(vis));
memset(cnt, 0x00, sizeof(cnt));
queue<int> q;
for (int i = 1; i <= n; i++) {
q.push(i);
vis[i] = true;
}
while (!q.empty()) {
int t = q.front();
q.pop();
vis[t] = false;
for (auto i : g[t]) {
if (dist[i.first] > dist[t] + i.second) {
dist[i.first] = dist[t] + i.second;
cnt[i.first] = cnt[t] + 1;
if (cnt[i.first] >= n) return true;
if (!vis[i.first]) {
q.push(i.first);
vis[i.first] = true;
}
}
}
}
return false;
}
int main() {
cin >> f;
while (f--) {
cin >> n >> m >> w;
for (int i = 1; i <= n; i++) {
g[i].clear();
}
for (int i = 1; i <= m; i++) {
cin >> u >> v >> t;
g[u].push_back(make_pair(v, t));
g[v].push_back(make_pair(u, t));
}
for (int i = 1; i <= w; i++) {
cin >> u >> v >> t;
g[u].push_back(make_pair(v, -t));
}
cout << (spfa() ? "YES" : "NO") << endl;
}
return 0;
}