#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 = 0; i <= n/k; i++){
if (i + (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][j][x][y] = min(d[i][j][x][y],d[i][j - 1][x][z] + d[i + (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] = 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 |
114 ms |
479900 KB |
Output is correct |
2 |
Correct |
1 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 |
3 ms |
10840 KB |
Output is correct |
6 |
Correct |
3 ms |
10844 KB |
Output is correct |
7 |
Correct |
3 ms |
10680 KB |
Output is correct |
8 |
Correct |
96 ms |
479824 KB |
Output is correct |
9 |
Correct |
96 ms |
479828 KB |
Output is correct |
10 |
Correct |
87 ms |
479824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
240704 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
15 ms |
10844 KB |
Output is correct |
8 |
Correct |
17 ms |
6892 KB |
Output is correct |
9 |
Correct |
94 ms |
480800 KB |
Output is correct |
10 |
Correct |
103 ms |
162928 KB |
Output is correct |
11 |
Correct |
90 ms |
242388 KB |
Output is correct |
12 |
Correct |
83 ms |
161972 KB |
Output is correct |
13 |
Correct |
64 ms |
60244 KB |
Output is correct |
14 |
Correct |
51 ms |
98520 KB |
Output is correct |
15 |
Correct |
57 ms |
59284 KB |
Output is correct |
16 |
Correct |
46 ms |
59100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
84 ms |
479884 KB |
Output is correct |
11 |
Correct |
83 ms |
240704 KB |
Output is correct |
12 |
Correct |
79 ms |
160600 KB |
Output is correct |
13 |
Correct |
82 ms |
160600 KB |
Output is correct |
14 |
Correct |
84 ms |
160600 KB |
Output is correct |
15 |
Correct |
42 ms |
57948 KB |
Output is correct |
16 |
Correct |
37 ms |
57948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
84 ms |
479884 KB |
Output is correct |
11 |
Correct |
83 ms |
240704 KB |
Output is correct |
12 |
Correct |
79 ms |
160600 KB |
Output is correct |
13 |
Correct |
82 ms |
160600 KB |
Output is correct |
14 |
Correct |
84 ms |
160600 KB |
Output is correct |
15 |
Correct |
42 ms |
57948 KB |
Output is correct |
16 |
Correct |
37 ms |
57948 KB |
Output is correct |
17 |
Correct |
80 ms |
240700 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 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 |
344 KB |
Output is correct |
23 |
Correct |
6 ms |
10840 KB |
Output is correct |
24 |
Correct |
5 ms |
6748 KB |
Output is correct |
25 |
Correct |
6 ms |
2652 KB |
Output is correct |
26 |
Correct |
6 ms |
4700 KB |
Output is correct |
27 |
Correct |
89 ms |
479832 KB |
Output is correct |
28 |
Correct |
103 ms |
160684 KB |
Output is correct |
29 |
Correct |
87 ms |
160680 KB |
Output is correct |
30 |
Correct |
79 ms |
160604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
114 ms |
479900 KB |
Output is correct |
2 |
Correct |
1 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 |
3 ms |
10840 KB |
Output is correct |
6 |
Correct |
3 ms |
10844 KB |
Output is correct |
7 |
Correct |
3 ms |
10680 KB |
Output is correct |
8 |
Correct |
96 ms |
479824 KB |
Output is correct |
9 |
Correct |
96 ms |
479828 KB |
Output is correct |
10 |
Correct |
87 ms |
479824 KB |
Output is correct |
11 |
Correct |
86 ms |
240704 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
15 ms |
10844 KB |
Output is correct |
18 |
Correct |
17 ms |
6892 KB |
Output is correct |
19 |
Correct |
94 ms |
480800 KB |
Output is correct |
20 |
Correct |
103 ms |
162928 KB |
Output is correct |
21 |
Correct |
90 ms |
242388 KB |
Output is correct |
22 |
Correct |
83 ms |
161972 KB |
Output is correct |
23 |
Correct |
64 ms |
60244 KB |
Output is correct |
24 |
Correct |
51 ms |
98520 KB |
Output is correct |
25 |
Correct |
57 ms |
59284 KB |
Output is correct |
26 |
Correct |
46 ms |
59100 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
2 ms |
10844 KB |
Output is correct |
33 |
Correct |
2 ms |
6744 KB |
Output is correct |
34 |
Correct |
2 ms |
2652 KB |
Output is correct |
35 |
Correct |
2 ms |
4700 KB |
Output is correct |
36 |
Correct |
84 ms |
479884 KB |
Output is correct |
37 |
Correct |
83 ms |
240704 KB |
Output is correct |
38 |
Correct |
79 ms |
160600 KB |
Output is correct |
39 |
Correct |
82 ms |
160600 KB |
Output is correct |
40 |
Correct |
84 ms |
160600 KB |
Output is correct |
41 |
Correct |
42 ms |
57948 KB |
Output is correct |
42 |
Correct |
37 ms |
57948 KB |
Output is correct |
43 |
Correct |
80 ms |
240700 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
344 KB |
Output is correct |
49 |
Correct |
6 ms |
10840 KB |
Output is correct |
50 |
Correct |
5 ms |
6748 KB |
Output is correct |
51 |
Correct |
6 ms |
2652 KB |
Output is correct |
52 |
Correct |
6 ms |
4700 KB |
Output is correct |
53 |
Correct |
89 ms |
479832 KB |
Output is correct |
54 |
Correct |
103 ms |
160684 KB |
Output is correct |
55 |
Correct |
87 ms |
160680 KB |
Output is correct |
56 |
Correct |
79 ms |
160604 KB |
Output is correct |
57 |
Correct |
110 ms |
125000 KB |
Output is correct |
58 |
Correct |
98 ms |
480592 KB |
Output is correct |
59 |
Correct |
98 ms |
242356 KB |
Output is correct |