From 4370ffb4097868c57b85af3b520ab34defe5db6d Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Fri, 24 Sep 2021 21:47:28 +0800 Subject: [PATCH] =?UTF-8?q?904.=20=E8=99=AB=E6=B4=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/7892844/ --- AcWing/904/904.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 AcWing/904/904.cpp diff --git a/AcWing/904/904.cpp b/AcWing/904/904.cpp new file mode 100644 index 00000000..1529e678 --- /dev/null +++ b/AcWing/904/904.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +int f, n, m, w, u, v, t, dist[505], cnt[505]; +vector> g[505]; +bool vis[505]; + +bool spfa() { + memset(dist, 0x3f, sizeof(dist)); + memset(vis, 0x00, sizeof(vis)); + memset(cnt, 0x00, sizeof(cnt)); + queue 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; +}