From b2b98018a0b7af350e7c0f71b75d59a5ba08b32f Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 14 Feb 2023 14:31:16 +0800 Subject: [PATCH] #1845. [COCI2008-2009#3] NAJKRACI https://sjzezoj.com/submission/69740 --- S2OJ/1845/1845.cpp | 157 ++++++++++++++++++++++++++++++++ S2OJ/1845/data/ex_najkraci1.in | 3 + S2OJ/1845/data/ex_najkraci1.out | 3 + S2OJ/1845/data/ex_najkraci2.in | 3 + S2OJ/1845/data/ex_najkraci2.out | 3 + S2OJ/1845/data/ex_najkraci3.in | 3 + S2OJ/1845/data/ex_najkraci3.out | 3 + S2OJ/1845/data/najkraci1.in | 3 + S2OJ/1845/data/najkraci1.out | 3 + S2OJ/1845/data/najkraci10.in | 3 + S2OJ/1845/data/najkraci10.out | 3 + S2OJ/1845/data/najkraci2.in | 3 + S2OJ/1845/data/najkraci2.out | 3 + S2OJ/1845/data/najkraci3.in | 3 + S2OJ/1845/data/najkraci3.out | 3 + S2OJ/1845/data/najkraci4.in | 3 + S2OJ/1845/data/najkraci4.out | 3 + S2OJ/1845/data/najkraci5.in | 3 + S2OJ/1845/data/najkraci5.out | 3 + S2OJ/1845/data/najkraci6.in | 3 + S2OJ/1845/data/najkraci6.out | 3 + S2OJ/1845/data/najkraci7.in | 3 + S2OJ/1845/data/najkraci7.out | 3 + S2OJ/1845/data/najkraci8.in | 3 + S2OJ/1845/data/najkraci8.out | 3 + S2OJ/1845/data/najkraci9.in | 3 + S2OJ/1845/data/najkraci9.out | 3 + S2OJ/1845/data/problem.conf | 3 + 28 files changed, 238 insertions(+) create mode 100644 S2OJ/1845/1845.cpp create mode 100644 S2OJ/1845/data/ex_najkraci1.in create mode 100644 S2OJ/1845/data/ex_najkraci1.out create mode 100644 S2OJ/1845/data/ex_najkraci2.in create mode 100644 S2OJ/1845/data/ex_najkraci2.out create mode 100644 S2OJ/1845/data/ex_najkraci3.in create mode 100644 S2OJ/1845/data/ex_najkraci3.out create mode 100644 S2OJ/1845/data/najkraci1.in create mode 100644 S2OJ/1845/data/najkraci1.out create mode 100644 S2OJ/1845/data/najkraci10.in create mode 100644 S2OJ/1845/data/najkraci10.out create mode 100644 S2OJ/1845/data/najkraci2.in create mode 100644 S2OJ/1845/data/najkraci2.out create mode 100644 S2OJ/1845/data/najkraci3.in create mode 100644 S2OJ/1845/data/najkraci3.out create mode 100644 S2OJ/1845/data/najkraci4.in create mode 100644 S2OJ/1845/data/najkraci4.out create mode 100644 S2OJ/1845/data/najkraci5.in create mode 100644 S2OJ/1845/data/najkraci5.out create mode 100644 S2OJ/1845/data/najkraci6.in create mode 100644 S2OJ/1845/data/najkraci6.out create mode 100644 S2OJ/1845/data/najkraci7.in create mode 100644 S2OJ/1845/data/najkraci7.out create mode 100644 S2OJ/1845/data/najkraci8.in create mode 100644 S2OJ/1845/data/najkraci8.out create mode 100644 S2OJ/1845/data/najkraci9.in create mode 100644 S2OJ/1845/data/najkraci9.out create mode 100644 S2OJ/1845/data/problem.conf diff --git a/S2OJ/1845/1845.cpp b/S2OJ/1845/1845.cpp new file mode 100644 index 00000000..7e48b73a --- /dev/null +++ b/S2OJ/1845/1845.cpp @@ -0,0 +1,157 @@ +#include +#include +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1505, + M = 5005; +const int INF = 0x3f3f3f3f; +const int mod = 1e9 + 7; + +int n, m, dist[N], deg[N], cnt_in[N], cnt_out[N], ans[M]; +std::vector> g[N]; +std::tuple edges[M]; +bool vis[N]; + +void dijkstra(int s) { + std::fill(std::begin(dist), std::end(dist), INF); + std::fill(std::begin(vis), std::end(vis), false); + + std::priority_queue< + std::pair, + std::vector>, + std::greater<>> + q; + + q.emplace(0, s); + dist[s] = 0; + + while (!q.empty()) { + int u = q.top().second; + q.pop(); + + if (vis[u]) continue; + + for (auto e : g[u]) { + int v, w; + + std::tie(v, w, std::ignore) = e; + + if (dist[v] > dist[u] + w) { + dist[v] = dist[u] + w; + + q.emplace(dist[v], v); + } + } + + vis[u] = true; + } +} + +void topo(int s) { + std::fill(std::begin(deg), std::end(deg), 0); + std::fill(std::begin(cnt_in), std::end(cnt_in), 0); + std::fill(std::begin(cnt_out), std::end(cnt_out), 0); + + for (int i = 1; i <= m; i++) { + if (std::get<3>(edges[i])) { + deg[std::get<1>(edges[i])]++; + } + } + + std::queue q; + std::vector v; + + cnt_in[s] = 1; + q.emplace(s); + + while (!q.empty()) { + int u = q.front(); + + v.emplace_back(u); + q.pop(); + + for (auto e : g[u]) { + int v, w, id; + + std::tie(v, w, id) = e; + + if (std::get<3>(edges[id])) { + cnt_in[v] = (static_cast(cnt_in[v]) + cnt_in[u]) % mod; + + if (--deg[v] == 0) { + q.emplace(v); + } + } + } + } + + std::for_each(v.rbegin(), v.rend(), [&](int u) { + for (auto e : g[u]) { + int v, w, id; + + std::tie(v, w, id) = e; + + if (std::get<3>(edges[id])) { + cnt_out[u] = (static_cast(cnt_out[u]) + cnt_out[v]) % mod; + } + } + + cnt_out[u] = (cnt_out[u] + 1) % mod; + }); +} + +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[u].emplace_back(v, w, i); + edges[i] = {u, v, w, false}; + } + + for (int i = 1; i <= n; i++) { + dijkstra(i); + + std::for_each( + std::begin(edges) + 1, + std::begin(edges) + m + 1, + [&](auto &e) { + int u = std::get<0>(e), + v = std::get<1>(e), + w = std::get<2>(e); + + std::get<3>(e) = dist[u] + w == dist[v]; + }); + + topo(i); + + for (int j = 1; j <= m; j++) { + int u, v; + bool is_short; + + std::tie(u, v, std::ignore, is_short) = edges[j]; + + if (is_short) { + ans[j] = (static_cast(ans[j]) + + static_cast(cnt_in[u]) * cnt_out[v] % mod) + % mod; + } + } + } + + std::copy_n(std::begin(ans) + 1, m, std::ostream_iterator(cout, "\n")); + + return 0; +} diff --git a/S2OJ/1845/data/ex_najkraci1.in b/S2OJ/1845/data/ex_najkraci1.in new file mode 100644 index 00000000..f7251063 --- /dev/null +++ b/S2OJ/1845/data/ex_najkraci1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:aac89c630d1fda022cbf92edc1f2fafa675b487891c55c460e69c9a27e2bf94f +size 22 diff --git a/S2OJ/1845/data/ex_najkraci1.out b/S2OJ/1845/data/ex_najkraci1.out new file mode 100644 index 00000000..31b4824a --- /dev/null +++ b/S2OJ/1845/data/ex_najkraci1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:35b19f220a9fc265419448b7a6547c1062cc11517c02f95b27a5d582ef413e7d +size 6 diff --git a/S2OJ/1845/data/ex_najkraci2.in b/S2OJ/1845/data/ex_najkraci2.in new file mode 100644 index 00000000..78ea521c --- /dev/null +++ b/S2OJ/1845/data/ex_najkraci2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:34e78955dd1d10985bb1bdd75d425ca0aa4e20dca46678cf5a8bffe47e5a2e9e +size 28 diff --git a/S2OJ/1845/data/ex_najkraci2.out b/S2OJ/1845/data/ex_najkraci2.out new file mode 100644 index 00000000..7bc6be92 --- /dev/null +++ b/S2OJ/1845/data/ex_najkraci2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:273564042a59cb8635c48fb4e44e057922ec15a2c717db6a7c27cbf60b791be4 +size 8 diff --git a/S2OJ/1845/data/ex_najkraci3.in b/S2OJ/1845/data/ex_najkraci3.in new file mode 100644 index 00000000..72df3081 --- /dev/null +++ b/S2OJ/1845/data/ex_najkraci3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9d0f833940ae21e5950e7838fb843b436e56ca8b3eb400b3209c611df0b36ea3 +size 54 diff --git a/S2OJ/1845/data/ex_najkraci3.out b/S2OJ/1845/data/ex_najkraci3.out new file mode 100644 index 00000000..cd72312a --- /dev/null +++ b/S2OJ/1845/data/ex_najkraci3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5b78b0fe63b38ec86f45dc58b0172b0de51519ae8ffc797764df9c83352b9f62 +size 16 diff --git a/S2OJ/1845/data/najkraci1.in b/S2OJ/1845/data/najkraci1.in new file mode 100644 index 00000000..4ddeed99 --- /dev/null +++ b/S2OJ/1845/data/najkraci1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6da43dedc95ce9176110d485139d9f3cacc2d89e73c05cdee6b0464b2b542855 +size 208 diff --git a/S2OJ/1845/data/najkraci1.out b/S2OJ/1845/data/najkraci1.out new file mode 100644 index 00000000..53728060 --- /dev/null +++ b/S2OJ/1845/data/najkraci1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6a89410387250599b4b2eff80ca15f36426ed0b6c71674f1bd98dfd31ba3772b +size 58 diff --git a/S2OJ/1845/data/najkraci10.in b/S2OJ/1845/data/najkraci10.in new file mode 100644 index 00000000..a8ab7652 --- /dev/null +++ b/S2OJ/1845/data/najkraci10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cf5378a0eddeb60f2b7546f593e0c4d55c76d603879c5460b8d95e3c8ad0bed7 +size 57804 diff --git a/S2OJ/1845/data/najkraci10.out b/S2OJ/1845/data/najkraci10.out new file mode 100644 index 00000000..5ed69424 --- /dev/null +++ b/S2OJ/1845/data/najkraci10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:252cc26200a90d80864560dfe9d822320ecd042813e8ddbf73e6c38259d9c846 +size 26215 diff --git a/S2OJ/1845/data/najkraci2.in b/S2OJ/1845/data/najkraci2.in new file mode 100644 index 00000000..a6837c61 --- /dev/null +++ b/S2OJ/1845/data/najkraci2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e5642962739e6198137cf3766a7a6fd85664138d0df1a527ffbc1357b04ebefb +size 174 diff --git a/S2OJ/1845/data/najkraci2.out b/S2OJ/1845/data/najkraci2.out new file mode 100644 index 00000000..42724257 --- /dev/null +++ b/S2OJ/1845/data/najkraci2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5744f37d408ab6f94f0e0749a3ccf5b335e154aeeb464aa9ed6dcedbee1c9a23 +size 70 diff --git a/S2OJ/1845/data/najkraci3.in b/S2OJ/1845/data/najkraci3.in new file mode 100644 index 00000000..8a7a40d0 --- /dev/null +++ b/S2OJ/1845/data/najkraci3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fcb4a8eb319d447161d88a81bb9f6cda97a2709cd3457c5aad9fa1c6ecc15521 +size 205 diff --git a/S2OJ/1845/data/najkraci3.out b/S2OJ/1845/data/najkraci3.out new file mode 100644 index 00000000..0031cef5 --- /dev/null +++ b/S2OJ/1845/data/najkraci3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:eb547d63ff36a3fe785422bf5a4af48a9486de25c743536f8e39db0df20e5b58 +size 76 diff --git a/S2OJ/1845/data/najkraci4.in b/S2OJ/1845/data/najkraci4.in new file mode 100644 index 00000000..74185551 --- /dev/null +++ b/S2OJ/1845/data/najkraci4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f392a0fe6856b395c244be052db20dafccb05be77bb545fe61ed1efb5b22edf4 +size 3519 diff --git a/S2OJ/1845/data/najkraci4.out b/S2OJ/1845/data/najkraci4.out new file mode 100644 index 00000000..b4ed4b23 --- /dev/null +++ b/S2OJ/1845/data/najkraci4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c93730cd8a5a1d43e67df70849d66d30abce9eb9554877e25927c9d96158ca18 +size 959 diff --git a/S2OJ/1845/data/najkraci5.in b/S2OJ/1845/data/najkraci5.in new file mode 100644 index 00000000..2f38d607 --- /dev/null +++ b/S2OJ/1845/data/najkraci5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c5a0665858dbf55ba8432d0101eff3ef26eac755448b4c1a01b82c7065b9f4b6 +size 9678 diff --git a/S2OJ/1845/data/najkraci5.out b/S2OJ/1845/data/najkraci5.out new file mode 100644 index 00000000..1590e716 --- /dev/null +++ b/S2OJ/1845/data/najkraci5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:47e25e0e1e2c6ec0c2ca2428c4f96285aa88da3e6588fb0b98abef72dc981d61 +size 3271 diff --git a/S2OJ/1845/data/najkraci6.in b/S2OJ/1845/data/najkraci6.in new file mode 100644 index 00000000..e0f681b7 --- /dev/null +++ b/S2OJ/1845/data/najkraci6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e644738d375e2615b3269704c507255be8f7366fff3df2104f50dc87b10d8d28 +size 9642 diff --git a/S2OJ/1845/data/najkraci6.out b/S2OJ/1845/data/najkraci6.out new file mode 100644 index 00000000..1264a267 --- /dev/null +++ b/S2OJ/1845/data/najkraci6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ae94715661eac503e2e230a7861ab011445b9c7aaf0431c19c335ea3da70b043 +size 8996 diff --git a/S2OJ/1845/data/najkraci7.in b/S2OJ/1845/data/najkraci7.in new file mode 100644 index 00000000..771b5c50 --- /dev/null +++ b/S2OJ/1845/data/najkraci7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:03d5f79506feb66464b8fd6472f7e9d7772ab9e3e84ec929be5a877088835ab4 +size 39616 diff --git a/S2OJ/1845/data/najkraci7.out b/S2OJ/1845/data/najkraci7.out new file mode 100644 index 00000000..9f351b78 --- /dev/null +++ b/S2OJ/1845/data/najkraci7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3bae83cd524b4e585cf76adec13ab12fcdd1cc3e7e873ac201c9c6052a35b5c0 +size 12565 diff --git a/S2OJ/1845/data/najkraci8.in b/S2OJ/1845/data/najkraci8.in new file mode 100644 index 00000000..68710b56 --- /dev/null +++ b/S2OJ/1845/data/najkraci8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:57041d4b698012723473729b9649188ac967c2a4d3997feb53b451a8730a3c16 +size 56197 diff --git a/S2OJ/1845/data/najkraci8.out b/S2OJ/1845/data/najkraci8.out new file mode 100644 index 00000000..a0a11a3b --- /dev/null +++ b/S2OJ/1845/data/najkraci8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:593c616a7eedc9eb6eda0abe81b1c1871cf4493ba5c14151cdfd1bfd6338d207 +size 28449 diff --git a/S2OJ/1845/data/najkraci9.in b/S2OJ/1845/data/najkraci9.in new file mode 100644 index 00000000..4566eda2 --- /dev/null +++ b/S2OJ/1845/data/najkraci9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bba5212ff6c6d56a11348e3228e58592de10693044d84b52e1427bc7d2c47387 +size 49643 diff --git a/S2OJ/1845/data/najkraci9.out b/S2OJ/1845/data/najkraci9.out new file mode 100644 index 00000000..0297bd88 --- /dev/null +++ b/S2OJ/1845/data/najkraci9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:11ad7c689d0a8b224ac90faaa0ab4d875f73f7402d4aaf3b6d75d251088eaf83 +size 19156 diff --git a/S2OJ/1845/data/problem.conf b/S2OJ/1845/data/problem.conf new file mode 100644 index 00000000..64c434d9 --- /dev/null +++ b/S2OJ/1845/data/problem.conf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:64d7c9fc4572670133036dea232fe0c0f15aa688e4d66d8eb00d6a5765708b36 +size 184