# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
383724 | ntabc05101 | Toll (BOI17_toll) | C++14 | 171 ms | 87020 KiB |
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 inf = 1e9 + 7;
const int mx = 50000;
const int log_mx = 17;
int k, n, m, o;
int c[5 + mx][log_mx][5][5], result[5][5], tmp[5][5];
void combine(int target[5][5], int a[5][5], int b[5][5]) {
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
for (int l = 0; l < k; l++) {
target[i][j] = min(target[i][j], a[i][l] + b[l][j]);
}
}
}
}
#define taskname "BOI17_toll"
int main() {
if (fopen(taskname".in", "r")) {
freopen(taskname".in", "r", stdin);
freopen(taskname".out", "w", stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> k >> n >> m >> o;
for (int i = 0; i < n; i++) {
for (int j = 0; j < log_mx; j++) {
for (int m = 0; m < k; m++) {
for (int l = 0; l < k; l++) {
c[i][j][m][l] = inf;
}
}
}
}
for (int i = 0; i < m; i++) {
int a, b, w; cin >> a >> b >> w;
c[a / k][0][a % k][b % k] = w;
}
for (int j = 1; j < log_mx; j++) {
for (int i = 0; i + (1 << j - 1) < (n + k - 1) / k; i++) {
combine(c[i][j], c[i][j - 1], c[i + (1 << j - 1)][j - 1]);
}
}
/*for (int i = 0; i < n; i++) {
for (int j = 0; j < log_mx; j++) {
for (int m = 0; m < k; m++) {
for (int l = 0; l < k; l++) {
cout << i << " " << j << " " << m << " " << l << " " << c[i][j][m][l] << "\n";
}
}
}
}*/
while (o--) {
int a, b; cin >> a >> b;
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
result[i][j] = inf;
}
}
for (int i = 0; i < k; i++) result[i][i] = 0;
for (int curr = a / k, dest = b / k, i = log_mx - 1; i >= 0; i--) {
if (curr + (1 << i) <= dest) {
for (int j = 0; j < k; j++) {
for (int l = 0; l < k; l++) {
tmp[j][l] = inf;
}
}
combine(tmp, result, c[curr][i]);
for (int j = 0; j < k; j++) {
for (int l = 0; l < k; l++) {
result[j][l] = tmp[j][l];
}
}
curr += (1 << i);
}
}
cout << (result[a % k][b % k] == inf ? -1: result[a % k][b % k]) << "\n";
}
return 0;
}
Compilation message (stderr)
# | 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... |