#include <bits/stdc++.h>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
using ull = unsigned ll;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000009;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 5e4 + 1;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int k, n, m, o;
ll dp[maxN][18][5][5];
void Init(){
cin >> k >> n >> m >> o;
memset(dp, 0x3f, sizeof(dp));
int x = __lg(n) + 1;
for (int i = 1; i <= m; ++i){
int a, b, w;
cin >> a >> b >> w;
dp[a / k][0][a % k][b % k] = w;
}
for (int i = 1; i <= x; ++i){
for (int j = 0; j + (1 << (i - 1)) < n / k; ++j){
for (int p = 0; p < k; ++p){
for (int q = 0; q < k; ++q){
for (int r = 0; r < k; ++r){
dp[j][i][p][r] = min(dp[j][i][p][r], dp[j][i - 1][p][q] + dp[j + (1 << (i - 1))][i - 1][q][r]);
}
}
}
}
}
while (o--){
int a, b;
cin >> a >> b;
int u = a % k, v = b % k, s = a / k, t = b / k - a / k;
ll ans[5], tem[5];
memset(ans, 0x3f, sizeof(ans));
memset(tem, 0x3f, sizeof(ans));
tem[u] = 0;
while (t){
int diff = t & -t;
int lg = __lg(diff);
for (int p = 0; p < 5; ++p){
for (int q = 0; q < 5; ++q){
ans[p] = min(ans[p], tem[q] + dp[s][lg][q][p]);
}
}
for (int p = 0; p < 5; ++p){
tem[p] = ans[p];
ans[p] = inf;
}
s = s + diff;
t ^= diff;
}
cout << ((tem[v] - inf) ? tem[v] : -1) << "\n";
}
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
176452 KB |
Output is correct |
2 |
Correct |
64 ms |
176360 KB |
Output is correct |
3 |
Correct |
63 ms |
176376 KB |
Output is correct |
4 |
Correct |
65 ms |
176412 KB |
Output is correct |
5 |
Correct |
67 ms |
176360 KB |
Output is correct |
6 |
Correct |
66 ms |
176400 KB |
Output is correct |
7 |
Correct |
79 ms |
176344 KB |
Output is correct |
8 |
Correct |
117 ms |
177304 KB |
Output is correct |
9 |
Correct |
110 ms |
177308 KB |
Output is correct |
10 |
Correct |
92 ms |
176484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
176436 KB |
Output is correct |
2 |
Correct |
72 ms |
176332 KB |
Output is correct |
3 |
Correct |
67 ms |
176428 KB |
Output is correct |
4 |
Correct |
71 ms |
176400 KB |
Output is correct |
5 |
Correct |
66 ms |
176340 KB |
Output is correct |
6 |
Correct |
68 ms |
176384 KB |
Output is correct |
7 |
Correct |
86 ms |
176540 KB |
Output is correct |
8 |
Correct |
72 ms |
176512 KB |
Output is correct |
9 |
Correct |
104 ms |
177312 KB |
Output is correct |
10 |
Correct |
149 ms |
178664 KB |
Output is correct |
11 |
Correct |
139 ms |
178116 KB |
Output is correct |
12 |
Correct |
153 ms |
177640 KB |
Output is correct |
13 |
Correct |
207 ms |
178820 KB |
Output is correct |
14 |
Correct |
113 ms |
177824 KB |
Output is correct |
15 |
Correct |
116 ms |
177556 KB |
Output is correct |
16 |
Correct |
134 ms |
177572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
176372 KB |
Output is correct |
2 |
Correct |
77 ms |
176328 KB |
Output is correct |
3 |
Correct |
73 ms |
176372 KB |
Output is correct |
4 |
Correct |
74 ms |
176400 KB |
Output is correct |
5 |
Correct |
74 ms |
176400 KB |
Output is correct |
6 |
Correct |
90 ms |
176424 KB |
Output is correct |
7 |
Correct |
73 ms |
176428 KB |
Output is correct |
8 |
Correct |
76 ms |
176440 KB |
Output is correct |
9 |
Correct |
69 ms |
176372 KB |
Output is correct |
10 |
Correct |
102 ms |
176328 KB |
Output is correct |
11 |
Correct |
128 ms |
177968 KB |
Output is correct |
12 |
Correct |
155 ms |
178592 KB |
Output is correct |
13 |
Correct |
167 ms |
178732 KB |
Output is correct |
14 |
Correct |
145 ms |
178208 KB |
Output is correct |
15 |
Correct |
120 ms |
177520 KB |
Output is correct |
16 |
Correct |
110 ms |
177548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
176372 KB |
Output is correct |
2 |
Correct |
77 ms |
176328 KB |
Output is correct |
3 |
Correct |
73 ms |
176372 KB |
Output is correct |
4 |
Correct |
74 ms |
176400 KB |
Output is correct |
5 |
Correct |
74 ms |
176400 KB |
Output is correct |
6 |
Correct |
90 ms |
176424 KB |
Output is correct |
7 |
Correct |
73 ms |
176428 KB |
Output is correct |
8 |
Correct |
76 ms |
176440 KB |
Output is correct |
9 |
Correct |
69 ms |
176372 KB |
Output is correct |
10 |
Correct |
102 ms |
176328 KB |
Output is correct |
11 |
Correct |
128 ms |
177968 KB |
Output is correct |
12 |
Correct |
155 ms |
178592 KB |
Output is correct |
13 |
Correct |
167 ms |
178732 KB |
Output is correct |
14 |
Correct |
145 ms |
178208 KB |
Output is correct |
15 |
Correct |
120 ms |
177520 KB |
Output is correct |
16 |
Correct |
110 ms |
177548 KB |
Output is correct |
17 |
Correct |
148 ms |
177960 KB |
Output is correct |
18 |
Correct |
83 ms |
176380 KB |
Output is correct |
19 |
Correct |
73 ms |
176328 KB |
Output is correct |
20 |
Correct |
72 ms |
176388 KB |
Output is correct |
21 |
Correct |
82 ms |
176400 KB |
Output is correct |
22 |
Correct |
69 ms |
176364 KB |
Output is correct |
23 |
Correct |
75 ms |
176432 KB |
Output is correct |
24 |
Correct |
78 ms |
176492 KB |
Output is correct |
25 |
Correct |
86 ms |
176508 KB |
Output is correct |
26 |
Correct |
85 ms |
176444 KB |
Output is correct |
27 |
Correct |
120 ms |
177276 KB |
Output is correct |
28 |
Correct |
166 ms |
178596 KB |
Output is correct |
29 |
Correct |
156 ms |
178808 KB |
Output is correct |
30 |
Correct |
156 ms |
178208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
176452 KB |
Output is correct |
2 |
Correct |
64 ms |
176360 KB |
Output is correct |
3 |
Correct |
63 ms |
176376 KB |
Output is correct |
4 |
Correct |
65 ms |
176412 KB |
Output is correct |
5 |
Correct |
67 ms |
176360 KB |
Output is correct |
6 |
Correct |
66 ms |
176400 KB |
Output is correct |
7 |
Correct |
79 ms |
176344 KB |
Output is correct |
8 |
Correct |
117 ms |
177304 KB |
Output is correct |
9 |
Correct |
110 ms |
177308 KB |
Output is correct |
10 |
Correct |
92 ms |
176484 KB |
Output is correct |
11 |
Correct |
131 ms |
176436 KB |
Output is correct |
12 |
Correct |
72 ms |
176332 KB |
Output is correct |
13 |
Correct |
67 ms |
176428 KB |
Output is correct |
14 |
Correct |
71 ms |
176400 KB |
Output is correct |
15 |
Correct |
66 ms |
176340 KB |
Output is correct |
16 |
Correct |
68 ms |
176384 KB |
Output is correct |
17 |
Correct |
86 ms |
176540 KB |
Output is correct |
18 |
Correct |
72 ms |
176512 KB |
Output is correct |
19 |
Correct |
104 ms |
177312 KB |
Output is correct |
20 |
Correct |
149 ms |
178664 KB |
Output is correct |
21 |
Correct |
139 ms |
178116 KB |
Output is correct |
22 |
Correct |
153 ms |
177640 KB |
Output is correct |
23 |
Correct |
207 ms |
178820 KB |
Output is correct |
24 |
Correct |
113 ms |
177824 KB |
Output is correct |
25 |
Correct |
116 ms |
177556 KB |
Output is correct |
26 |
Correct |
134 ms |
177572 KB |
Output is correct |
27 |
Correct |
72 ms |
176372 KB |
Output is correct |
28 |
Correct |
77 ms |
176328 KB |
Output is correct |
29 |
Correct |
73 ms |
176372 KB |
Output is correct |
30 |
Correct |
74 ms |
176400 KB |
Output is correct |
31 |
Correct |
74 ms |
176400 KB |
Output is correct |
32 |
Correct |
90 ms |
176424 KB |
Output is correct |
33 |
Correct |
73 ms |
176428 KB |
Output is correct |
34 |
Correct |
76 ms |
176440 KB |
Output is correct |
35 |
Correct |
69 ms |
176372 KB |
Output is correct |
36 |
Correct |
102 ms |
176328 KB |
Output is correct |
37 |
Correct |
128 ms |
177968 KB |
Output is correct |
38 |
Correct |
155 ms |
178592 KB |
Output is correct |
39 |
Correct |
167 ms |
178732 KB |
Output is correct |
40 |
Correct |
145 ms |
178208 KB |
Output is correct |
41 |
Correct |
120 ms |
177520 KB |
Output is correct |
42 |
Correct |
110 ms |
177548 KB |
Output is correct |
43 |
Correct |
148 ms |
177960 KB |
Output is correct |
44 |
Correct |
83 ms |
176380 KB |
Output is correct |
45 |
Correct |
73 ms |
176328 KB |
Output is correct |
46 |
Correct |
72 ms |
176388 KB |
Output is correct |
47 |
Correct |
82 ms |
176400 KB |
Output is correct |
48 |
Correct |
69 ms |
176364 KB |
Output is correct |
49 |
Correct |
75 ms |
176432 KB |
Output is correct |
50 |
Correct |
78 ms |
176492 KB |
Output is correct |
51 |
Correct |
86 ms |
176508 KB |
Output is correct |
52 |
Correct |
85 ms |
176444 KB |
Output is correct |
53 |
Correct |
120 ms |
177276 KB |
Output is correct |
54 |
Correct |
166 ms |
178596 KB |
Output is correct |
55 |
Correct |
156 ms |
178808 KB |
Output is correct |
56 |
Correct |
156 ms |
178208 KB |
Output is correct |
57 |
Correct |
206 ms |
179044 KB |
Output is correct |
58 |
Correct |
127 ms |
177284 KB |
Output is correct |
59 |
Correct |
145 ms |
178156 KB |
Output is correct |