This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<
// a doua zi dupa 11 ore de somn merge mai bine garantez eu
// ah.. sau poate ca nu.. sau?
// omfg trebe sa invat sa codez ca nu i ok asa sincer
using namespace std;
const int INF = (int)1e9 + 5;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int k, n, m, q; cin >> k >> n >> m >> q;
int groups = (n + k - 1) / k;
int log_groups = 32 - __builtin_clz(groups);
vector<vector<vector<int>>> dist(log_groups,
vector<vector<int>>(n,
vector<int>(k, INF)));
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < m; ++i) {
int a, b, c; cin >> a >> b >> c;
adj[a].emplace_back(b, c);
adj[b].emplace_back(a, c);
dist[0][a][b % k] = c;
}
for (int p = 1; p < log_groups; ++p) {
for (int from = 0; from / k + (1 << p) <= (n - 1) / k; ++from) {
for (int to = 0; to < k; ++to) {
int gr = from / k + (1 << (p - 1));
int lst = min((gr + 1) * k, n);
for (int x = gr * k, r = 0; x < lst; ++x, ++r) {
dist[p][from][to] = min(dist[p][from][to],
dist[p - 1][from][r] + dist[p - 1][x][to]);
}
}
}
}
while (q--) {
int a, b; cin >> a >> b;
vector<int> d(k, INF);
d[a % k] = 0;
a = a / k;
int len = b / k - a;
for (int bit = 0; (1 << bit) <= len; ++bit) {
if (((1 << bit) & len) == 0) continue;
vector<int> nd(k, INF);
for (int from = a * k; from < (a + 1) * k; ++from) {
for (int x = 0; x < k; ++x) {
nd[x] = min(nd[x], d[from - a * k] + dist[bit][from][x]);
}
}
a += 1 << bit;
d = move(nd);
}
int ans = d[b % k];
if (ans == INF) ans = -1;
cout << ans << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |