#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 50002;
const int MAXK = 5;
const int MOD = 1000000007;
int K, N, M, O;
int dp[MAXN][18][MAXK][MAXK];
int ans[MAXK][MAXK], tmp[MAXK][MAXK];
void minimize(int x[MAXK][MAXK], int a[MAXK][MAXK], int b[MAXK][MAXK]) {
for (int i = 0; i < K; i++) {
for (int j = 0; j < K; j++) {
for (int k = 0; k < K; k++) {
x[i][j] = min(x[i][j], a[i][k]+b[k][j]);
}
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> K >> N >> M >> O;
memset(dp, 0x3f, sizeof dp);
for (int i = 0; i < M; i++) {
int a, b, t;
cin >> a >> b >> t;
dp[a/K][0][a%K][b%K] = t;
}
for (int i = 1; i < 18; i++) {
for (int j = 0; j < (N+K-1)/K-(1<<i); j++) {
minimize(dp[j][i], dp[j][i-1], dp[j+(1<<(i-1))][i-1]);
}
}
for (int o = 0; o < O; o++) {
int a, b; cin >> a >> b;
memset(ans, 0x3f, sizeof ans);
for (int i = 0; i < K; i++) ans[i][i] = 0;
int cur = a/K, dest = b/K;
for (int i = 17; i >= 0; i--) {
if (cur + (1<<i) <= dest) {
memset(tmp, 0x3f, sizeof tmp);
minimize(tmp, ans, dp[cur][i]);
memcpy(ans, tmp, sizeof ans);
cur += (1<<i);
}
}
if (ans[a%K][b%K] == 0x3f3f3f3f) cout << -1;
else cout << ans[a%K][b%K];
cout << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
89464 KB |
Output is correct |
2 |
Correct |
54 ms |
88436 KB |
Output is correct |
3 |
Correct |
49 ms |
88444 KB |
Output is correct |
4 |
Correct |
52 ms |
88448 KB |
Output is correct |
5 |
Correct |
52 ms |
88440 KB |
Output is correct |
6 |
Correct |
56 ms |
88440 KB |
Output is correct |
7 |
Correct |
52 ms |
88392 KB |
Output is correct |
8 |
Correct |
107 ms |
89336 KB |
Output is correct |
9 |
Correct |
111 ms |
89336 KB |
Output is correct |
10 |
Correct |
94 ms |
88568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
90104 KB |
Output is correct |
2 |
Correct |
51 ms |
88348 KB |
Output is correct |
3 |
Correct |
55 ms |
88440 KB |
Output is correct |
4 |
Correct |
49 ms |
88440 KB |
Output is correct |
5 |
Correct |
49 ms |
88440 KB |
Output is correct |
6 |
Correct |
54 ms |
88440 KB |
Output is correct |
7 |
Correct |
57 ms |
88568 KB |
Output is correct |
8 |
Correct |
54 ms |
88568 KB |
Output is correct |
9 |
Correct |
102 ms |
89336 KB |
Output is correct |
10 |
Correct |
135 ms |
90744 KB |
Output is correct |
11 |
Correct |
133 ms |
90232 KB |
Output is correct |
12 |
Correct |
116 ms |
89720 KB |
Output is correct |
13 |
Correct |
122 ms |
90872 KB |
Output is correct |
14 |
Correct |
94 ms |
89848 KB |
Output is correct |
15 |
Correct |
107 ms |
89656 KB |
Output is correct |
16 |
Correct |
93 ms |
89692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
88440 KB |
Output is correct |
2 |
Correct |
52 ms |
88440 KB |
Output is correct |
3 |
Correct |
50 ms |
88440 KB |
Output is correct |
4 |
Correct |
48 ms |
88440 KB |
Output is correct |
5 |
Correct |
49 ms |
88440 KB |
Output is correct |
6 |
Correct |
51 ms |
88440 KB |
Output is correct |
7 |
Correct |
53 ms |
88440 KB |
Output is correct |
8 |
Correct |
50 ms |
88440 KB |
Output is correct |
9 |
Correct |
51 ms |
88440 KB |
Output is correct |
10 |
Correct |
94 ms |
89208 KB |
Output is correct |
11 |
Correct |
122 ms |
89976 KB |
Output is correct |
12 |
Correct |
133 ms |
90608 KB |
Output is correct |
13 |
Correct |
139 ms |
90784 KB |
Output is correct |
14 |
Correct |
121 ms |
90232 KB |
Output is correct |
15 |
Correct |
88 ms |
89592 KB |
Output is correct |
16 |
Correct |
92 ms |
89592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
88440 KB |
Output is correct |
2 |
Correct |
52 ms |
88440 KB |
Output is correct |
3 |
Correct |
50 ms |
88440 KB |
Output is correct |
4 |
Correct |
48 ms |
88440 KB |
Output is correct |
5 |
Correct |
49 ms |
88440 KB |
Output is correct |
6 |
Correct |
51 ms |
88440 KB |
Output is correct |
7 |
Correct |
53 ms |
88440 KB |
Output is correct |
8 |
Correct |
50 ms |
88440 KB |
Output is correct |
9 |
Correct |
51 ms |
88440 KB |
Output is correct |
10 |
Correct |
94 ms |
89208 KB |
Output is correct |
11 |
Correct |
122 ms |
89976 KB |
Output is correct |
12 |
Correct |
133 ms |
90608 KB |
Output is correct |
13 |
Correct |
139 ms |
90784 KB |
Output is correct |
14 |
Correct |
121 ms |
90232 KB |
Output is correct |
15 |
Correct |
88 ms |
89592 KB |
Output is correct |
16 |
Correct |
92 ms |
89592 KB |
Output is correct |
17 |
Correct |
127 ms |
90000 KB |
Output is correct |
18 |
Correct |
51 ms |
88440 KB |
Output is correct |
19 |
Correct |
50 ms |
88440 KB |
Output is correct |
20 |
Correct |
53 ms |
88440 KB |
Output is correct |
21 |
Correct |
49 ms |
88448 KB |
Output is correct |
22 |
Correct |
62 ms |
88440 KB |
Output is correct |
23 |
Correct |
52 ms |
88440 KB |
Output is correct |
24 |
Correct |
51 ms |
88440 KB |
Output is correct |
25 |
Correct |
66 ms |
88440 KB |
Output is correct |
26 |
Correct |
54 ms |
88440 KB |
Output is correct |
27 |
Correct |
103 ms |
89208 KB |
Output is correct |
28 |
Correct |
137 ms |
90620 KB |
Output is correct |
29 |
Correct |
160 ms |
90872 KB |
Output is correct |
30 |
Correct |
128 ms |
90232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
89464 KB |
Output is correct |
2 |
Correct |
54 ms |
88436 KB |
Output is correct |
3 |
Correct |
49 ms |
88444 KB |
Output is correct |
4 |
Correct |
52 ms |
88448 KB |
Output is correct |
5 |
Correct |
52 ms |
88440 KB |
Output is correct |
6 |
Correct |
56 ms |
88440 KB |
Output is correct |
7 |
Correct |
52 ms |
88392 KB |
Output is correct |
8 |
Correct |
107 ms |
89336 KB |
Output is correct |
9 |
Correct |
111 ms |
89336 KB |
Output is correct |
10 |
Correct |
94 ms |
88568 KB |
Output is correct |
11 |
Correct |
129 ms |
90104 KB |
Output is correct |
12 |
Correct |
51 ms |
88348 KB |
Output is correct |
13 |
Correct |
55 ms |
88440 KB |
Output is correct |
14 |
Correct |
49 ms |
88440 KB |
Output is correct |
15 |
Correct |
49 ms |
88440 KB |
Output is correct |
16 |
Correct |
54 ms |
88440 KB |
Output is correct |
17 |
Correct |
57 ms |
88568 KB |
Output is correct |
18 |
Correct |
54 ms |
88568 KB |
Output is correct |
19 |
Correct |
102 ms |
89336 KB |
Output is correct |
20 |
Correct |
135 ms |
90744 KB |
Output is correct |
21 |
Correct |
133 ms |
90232 KB |
Output is correct |
22 |
Correct |
116 ms |
89720 KB |
Output is correct |
23 |
Correct |
122 ms |
90872 KB |
Output is correct |
24 |
Correct |
94 ms |
89848 KB |
Output is correct |
25 |
Correct |
107 ms |
89656 KB |
Output is correct |
26 |
Correct |
93 ms |
89692 KB |
Output is correct |
27 |
Correct |
50 ms |
88440 KB |
Output is correct |
28 |
Correct |
52 ms |
88440 KB |
Output is correct |
29 |
Correct |
50 ms |
88440 KB |
Output is correct |
30 |
Correct |
48 ms |
88440 KB |
Output is correct |
31 |
Correct |
49 ms |
88440 KB |
Output is correct |
32 |
Correct |
51 ms |
88440 KB |
Output is correct |
33 |
Correct |
53 ms |
88440 KB |
Output is correct |
34 |
Correct |
50 ms |
88440 KB |
Output is correct |
35 |
Correct |
51 ms |
88440 KB |
Output is correct |
36 |
Correct |
94 ms |
89208 KB |
Output is correct |
37 |
Correct |
122 ms |
89976 KB |
Output is correct |
38 |
Correct |
133 ms |
90608 KB |
Output is correct |
39 |
Correct |
139 ms |
90784 KB |
Output is correct |
40 |
Correct |
121 ms |
90232 KB |
Output is correct |
41 |
Correct |
88 ms |
89592 KB |
Output is correct |
42 |
Correct |
92 ms |
89592 KB |
Output is correct |
43 |
Correct |
127 ms |
90000 KB |
Output is correct |
44 |
Correct |
51 ms |
88440 KB |
Output is correct |
45 |
Correct |
50 ms |
88440 KB |
Output is correct |
46 |
Correct |
53 ms |
88440 KB |
Output is correct |
47 |
Correct |
49 ms |
88448 KB |
Output is correct |
48 |
Correct |
62 ms |
88440 KB |
Output is correct |
49 |
Correct |
52 ms |
88440 KB |
Output is correct |
50 |
Correct |
51 ms |
88440 KB |
Output is correct |
51 |
Correct |
66 ms |
88440 KB |
Output is correct |
52 |
Correct |
54 ms |
88440 KB |
Output is correct |
53 |
Correct |
103 ms |
89208 KB |
Output is correct |
54 |
Correct |
137 ms |
90620 KB |
Output is correct |
55 |
Correct |
160 ms |
90872 KB |
Output is correct |
56 |
Correct |
128 ms |
90232 KB |
Output is correct |
57 |
Correct |
171 ms |
91768 KB |
Output is correct |
58 |
Correct |
114 ms |
89336 KB |
Output is correct |
59 |
Correct |
154 ms |
90232 KB |
Output is correct |