#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <utility>
#include <cmath>
#include <numeric>
#include <assert.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 50007;
const ll inf = 2e9;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
ll k, n, m, q, dp[M][6][20][6];
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> k >> n >> m >> q;
for(int i = 0; i <= n / k; ++i) for(int j = 0; j <= k; ++j) for(int bit = 0; bit < 20; ++bit) for(int j2 = 0; j2 <= k; ++j2) dp[i][j][bit][j2] = inf;
for(int i = 1; i <= m; ++i){
int a, b, c;
cin >> a >> b >> c;
dp[a / k][a % k][0][b % k] = c;
}
for(int j = 1; j < 20; ++j)
for(int i = 0; i + (1 << j) <= n / k + 1; ++i)
for(int from = 0; from < k; ++from)
for(int to = 0; to < k; ++to)
for(int mid = 0; mid < k; ++mid)
dp[i][from][j][to] = min(dp[i][from][j][to], dp[i][from][j - 1][mid] + dp[i + (1 << (j - 1))][mid][j - 1][to]);
while(q--){
int a, b;
cin >> a >> b;
vector <ll> ans;
for(int i = 0; i < k; ++i) ans.pb(inf);
int cur = a / k;
ans[a % k] = 0;
for(int j = 20; j >= 0; --j){
if(cur + (1 << j) > b / k) continue;
vector <ll> nxt;
for(int i = 0; i < k; ++i) nxt.pb(inf);
for(int from = 0; from < k; ++from) for(int to = 0; to < k; ++to) nxt[to] = min(nxt[to], ans[from] + dp[cur][from][j][to]);
cur += (1 << j);
ans = nxt;
}
if(ans[b % k] >= inf) cout << -1 << endl;
else cout << ans[b % k] << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
137 ms |
282180 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
4 ms |
5964 KB |
Output is correct |
6 |
Correct |
4 ms |
5836 KB |
Output is correct |
7 |
Correct |
3 ms |
5964 KB |
Output is correct |
8 |
Correct |
155 ms |
282108 KB |
Output is correct |
9 |
Correct |
143 ms |
282096 KB |
Output is correct |
10 |
Correct |
141 ms |
282128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
141216 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
8 ms |
5964 KB |
Output is correct |
8 |
Correct |
10 ms |
3156 KB |
Output is correct |
9 |
Correct |
145 ms |
282064 KB |
Output is correct |
10 |
Correct |
125 ms |
94244 KB |
Output is correct |
11 |
Correct |
118 ms |
141216 KB |
Output is correct |
12 |
Correct |
109 ms |
94316 KB |
Output is correct |
13 |
Correct |
81 ms |
34116 KB |
Output is correct |
14 |
Correct |
78 ms |
56652 KB |
Output is correct |
15 |
Correct |
71 ms |
34124 KB |
Output is correct |
16 |
Correct |
71 ms |
34128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
5964 KB |
Output is correct |
7 |
Correct |
2 ms |
3020 KB |
Output is correct |
8 |
Correct |
2 ms |
1356 KB |
Output is correct |
9 |
Correct |
2 ms |
1740 KB |
Output is correct |
10 |
Correct |
139 ms |
282104 KB |
Output is correct |
11 |
Correct |
106 ms |
141148 KB |
Output is correct |
12 |
Correct |
108 ms |
94240 KB |
Output is correct |
13 |
Correct |
115 ms |
94208 KB |
Output is correct |
14 |
Correct |
102 ms |
94232 KB |
Output is correct |
15 |
Correct |
79 ms |
34124 KB |
Output is correct |
16 |
Correct |
62 ms |
34056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
5964 KB |
Output is correct |
7 |
Correct |
2 ms |
3020 KB |
Output is correct |
8 |
Correct |
2 ms |
1356 KB |
Output is correct |
9 |
Correct |
2 ms |
1740 KB |
Output is correct |
10 |
Correct |
139 ms |
282104 KB |
Output is correct |
11 |
Correct |
106 ms |
141148 KB |
Output is correct |
12 |
Correct |
108 ms |
94240 KB |
Output is correct |
13 |
Correct |
115 ms |
94208 KB |
Output is correct |
14 |
Correct |
102 ms |
94232 KB |
Output is correct |
15 |
Correct |
79 ms |
34124 KB |
Output is correct |
16 |
Correct |
62 ms |
34056 KB |
Output is correct |
17 |
Correct |
103 ms |
141212 KB |
Output is correct |
18 |
Correct |
0 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
5 ms |
5964 KB |
Output is correct |
24 |
Correct |
4 ms |
3148 KB |
Output is correct |
25 |
Correct |
5 ms |
1356 KB |
Output is correct |
26 |
Correct |
4 ms |
1740 KB |
Output is correct |
27 |
Correct |
143 ms |
282040 KB |
Output is correct |
28 |
Correct |
110 ms |
94236 KB |
Output is correct |
29 |
Correct |
128 ms |
94236 KB |
Output is correct |
30 |
Correct |
125 ms |
94240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
137 ms |
282180 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
4 ms |
5964 KB |
Output is correct |
6 |
Correct |
4 ms |
5836 KB |
Output is correct |
7 |
Correct |
3 ms |
5964 KB |
Output is correct |
8 |
Correct |
155 ms |
282108 KB |
Output is correct |
9 |
Correct |
143 ms |
282096 KB |
Output is correct |
10 |
Correct |
141 ms |
282128 KB |
Output is correct |
11 |
Correct |
111 ms |
141216 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
8 ms |
5964 KB |
Output is correct |
18 |
Correct |
10 ms |
3156 KB |
Output is correct |
19 |
Correct |
145 ms |
282064 KB |
Output is correct |
20 |
Correct |
125 ms |
94244 KB |
Output is correct |
21 |
Correct |
118 ms |
141216 KB |
Output is correct |
22 |
Correct |
109 ms |
94316 KB |
Output is correct |
23 |
Correct |
81 ms |
34116 KB |
Output is correct |
24 |
Correct |
78 ms |
56652 KB |
Output is correct |
25 |
Correct |
71 ms |
34124 KB |
Output is correct |
26 |
Correct |
71 ms |
34128 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
0 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
3 ms |
5964 KB |
Output is correct |
33 |
Correct |
2 ms |
3020 KB |
Output is correct |
34 |
Correct |
2 ms |
1356 KB |
Output is correct |
35 |
Correct |
2 ms |
1740 KB |
Output is correct |
36 |
Correct |
139 ms |
282104 KB |
Output is correct |
37 |
Correct |
106 ms |
141148 KB |
Output is correct |
38 |
Correct |
108 ms |
94240 KB |
Output is correct |
39 |
Correct |
115 ms |
94208 KB |
Output is correct |
40 |
Correct |
102 ms |
94232 KB |
Output is correct |
41 |
Correct |
79 ms |
34124 KB |
Output is correct |
42 |
Correct |
62 ms |
34056 KB |
Output is correct |
43 |
Correct |
103 ms |
141212 KB |
Output is correct |
44 |
Correct |
0 ms |
336 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
0 ms |
332 KB |
Output is correct |
48 |
Correct |
0 ms |
332 KB |
Output is correct |
49 |
Correct |
5 ms |
5964 KB |
Output is correct |
50 |
Correct |
4 ms |
3148 KB |
Output is correct |
51 |
Correct |
5 ms |
1356 KB |
Output is correct |
52 |
Correct |
4 ms |
1740 KB |
Output is correct |
53 |
Correct |
143 ms |
282040 KB |
Output is correct |
54 |
Correct |
110 ms |
94236 KB |
Output is correct |
55 |
Correct |
128 ms |
94236 KB |
Output is correct |
56 |
Correct |
125 ms |
94240 KB |
Output is correct |
57 |
Correct |
165 ms |
70732 KB |
Output is correct |
58 |
Correct |
147 ms |
282088 KB |
Output is correct |
59 |
Correct |
126 ms |
141216 KB |
Output is correct |