From 97ee725ba5a19fc6e99678022e6614baee65f086 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Fri, 23 Jul 2021 00:35:48 +0800 Subject: [PATCH] =?UTF-8?q?P3385=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E8=B4=9F=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R53951626 --- Luogu/problem/P3385/P3385.cpp | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Luogu/problem/P3385/P3385.cpp diff --git a/Luogu/problem/P3385/P3385.cpp b/Luogu/problem/P3385/P3385.cpp new file mode 100644 index 00000000..617b23e8 --- /dev/null +++ b/Luogu/problem/P3385/P3385.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +int t, n, m, u, v, w, cnt, dist[2005]; +bool flag; + +struct node { + int u, v, w; + + node() { + u = v = w = 0; + } + node(int _u, int _v, int _w) { + u = _u; + v = _v; + w = _w; + } +} g[6005]; + +int main() { + cin >> t; + while (t--) { + cnt = 0; + flag = false; + memset(g, 0x00, sizeof(g)); + memset(dist, 0x3f, sizeof(dist)); + cin >> n >> m; + for (int i = 1; i <= m; i++) { + cin >> u >> v >> w; + g[++cnt] = node(u, v, w); + if (w >= 0) g[++cnt] = node(v, u, w); + } + dist[1] = 0; + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= cnt; j++) { + if (dist[g[j].u] == 0x3f3f3f3f) continue; + dist[g[j].v] = min(dist[g[j].v], dist[g[j].u] + g[j].w); + } + } + for (int i = 1; i <= cnt; i++) { + if (dist[g[i].u] == 0x3f3f3f3f || dist[g[i].v] == 0x3f3f3f3f) continue; + if (dist[g[i].v] > dist[g[i].u] + g[i].w) { + flag = true; + } + } + cout << (flag ? "YES" : "NO") << endl; + } + return 0; +}