# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
856759 | vjudge1 | Toll (BOI17_toll) | C++14 | 43 ms | 79700 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;
#define task ""
const int INF = 1e9 + 7;
const int MAX = 50005;
int k, n, m, q;
int dp[16][MAX][5][5];
int tmp[5][5], newTmp[5][5];
bool minimize(int &x, const int &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> k >> n >> m >> q;
for (int j = 0; (1 << j) <= n; ++j) {
for (int i = 0; i < n; ++i) {
for (int a = 0; a < k; ++a)
for (int b = 0; b < k; ++b)
dp[j][i][a][b] = INF;
}
}
while (m--) {
int a, b, t; cin >> a >> b >> t;
minimize(dp[0][a / k][a % k][b % k], t);
}
for (int j = 1; (1 << j) <= n; ++j) {
for (int i = 0; i < n; ++i) if (i / k + (1 << j) <= (n - 1) / k) {
int a = i / k, b = i % k;
for (int c = 0; c < k; ++c) {
for (int d = 0; d < k; ++d)
minimize(dp[j][a][b][d], dp[j - 1][a][b][c] + dp[j - 1][a + (1 << j - 1)][c][d]);
}
}
}
while (q--) {
int a, b; cin >> a >> b; int s = a / k, t = b / k;
for (int a = 0; a < k; ++a) {
for (int b = 0; b < k; ++b) {
tmp[a][b] = 0;
newTmp[a][b] = INF;
}
}
for (int j = 0, l = t - s; (1 << j) <= l; ++j) {
if (l >> j & 1) {
for (int a = 0; a < k; ++a) {
for (int b = 0; b < k; ++b) {
newTmp[a][b] = INF;
}
}
for (int a = 0; a < k; ++a) {
for (int b = 0; b < k; ++b) {
for (int c = 0; c < k; ++c)
minimize(newTmp[a][c], tmp[a][b] + dp[j][s][b][c]);
}
}
for (int a = 0; a < k; ++a) {
for (int b = 0; b < k; ++b)
tmp[a][b] = newTmp[a][b];
}
s += 1 << j;
}
}
cout << (tmp[a % k][b % k] == INF ? -1 : tmp[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... |