From 55a83b71b51445f29cb07581f545f96d048a998d Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Wed, 11 Aug 2021 21:20:06 +0800 Subject: [PATCH] =?UTF-8?q?852.=20spfa=E5=88=A4=E6=96=AD=E8=B4=9F=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/7062672/ --- AcWing/852/852.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 AcWing/852/852.cpp diff --git a/AcWing/852/852.cpp b/AcWing/852/852.cpp new file mode 100644 index 00000000..03f2120c --- /dev/null +++ b/AcWing/852/852.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int n, m, u, v, w, dist[100005], cnt[100005]; +vector> g[100005]; + +bool spfa() { + memset(dist, 0x3f, sizeof(dist)); + queue q; + for (int i = 1; i <= n; i++) { + q.push(i); + } + while (!q.empty()) { + int t = q.front(); + q.pop(); + 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; + q.push(i.first); + } + } + } + return false; +} + +int main() { + cin >> n >> m; + for (int i = 0; i < m; i++) { + cin >> u >> v >> w; + g[u].push_back(make_pair(v, w)); + } + cout << (spfa() ? "Yes" : "No") << endl; + return 0; +}