# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
444550 | BeanZ | Toll (BOI17_toll) | C++14 | 317 ms | 189508 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.
// I_Love_LPL 1y0m6d
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 5e4 + 5;
long long mod = 1e9 + 7;
const int lim = 4e5 + 5;
const int lg = 18;
const int base = 37;
const long double eps = 1e-6;
ll dp[N][lg + 1][5][5], temp[5], res[5];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("tests.inp", "r")){
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
ll k, n, m, q;
cin >> k >> n >> m >> q;
for (int i = 0; i < n; i++){
for (int j = 0; j <= lg; j++){
for (int g = 0; g < k; g++){
for (int t = 0; t < k; t++){
dp[i][j][g][t] = 1e9;
}
}
}
}
for (int i = 1; i <= m; i++){
ll u, v, c;
cin >> u >> v >> c;
dp[u][0][u % k][v % k] = c;
}
for (int lo = 1; lo <= lg; lo++){
for (int i = 0; i < n; i++){
for (int j = 0; j < k; j++){ // des
for (int g = 0; g < k; g++){ // trans
ll id = i + k * (1 << (lo - 1)) - (i % k) + (g % k);
if (id >= N) continue;
dp[i][lo][i % k][j] = min(dp[i][lo][i % k][j], dp[i][lo - 1][i % k][g] + dp[i + k * (1 << (lo - 1)) - (i % k) + (g % k)][lo - 1][g % k][j]);
}
}
}
}
while (q--){
ll u, v;
cin >> u >> v;
ll dep = (v / k) - (u / k);
for (int i = 0; i < k; i++) res[i] = 1e9;
res[u % k] = 0;
ll cur = u / k;
for (int i = 0; i <= lg; i++){
for (int g = 0; g < k; g++) temp[g] = 1e9;
if (dep & (1 << i)){
for (int g = 0; g < k; g++){ // des
for (int j = 0; j < k; j++){ // trans
temp[g] = min(temp[g], res[j] + dp[cur * k + j][i][j][g]);
}
}
cur = cur + (1 << i);
for (int g = 0; g < k; g++) res[g] = temp[g];
}
}
if (res[v % k] < 1e9) cout << res[v % k] << endl;
else cout << -1 << endl;
}
}
/*
Ans:
Out:
*/
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... |