From 15193a99384423c80c2247484dd9e95c9117f065 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 20 Nov 2022 21:03:41 +0800 Subject: [PATCH] =?UTF-8?q?P5960=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E5=B7=AE=E5=88=86=E7=BA=A6=E6=9D=9F=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/94859058 --- Luogu/P5960/P5960.cpp | 74 +++++++++++++++++++++++++++++++++++ Luogu/P5960/data/P5960_11.in | 3 ++ Luogu/P5960/data/P5960_11.out | 3 ++ 3 files changed, 80 insertions(+) create mode 100644 Luogu/P5960/P5960.cpp create mode 100644 Luogu/P5960/data/P5960_11.in create mode 100644 Luogu/P5960/data/P5960_11.out diff --git a/Luogu/P5960/P5960.cpp b/Luogu/P5960/P5960.cpp new file mode 100644 index 00000000..e73a2da9 --- /dev/null +++ b/Luogu/P5960/P5960.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 5005; +const int INF = 0x3f3f3f3f; + +int n, m, dist[N], cnt[N]; +std::vector> g[N]; + +bool spfa() { + std::fill_n(dist, N, INF); + + std::queue q; + + q.emplace(0); + dist[0] = 0; + + while (!q.empty()) { + int u = q.front(); + q.pop(); + + for (auto e : g[u]) { + int v = e.first, + w = e.second; + + if (dist[v] > dist[u] + w) { + dist[v] = dist[u] + w; + cnt[v] = cnt[u] + 1; + + if (cnt[v] > n) return false; + + q.emplace(v); + } + } + } + + return true; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1, u, v, w; i <= m; i++) { + cin >> u >> v >> w; + + g[v].emplace_back(u, w); + } + + for (int i = 1; i <= n; i++) { + g[0].emplace_back(i, 0); + } + + if (spfa()) { + for (int i = 1; i <= n; i++) { + cout << dist[i] << ' '; + } + + cout << endl; + } else { + cout << "NO" << endl; + } + + return 0; +} diff --git a/Luogu/P5960/data/P5960_11.in b/Luogu/P5960/data/P5960_11.in new file mode 100644 index 00000000..fd1b3c12 --- /dev/null +++ b/Luogu/P5960/data/P5960_11.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:09ad13a71544c06d3b4898d0f2ed87f009a63ec280a685ab0d8bf9a2b0de9663 +size 19 diff --git a/Luogu/P5960/data/P5960_11.out b/Luogu/P5960/data/P5960_11.out new file mode 100644 index 00000000..83172ec9 --- /dev/null +++ b/Luogu/P5960/data/P5960_11.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f9171164593756e56fb197327b529a4955590566560dbe62d586bff41be9d297 +size 3