#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
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) {
int lim = k;
if ((from / k + (1 << p)) == (n - 1) / k) lim = (n - 1) % k;
for (int to = 0; to < lim; ++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]);
}
}
}
}
function<int(int, int, int)> GetCost = [&](int from, int to, int max_bit) {
int ans = INF;
for (int bit = max_bit - 1; bit >= 0; --bit) {
int gr = from / k + (1 << bit);
if (gr > to / k) continue;
if (gr == to / k) {
return dist[bit][from][to % k];
}
for (int x = gr * k, r = 0; x < (gr + 1) * k; ++x, ++r) {
int res = dist[bit][from][r];
if (res == INF) continue;
res += GetCost(x, to, bit);
ans = min(ans, res);
}
break;
}
return ans;
};
while (q--) {
int a, b; cin >> a >> b;
int ans = GetCost(a, b, log_groups);
if (ans == INF) ans = -1;
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
47120 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
632 KB |
Output is correct |
5 |
Correct |
4 ms |
888 KB |
Output is correct |
6 |
Correct |
4 ms |
1016 KB |
Output is correct |
7 |
Correct |
2 ms |
1016 KB |
Output is correct |
8 |
Correct |
165 ms |
47840 KB |
Output is correct |
9 |
Correct |
145 ms |
47836 KB |
Output is correct |
10 |
Correct |
99 ms |
47040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
201 ms |
45484 KB |
Output is correct |
2 |
Correct |
0 ms |
376 KB |
Output is correct |
3 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
47120 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
632 KB |
Output is correct |
5 |
Correct |
4 ms |
888 KB |
Output is correct |
6 |
Correct |
4 ms |
1016 KB |
Output is correct |
7 |
Correct |
2 ms |
1016 KB |
Output is correct |
8 |
Correct |
165 ms |
47840 KB |
Output is correct |
9 |
Correct |
145 ms |
47836 KB |
Output is correct |
10 |
Correct |
99 ms |
47040 KB |
Output is correct |
11 |
Correct |
201 ms |
45484 KB |
Output is correct |
12 |
Correct |
0 ms |
376 KB |
Output is correct |
13 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |