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>
using namespace std;
#define int long long
#define N 50005
#define log 17
#define K 5
#define oo 1e18
int dp[N][log][K][K], res[K][K], tmp[K][K], k, n, m, o;
void add(int rt[K][K], int a[K][K], int b[K][K]){
for (int z = 0; z < k; z++){
for (int x = 0; x < k; x++){
for (int y = 0; y < k; y++){
rt[x][y] = min(rt[x][y], a[x][z] + b[z][y]);
}
}
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> k >> n >> m >> o;
for (int i = 0; i < n; i++){
for (int j = 0; j < log; j++){
for (int k = 0; k < K; k++){
for (int k1 = 0; k1 < K; k1++){
dp[i][j][k][k1] = oo;
}
}
}
}
while(m--){
int a, b, t;
cin >> a >> b >> t;
dp[a / k][0][a % k][b % k] = t;
}
for (int j = 1; j < log; j++){
for (int i = 0; i + (1 << j) < (n + k - 1) / k; i++){
add(dp[i][j], dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
}
}
while (o--){
int a, b;
cin >> a >> b;
for (int i = 0; i < K; i++){
for (int j = 0; j < K; j++){
res[i][j] = oo;
if (i == j) res[i][j] = 0;
}
}
int da = a / k, db = b / k;
for (int j = log - 1; j >= 0; j--){
if (da + (1 << j) <= db){
for (int ii = 0; ii < K; ii++){
for (int jj = 0; jj < K; jj++){
tmp[ii][jj] = oo;
}
}
add(tmp, res, dp[da][j]);
for (int ii = 0; ii < K; ii++){
for (int jj = 0; jj < K; jj++){
res[ii][jj] = tmp[ii][jj];
}
}
da += (1 << j);
}
}
if (res[a % k][b % k] == oo) cout << -1 << "\n"; else cout << res[a % k][b % k] << "\n";
}
return 0;
}
# | 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... |