0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 21:28:48 +00:00

#1845. [COCI2008-2009#3] NAJKRACI

https://sjzezoj.com/submission/69740
This commit is contained in:
Baoshuo Ren 2023-02-14 14:31:16 +08:00
parent 85478fb9d9
commit b2b98018a0
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
28 changed files with 238 additions and 0 deletions

157
S2OJ/1845/1845.cpp Normal file
View File

@ -0,0 +1,157 @@
#include <iostream>
#include <algorithm>
#include <iterator>
#include <queue>
#include <tuple>
#include <utility>
#include <vector>
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<std::tuple<int, int, int>> g[N];
std::tuple<int, int, int, bool> 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<int, int>,
std::vector<std::pair<int, int>>,
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<int> q;
std::vector<int> 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<long long>(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<long long>(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<long long>(ans[j])
+ static_cast<long long>(cnt_in[u]) * cnt_out[v] % mod)
% mod;
}
}
}
std::copy_n(std::begin(ans) + 1, m, std::ostream_iterator<int>(cout, "\n"));
return 0;
}

BIN
S2OJ/1845/data/ex_najkraci1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/ex_najkraci1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/ex_najkraci2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/ex_najkraci2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/ex_najkraci3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/ex_najkraci3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci6.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci8.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/najkraci9.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1845/data/problem.conf (Stored with Git LFS) Normal file

Binary file not shown.