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>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 5e4 + 2;
const int inf = 2e9;
const int mod = 1e9 + 7;
int lift[N][16][5];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int k, n, m, q;
cin >> k >> n >> m >> q;
for (int i = 0; i < n; i++) {
for (int l = 0; l < 16; l++) {
for (int o = 0; o < k; o++) {
lift[i][l][o] = inf;
}
}
}
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
lift[a][0][b % k] = c;
}
for (int l = 1; l < 16; l++) {
for (int i = 0; i < n; i++) {
int nesto = k * (i / k + (1 << l) / 2);
int pom = k * (i / k + (1 << l));
for (int o = 0; o < k; o++) {
if (pom + o >= n) break;
for (int oo = 0; oo < k; oo++) {
if (lift[i][l - 1][oo] == inf || lift[nesto + oo][l - 1][o] == inf) continue;
smin(lift[i][l][o], lift[i][l - 1][oo] + lift[nesto + oo][l - 1][o]);
}
}
}
}
while (q--) {
int a, b;
cin >> a >> b;
int dn = b / k - a / k;
int cur = a / k;
vector<int> mn(k, inf);
mn[a % k] = 0;
for (int i = 0; i < 16; i++) {
if ((1 << i) & dn) {
vector<int> novi(k, inf);
for (int o = 0; o < k; o++) {
for (int oo = 0; oo < k; oo++) {
if (mn[oo] == inf || lift[cur * k + oo][i][o] == inf) continue;
smin(novi[o], mn[oo] + lift[cur * k + oo][i][o]);
}
}
cur += (1 << i);
swap(mn, novi);
}
}
if (mn[b % k] == inf) mn[b % k] = -1;
cout << mn[b % k] << en;
}
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... |