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;
const int maxn = 50000;
const int maxo = 10000;
const int maxk = 5;
const int logn = 19;
const int INF = 1e9;
int k, n, m, o;
pair<int,int> orders[maxo];
int dp[maxn][logn][maxk][maxk]; // from k*maxn+maxk1 --> k*(maxn+2^logn) + maxk2
int main() {
// freopen("main.in", "r", stdin);
fill(dp[0][0][0], dp[0][0][0] + maxn*logn*maxk*maxk, INF);
cin >> k >> n >> m >> o;
for (int i = 0; i < m; i++) {
int a, b, t; cin >> a >> b >> t;
dp[a / k][0][a%k][b%k] = t;
}
// for (int i = 0; i <= 1; i++) {
// for (int a = 0; a < 5; a++) {
// for (int b = 0; b < 5; b++) {
// cout << dp[i][0][a][b] << ' ';
// }
// cout << endl;
// }
// }
// set up binary lifting
for (int i = 1; i < logn; i++) { // for each power of 2 jump
for (int j = 0; j + (1 << i) <= (n-k+1)/k; j++) { // do all j where j+(power of 2) is still an actual section
for (int a = 0; a < k; a++) {
for (int b = 0; b < k; b++) {
for (int c = 0; c < k; c++) {
dp[j][i][a][c] = min(
dp[j][i][a][c],
dp[j][i-1][a][b]+
dp[j+(1<<(i-1))][i-1][b][c]
);
}
}
}
// cout << j << ' ' << j+(1<<i) << endl;
// for (int a = 0; a < 5; a++) {
// for (int b = 0; b < 5; b++) {
// cout << dp[j][i][a][b] << ' ';
// }
// cout << endl;
// }
}
}
int ans[k][k], ans2[k][k];
for (int i = 0; i < o; i++) {
int a, b; cin >> a >> b;
int A = a/k, B = b/k;
fill(ans[0], ans[0] + k*k, INF);
// for (int j = 0; j < k; j++) ans[j][j] = 0;
ans[a%k][a%k] = 0; // because we are starting at a%k, and only care about paths from it
// a->b where a =/= b doesn't make sense since we haven't made jumps, and rest we aren't starting at it
for (int j = logn-1; j >= 0; j--)
if (((B-A)>>j) & 1) {
// cout << B << ' ' << A << ' ' << j << endl;
// make jump of size j
fill(ans2[0], ans2[0] + k*k, INF);
for (int c = 0; c < k; c++)
for (int d = 0; d < k; d++)
for (int e = 0; e < k; e++) {
// if (a%k == c && a%k == d && b%k == e) cout << A << ' ' << j << ' ' << d << ' ' << e << ' ' << ans[c][d] << ' ' << dp[A][j][d][e] << endl;
ans2[c][e] = min(
ans2[c][e],
ans[c][d] + dp[A][j][d][e]
);
}
A += (1<<j);
for (int c = 0; c < k; c++)
for (int d = 0; d < k; d++)
ans[c][d] = ans2[c][d];
}
cout << (ans[a%k][b%k] == INF ? -1 : ans[a%k][b%k]) << endl;
}
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... |