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 rep(i,s,e) for (int i = s; i <= e; ++i)
#define rrep(i,s,e) for (int i = s; i >= e; --i)
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(a) a.begin(), a.end()
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int k, n, m, o; cin >> k >> n >> m >> o;
int d[n/k+1][30][k][k];
memset(d, -1, sizeof(d));
rep (i,0,m-1) {
int a, b, t; cin >> a >> b >> t;
d[a/k][0][a%k][b%k] = t;
}
rep (i,1,29) {
for (int j = 0; j/k+(1<<i) <= n/k; ++j) {
rep (a,0,k-1) {
rep (b,0,k-1) {
rep (c,0,k-1) {
int d1 = d[j/k][i-1][a][b];
int d2 = d[j/k+(1<<(i-1))][i-1][b][c];
if (min(d1,d2)==-1) continue;
if (d[j/k][i][a][c]==-1 || d[j/k][i][a][c]>d1+d2) d[j/k][i][a][c] = d1+d2;
}
}
}
}
}
while (o--) {
int a, b; cin >> a >> b;
int g = b/k-a/k, l = 0;
int ways[k], c = a/k;
memset(ways, -1, 4*k);
ways[a%k] = 0;
while (g) {
int new_ways[k];
memset(new_ways, -1, 4*k);
if (g%2) {
rep (x,0,k-1) {
rep (y,0,k-1) {
if (min(d[c][l][x][y], ways[x])==-1) continue;
if (new_ways[y]==-1 || new_ways[y]>ways[x]+d[c][l][x][y])
new_ways[y] = ways[x]+d[c][l][x][y];
}
}
c += (1<<l);
memcpy(ways, new_ways, 4*k);
}
l++;
g/=2;
}
cout << ways[b%k] << "\n";
}
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... |