#include <bits/stdc++.h>
#define ll long long
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define left (h<<1),l,(l + r)/2
#define right ((h<<1)|1),(l + r)/2 + 1,r
#define int ll
using namespace std;
const int N = 50000 + 3,inf = 1e18;
int d[N][25][7][7];
int dp[7][7],dpold[7][7];
signed main(){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int k,n,m,q;
cin>>k>>n>>m>>q;
for (int i = 0; i <= n/k; i++)
for (int D=0;D<=20;D++)
for (int j = 0; j < k; j++)
for (int t=0;t<k;t++)
d[i][D][j][t] = inf;
for (int i = 1; i <= m; i++){
int a,b,c;
cin>>a>>b>>c;
d[a/k][0][a%k][b%k] = c;
}
for (int j = 1; j <= 20; j++){
for (int i = 1; i <= n; i+=k){
if (i/k + (1LL<<(j - 1)) > (n/k)) continue;
for (int x = 0; x < k; x++){
for (int y = 0; y < k;y++){
for (int z=0;z<k;z++){
d[i/k][j][x][y] = min(d[i/k][j][x][y],d[i/k][j - 1][x][z] + d[i/k + (1LL<<(j-1))][j - 1][z][y]);
}
}
}
}
}
while (q--){
int x,y;
cin>>x>>y;
if (x/k >= y/k){
cout<<-1<<endl;
continue;
}
for (int i = 0; i < k; i++)
for (int j = 0; j < k; j++)
dp[i][j] = inf,dpold[i][j] = inf;
dpold[x%k][x%k] = 0;
int len = y/k - x/k,cur = x/k;
for (int i = 20; i >= 0; i--){
if (len >= (1LL<<i)) {
len -= (1LL<<i);
for (int y=0;y<k;y++)
for (int z=0;z<k;z++)
dp[x%k][y] = min(dp[x%k][y],dpold[x%k][z] + d[cur][i][z][y]);
for (int y=0;y<k;y++)
for (int z=0;z<k;z++)
dpold[y][z] = dp[y][z],dp[y][z] = inf;
cur += (1LL<<i);
}
}
if (dpold[x%k][y%k] == inf) cout<<-1<<endl;
else cout<<dpold[x%k][y%k]<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
480872 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
10844 KB |
Output is correct |
6 |
Correct |
5 ms |
10840 KB |
Output is correct |
7 |
Correct |
3 ms |
10844 KB |
Output is correct |
8 |
Correct |
102 ms |
480816 KB |
Output is correct |
9 |
Correct |
107 ms |
480632 KB |
Output is correct |
10 |
Correct |
104 ms |
480020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
242256 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
464 KB |
Output is correct |
7 |
Incorrect |
15 ms |
10844 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
472 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
87 ms |
480592 KB |
Output is correct |
11 |
Correct |
78 ms |
242228 KB |
Output is correct |
12 |
Correct |
86 ms |
162772 KB |
Output is correct |
13 |
Correct |
85 ms |
162900 KB |
Output is correct |
14 |
Correct |
95 ms |
162384 KB |
Output is correct |
15 |
Correct |
40 ms |
59104 KB |
Output is correct |
16 |
Correct |
42 ms |
59228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
472 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
87 ms |
480592 KB |
Output is correct |
11 |
Correct |
78 ms |
242228 KB |
Output is correct |
12 |
Correct |
86 ms |
162772 KB |
Output is correct |
13 |
Correct |
85 ms |
162900 KB |
Output is correct |
14 |
Correct |
95 ms |
162384 KB |
Output is correct |
15 |
Correct |
40 ms |
59104 KB |
Output is correct |
16 |
Correct |
42 ms |
59228 KB |
Output is correct |
17 |
Correct |
105 ms |
242436 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
6 ms |
10940 KB |
Output is correct |
24 |
Correct |
6 ms |
6748 KB |
Output is correct |
25 |
Correct |
6 ms |
2652 KB |
Output is correct |
26 |
Correct |
6 ms |
4696 KB |
Output is correct |
27 |
Correct |
98 ms |
480744 KB |
Output is correct |
28 |
Correct |
92 ms |
162884 KB |
Output is correct |
29 |
Correct |
100 ms |
163000 KB |
Output is correct |
30 |
Correct |
92 ms |
162388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
480872 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
10844 KB |
Output is correct |
6 |
Correct |
5 ms |
10840 KB |
Output is correct |
7 |
Correct |
3 ms |
10844 KB |
Output is correct |
8 |
Correct |
102 ms |
480816 KB |
Output is correct |
9 |
Correct |
107 ms |
480632 KB |
Output is correct |
10 |
Correct |
104 ms |
480020 KB |
Output is correct |
11 |
Correct |
101 ms |
242256 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
464 KB |
Output is correct |
17 |
Incorrect |
15 ms |
10844 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |