#include <bits/stdc++.h>
using namespace std ;
const int inf = 2e9 ;
const int MAX = 5e4 + 10 ;
int arr[MAX][5] ;
int k , n , m , q ;
int dist[MAX][17][5][5] , ans[5] , tmp[5] ;
void preprocess()
{
for(int j = 1 ; j < 17 ; ++j)
{
for(int i = 0 ; i <= n/k ; ++i)
{
for(int k1 = 0 ; k1 < k ; ++k1)
{
for(int k2 = 0 ; k2 < k ; ++k2)
{
int jump = (1 << j) ;
if((i + jump) * k + k2 >= n)
continue ;
dist[i][j][k1][k2] = inf ;
int prv_jump = (1 << (j-1)) ;
for(int k3 = 0 ; k3 < k ; ++k3)
{
if(dist[i][j-1][k1][k3] && dist[i+prv_jump][j-1][k3][k2])
dist[i][j][k1][k2] = min(dist[i][j][k1][k2] , dist[i][j-1][k1][k3] + dist[i + prv_jump][j-1][k3][k2]) ;
}
if(dist[i][j][k1][k2] == inf)
dist[i][j][k1][k2] = 0 ;
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>k>>n>>m>>q ;
for(int i = 0 ; i < m ; ++i)
{
int x , y , z ;
cin>>x>>y>>z ;
dist[x/k][0][x%k][y%k] = z ;
}
preprocess() ;
while(q--)
{
memset(ans , -1 , sizeof(ans)) ;
int x , y ;
cin>>x>>y ;
int a = x/k , b = y/k ;
ans[x%k] = 0 ;
for(int j = 16 ; j >= 0 ; --j)
{
if((a + (1 << j)) > b)
continue ;
for(int k1 = 0 ; k1 < k ; ++k1)
tmp[k1] = ans[k1] ;
for(int k2 = 0 ; k2 < k ; ++k2)
{
ans[k2] = -1 ;
for(int k1 = 0 ; k1 < k ; ++k1)
{
if(dist[a][j][k1][k2] && tmp[k1] != -1)
{
if(ans[k2] == -1)
ans[k2] = dist[a][j][k1][k2] + tmp[k1] ;
else
ans[k2] = min(ans[k2] , dist[a][j][k1][k2] + tmp[k1]) ;
}
}
}
a += (1 << j) ;
}
cout<<ans[y%k]<<"\n" ;
}
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
84588 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
2028 KB |
Output is correct |
6 |
Correct |
3 ms |
2028 KB |
Output is correct |
7 |
Correct |
2 ms |
2028 KB |
Output is correct |
8 |
Correct |
85 ms |
84460 KB |
Output is correct |
9 |
Correct |
82 ms |
84460 KB |
Output is correct |
10 |
Correct |
59 ms |
83692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
43628 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
7 ms |
2156 KB |
Output is correct |
8 |
Correct |
6 ms |
1388 KB |
Output is correct |
9 |
Correct |
80 ms |
84460 KB |
Output is correct |
10 |
Correct |
107 ms |
30444 KB |
Output is correct |
11 |
Correct |
103 ms |
43756 KB |
Output is correct |
12 |
Correct |
90 ms |
29420 KB |
Output is correct |
13 |
Correct |
76 ms |
12780 KB |
Output is correct |
14 |
Correct |
55 ms |
18412 KB |
Output is correct |
15 |
Correct |
62 ms |
11628 KB |
Output is correct |
16 |
Correct |
61 ms |
11628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
2028 KB |
Output is correct |
7 |
Correct |
2 ms |
1260 KB |
Output is correct |
8 |
Correct |
3 ms |
748 KB |
Output is correct |
9 |
Correct |
3 ms |
876 KB |
Output is correct |
10 |
Correct |
75 ms |
84332 KB |
Output is correct |
11 |
Correct |
90 ms |
43500 KB |
Output is correct |
12 |
Correct |
97 ms |
30316 KB |
Output is correct |
13 |
Correct |
99 ms |
30444 KB |
Output is correct |
14 |
Correct |
89 ms |
29932 KB |
Output is correct |
15 |
Correct |
58 ms |
11500 KB |
Output is correct |
16 |
Correct |
57 ms |
11500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
2028 KB |
Output is correct |
7 |
Correct |
2 ms |
1260 KB |
Output is correct |
8 |
Correct |
3 ms |
748 KB |
Output is correct |
9 |
Correct |
3 ms |
876 KB |
Output is correct |
10 |
Correct |
75 ms |
84332 KB |
Output is correct |
11 |
Correct |
90 ms |
43500 KB |
Output is correct |
12 |
Correct |
97 ms |
30316 KB |
Output is correct |
13 |
Correct |
99 ms |
30444 KB |
Output is correct |
14 |
Correct |
89 ms |
29932 KB |
Output is correct |
15 |
Correct |
58 ms |
11500 KB |
Output is correct |
16 |
Correct |
57 ms |
11500 KB |
Output is correct |
17 |
Correct |
91 ms |
43500 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
3 ms |
2028 KB |
Output is correct |
24 |
Correct |
3 ms |
1260 KB |
Output is correct |
25 |
Correct |
4 ms |
748 KB |
Output is correct |
26 |
Correct |
4 ms |
876 KB |
Output is correct |
27 |
Correct |
85 ms |
84460 KB |
Output is correct |
28 |
Correct |
101 ms |
30316 KB |
Output is correct |
29 |
Correct |
102 ms |
30572 KB |
Output is correct |
30 |
Correct |
94 ms |
29932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
84588 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
2028 KB |
Output is correct |
6 |
Correct |
3 ms |
2028 KB |
Output is correct |
7 |
Correct |
2 ms |
2028 KB |
Output is correct |
8 |
Correct |
85 ms |
84460 KB |
Output is correct |
9 |
Correct |
82 ms |
84460 KB |
Output is correct |
10 |
Correct |
59 ms |
83692 KB |
Output is correct |
11 |
Correct |
98 ms |
43628 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
7 ms |
2156 KB |
Output is correct |
18 |
Correct |
6 ms |
1388 KB |
Output is correct |
19 |
Correct |
80 ms |
84460 KB |
Output is correct |
20 |
Correct |
107 ms |
30444 KB |
Output is correct |
21 |
Correct |
103 ms |
43756 KB |
Output is correct |
22 |
Correct |
90 ms |
29420 KB |
Output is correct |
23 |
Correct |
76 ms |
12780 KB |
Output is correct |
24 |
Correct |
55 ms |
18412 KB |
Output is correct |
25 |
Correct |
62 ms |
11628 KB |
Output is correct |
26 |
Correct |
61 ms |
11628 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
2 ms |
2028 KB |
Output is correct |
33 |
Correct |
2 ms |
1260 KB |
Output is correct |
34 |
Correct |
3 ms |
748 KB |
Output is correct |
35 |
Correct |
3 ms |
876 KB |
Output is correct |
36 |
Correct |
75 ms |
84332 KB |
Output is correct |
37 |
Correct |
90 ms |
43500 KB |
Output is correct |
38 |
Correct |
97 ms |
30316 KB |
Output is correct |
39 |
Correct |
99 ms |
30444 KB |
Output is correct |
40 |
Correct |
89 ms |
29932 KB |
Output is correct |
41 |
Correct |
58 ms |
11500 KB |
Output is correct |
42 |
Correct |
57 ms |
11500 KB |
Output is correct |
43 |
Correct |
91 ms |
43500 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
3 ms |
2028 KB |
Output is correct |
50 |
Correct |
3 ms |
1260 KB |
Output is correct |
51 |
Correct |
4 ms |
748 KB |
Output is correct |
52 |
Correct |
4 ms |
876 KB |
Output is correct |
53 |
Correct |
85 ms |
84460 KB |
Output is correct |
54 |
Correct |
101 ms |
30316 KB |
Output is correct |
55 |
Correct |
102 ms |
30572 KB |
Output is correct |
56 |
Correct |
94 ms |
29932 KB |
Output is correct |
57 |
Correct |
125 ms |
24556 KB |
Output is correct |
58 |
Correct |
85 ms |
84460 KB |
Output is correct |
59 |
Correct |
111 ms |
43756 KB |
Output is correct |