#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=5e4+5;
int dp[mxn][20][5][5];
int jump[mxn][20][5][5];
int ans[5][5];
int tmp[5][5];
int k,n,m,q;
void ckmin(int a[5][5],int b[5][5],int c[5][5]){
for(int i=0;i<k;i++){
for(int j=0;j<k;j++){
for(int z=0;z<k;z++){
a[i][z]=min(a[i][z],b[i][j]+c[j][z]);
}
}
}
}
int main() {_
//setIO("wayne");
cin>>k>>n>>m>>q;
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 j = 1; j < 17; j++) {
for (int i = 0; i + (1 << j) < (n + k - 1) / k; i++) {
ckmin(dp[i][j], dp[i][j - 1], dp[i + (1 << j - 1)][j - 1]);
}
}
while (q--) {
int a, b;
cin >> a >> b;
memset(ans, 0x3f, sizeof ans);
for (int i = 0; i < 5; i++) ans[i][i] = 0;
for (int curr = a / k, dest = b / k, i = 16; ~i; i--) {
if (curr + (1 << i) <= dest) {
memset(tmp, 0x3f, sizeof tmp);
ckmin(tmp, ans, dp[curr][i]);
memcpy(ans, tmp, sizeof ans);
curr += (1 << i);
}
}
cout << (ans[a % k][b % k] == 0x3f3f3f3f ? -1 : ans[a % k][b % k]) << '\n';
}
return 0;
}
//maybe its multiset not set
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:42:49: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
42 | ckmin(dp[i][j], dp[i][j - 1], dp[i + (1 << j - 1)][j - 1]);
| ~~^~~
toll.cpp: In function 'void setIO(std::string)':
toll.cpp:10:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:11:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
99060 KB |
Output is correct |
2 |
Correct |
35 ms |
98132 KB |
Output is correct |
3 |
Correct |
36 ms |
98076 KB |
Output is correct |
4 |
Correct |
39 ms |
98132 KB |
Output is correct |
5 |
Correct |
38 ms |
98144 KB |
Output is correct |
6 |
Correct |
36 ms |
98124 KB |
Output is correct |
7 |
Correct |
37 ms |
98124 KB |
Output is correct |
8 |
Correct |
71 ms |
99064 KB |
Output is correct |
9 |
Correct |
71 ms |
99012 KB |
Output is correct |
10 |
Correct |
56 ms |
98312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
99948 KB |
Output is correct |
2 |
Correct |
35 ms |
98124 KB |
Output is correct |
3 |
Correct |
36 ms |
98100 KB |
Output is correct |
4 |
Correct |
36 ms |
98128 KB |
Output is correct |
5 |
Correct |
35 ms |
98124 KB |
Output is correct |
6 |
Correct |
36 ms |
98176 KB |
Output is correct |
7 |
Correct |
40 ms |
98276 KB |
Output is correct |
8 |
Correct |
39 ms |
98256 KB |
Output is correct |
9 |
Correct |
74 ms |
99020 KB |
Output is correct |
10 |
Correct |
97 ms |
100500 KB |
Output is correct |
11 |
Correct |
84 ms |
99944 KB |
Output is correct |
12 |
Correct |
83 ms |
99384 KB |
Output is correct |
13 |
Correct |
84 ms |
100572 KB |
Output is correct |
14 |
Correct |
79 ms |
99624 KB |
Output is correct |
15 |
Correct |
68 ms |
99304 KB |
Output is correct |
16 |
Correct |
69 ms |
99400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
98160 KB |
Output is correct |
2 |
Correct |
36 ms |
98136 KB |
Output is correct |
3 |
Correct |
35 ms |
98040 KB |
Output is correct |
4 |
Correct |
43 ms |
98164 KB |
Output is correct |
5 |
Correct |
39 ms |
98068 KB |
Output is correct |
6 |
Correct |
36 ms |
98184 KB |
Output is correct |
7 |
Correct |
37 ms |
98116 KB |
Output is correct |
8 |
Correct |
39 ms |
98120 KB |
Output is correct |
9 |
Correct |
37 ms |
98124 KB |
Output is correct |
10 |
Correct |
65 ms |
98892 KB |
Output is correct |
11 |
Correct |
80 ms |
99688 KB |
Output is correct |
12 |
Correct |
101 ms |
100420 KB |
Output is correct |
13 |
Correct |
94 ms |
100452 KB |
Output is correct |
14 |
Correct |
87 ms |
99944 KB |
Output is correct |
15 |
Correct |
63 ms |
99276 KB |
Output is correct |
16 |
Correct |
64 ms |
99264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
98160 KB |
Output is correct |
2 |
Correct |
36 ms |
98136 KB |
Output is correct |
3 |
Correct |
35 ms |
98040 KB |
Output is correct |
4 |
Correct |
43 ms |
98164 KB |
Output is correct |
5 |
Correct |
39 ms |
98068 KB |
Output is correct |
6 |
Correct |
36 ms |
98184 KB |
Output is correct |
7 |
Correct |
37 ms |
98116 KB |
Output is correct |
8 |
Correct |
39 ms |
98120 KB |
Output is correct |
9 |
Correct |
37 ms |
98124 KB |
Output is correct |
10 |
Correct |
65 ms |
98892 KB |
Output is correct |
11 |
Correct |
80 ms |
99688 KB |
Output is correct |
12 |
Correct |
101 ms |
100420 KB |
Output is correct |
13 |
Correct |
94 ms |
100452 KB |
Output is correct |
14 |
Correct |
87 ms |
99944 KB |
Output is correct |
15 |
Correct |
63 ms |
99276 KB |
Output is correct |
16 |
Correct |
64 ms |
99264 KB |
Output is correct |
17 |
Correct |
91 ms |
99828 KB |
Output is correct |
18 |
Correct |
36 ms |
98096 KB |
Output is correct |
19 |
Correct |
37 ms |
98136 KB |
Output is correct |
20 |
Correct |
37 ms |
98072 KB |
Output is correct |
21 |
Correct |
37 ms |
98116 KB |
Output is correct |
22 |
Correct |
37 ms |
98164 KB |
Output is correct |
23 |
Correct |
38 ms |
98120 KB |
Output is correct |
24 |
Correct |
38 ms |
98144 KB |
Output is correct |
25 |
Correct |
39 ms |
98168 KB |
Output is correct |
26 |
Correct |
39 ms |
98212 KB |
Output is correct |
27 |
Correct |
73 ms |
99016 KB |
Output is correct |
28 |
Correct |
94 ms |
100324 KB |
Output is correct |
29 |
Correct |
97 ms |
100636 KB |
Output is correct |
30 |
Correct |
90 ms |
100044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
99060 KB |
Output is correct |
2 |
Correct |
35 ms |
98132 KB |
Output is correct |
3 |
Correct |
36 ms |
98076 KB |
Output is correct |
4 |
Correct |
39 ms |
98132 KB |
Output is correct |
5 |
Correct |
38 ms |
98144 KB |
Output is correct |
6 |
Correct |
36 ms |
98124 KB |
Output is correct |
7 |
Correct |
37 ms |
98124 KB |
Output is correct |
8 |
Correct |
71 ms |
99064 KB |
Output is correct |
9 |
Correct |
71 ms |
99012 KB |
Output is correct |
10 |
Correct |
56 ms |
98312 KB |
Output is correct |
11 |
Correct |
93 ms |
99948 KB |
Output is correct |
12 |
Correct |
35 ms |
98124 KB |
Output is correct |
13 |
Correct |
36 ms |
98100 KB |
Output is correct |
14 |
Correct |
36 ms |
98128 KB |
Output is correct |
15 |
Correct |
35 ms |
98124 KB |
Output is correct |
16 |
Correct |
36 ms |
98176 KB |
Output is correct |
17 |
Correct |
40 ms |
98276 KB |
Output is correct |
18 |
Correct |
39 ms |
98256 KB |
Output is correct |
19 |
Correct |
74 ms |
99020 KB |
Output is correct |
20 |
Correct |
97 ms |
100500 KB |
Output is correct |
21 |
Correct |
84 ms |
99944 KB |
Output is correct |
22 |
Correct |
83 ms |
99384 KB |
Output is correct |
23 |
Correct |
84 ms |
100572 KB |
Output is correct |
24 |
Correct |
79 ms |
99624 KB |
Output is correct |
25 |
Correct |
68 ms |
99304 KB |
Output is correct |
26 |
Correct |
69 ms |
99400 KB |
Output is correct |
27 |
Correct |
36 ms |
98160 KB |
Output is correct |
28 |
Correct |
36 ms |
98136 KB |
Output is correct |
29 |
Correct |
35 ms |
98040 KB |
Output is correct |
30 |
Correct |
43 ms |
98164 KB |
Output is correct |
31 |
Correct |
39 ms |
98068 KB |
Output is correct |
32 |
Correct |
36 ms |
98184 KB |
Output is correct |
33 |
Correct |
37 ms |
98116 KB |
Output is correct |
34 |
Correct |
39 ms |
98120 KB |
Output is correct |
35 |
Correct |
37 ms |
98124 KB |
Output is correct |
36 |
Correct |
65 ms |
98892 KB |
Output is correct |
37 |
Correct |
80 ms |
99688 KB |
Output is correct |
38 |
Correct |
101 ms |
100420 KB |
Output is correct |
39 |
Correct |
94 ms |
100452 KB |
Output is correct |
40 |
Correct |
87 ms |
99944 KB |
Output is correct |
41 |
Correct |
63 ms |
99276 KB |
Output is correct |
42 |
Correct |
64 ms |
99264 KB |
Output is correct |
43 |
Correct |
91 ms |
99828 KB |
Output is correct |
44 |
Correct |
36 ms |
98096 KB |
Output is correct |
45 |
Correct |
37 ms |
98136 KB |
Output is correct |
46 |
Correct |
37 ms |
98072 KB |
Output is correct |
47 |
Correct |
37 ms |
98116 KB |
Output is correct |
48 |
Correct |
37 ms |
98164 KB |
Output is correct |
49 |
Correct |
38 ms |
98120 KB |
Output is correct |
50 |
Correct |
38 ms |
98144 KB |
Output is correct |
51 |
Correct |
39 ms |
98168 KB |
Output is correct |
52 |
Correct |
39 ms |
98212 KB |
Output is correct |
53 |
Correct |
73 ms |
99016 KB |
Output is correct |
54 |
Correct |
94 ms |
100324 KB |
Output is correct |
55 |
Correct |
97 ms |
100636 KB |
Output is correct |
56 |
Correct |
90 ms |
100044 KB |
Output is correct |
57 |
Correct |
127 ms |
101520 KB |
Output is correct |
58 |
Correct |
74 ms |
99092 KB |
Output is correct |
59 |
Correct |
91 ms |
99916 KB |
Output is correct |