#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define N 50005
#define INF 0x3f3f3f3f
#define MOD 1000000007LL
#define LEN 240
int fl[250][240][240];
vector<pair<pair<int, int>, int> > tr[250], buck[250];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int k, n, m, o;
cin>>k>>n>>m>>o;
memset(fl, 0x3f, sizeof fl);
for(int i = 0; i < 210; ++i)
for(int j = 0; j < 240; ++j)
fl[i][j][j] = 0;
while(m--){
int a, b, t;
cin>>a>>b>>t;
if(a / LEN != b / LEN){
tr[a / LEN].push_back({{a % k, b % k}, t});
} else {
buck[a / LEN].push_back({{a % LEN, b % LEN}, t});
}
}
for(int i = 0; i < 210; ++i){
sort(buck[i].begin(), buck[i].end(), [](const pair<pair<int, int>, int>& lhs, const pair<pair<int, int>, int>& rhs){
return lhs.ff.ss < rhs.ff.ss;
});
for(auto& x : buck[i]){
for(int j = 0; j <= x.ff.ff; ++j){
fl[i][j][x.ff.ss] = min(fl[i][j][x.ff.ss], fl[i][j][x.ff.ff] + x.ss);
}
}
}
while(o--){
int a, b;
cin>>a>>b;
int ans = INF;
if(a / LEN == b / LEN){
ans = fl[a / LEN][a % LEN][b % LEN];
} else {
vector<int> tmp(k);
for(int i = 0; i < k; ++i){
tmp[i] = fl[a / LEN][a % LEN][LEN - k + i];
}
vector<int> tmp3(k, INF);
for(auto& x : tr[a / LEN])
tmp3[x.ff.ss] = min(tmp3[x.ff.ss], tmp[x.ff.ff] + x.ss);
swap(tmp, tmp3);
for(int i = a / LEN + 1; i < b / LEN; ++i){
vector<int> tmp2(k, INF);
for(int j = 0; j < k; ++j){
for(int l = 0; l < k; ++l){
tmp2[l] = min(tmp2[l], tmp[j] + fl[i][j][LEN - k + l]);
}
}
swap(tmp, tmp2);
vector<int> tmp4(k, INF);
for(auto& x : tr[i])
tmp4[x.ff.ss] = min(tmp4[x.ff.ss], tmp[x.ff.ff] + x.ss);
swap(tmp, tmp4);
}
for(int i = 0; i < k; i++){
ans = min(ans, tmp[i] + fl[b / LEN][i][b % LEN]);
}
}
cout<<(ans == INF ? -1 : ans)<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
58488 KB |
Output is correct |
2 |
Correct |
51 ms |
56696 KB |
Output is correct |
3 |
Correct |
50 ms |
56824 KB |
Output is correct |
4 |
Correct |
53 ms |
56700 KB |
Output is correct |
5 |
Correct |
52 ms |
56824 KB |
Output is correct |
6 |
Correct |
52 ms |
56952 KB |
Output is correct |
7 |
Correct |
53 ms |
56824 KB |
Output is correct |
8 |
Correct |
168 ms |
58492 KB |
Output is correct |
9 |
Correct |
161 ms |
58360 KB |
Output is correct |
10 |
Correct |
137 ms |
56844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
254 ms |
59844 KB |
Output is correct |
2 |
Correct |
55 ms |
56700 KB |
Output is correct |
3 |
Correct |
51 ms |
56796 KB |
Output is correct |
4 |
Correct |
52 ms |
56664 KB |
Output is correct |
5 |
Correct |
51 ms |
56696 KB |
Output is correct |
6 |
Correct |
51 ms |
56696 KB |
Output is correct |
7 |
Correct |
57 ms |
56828 KB |
Output is correct |
8 |
Correct |
58 ms |
56876 KB |
Output is correct |
9 |
Correct |
194 ms |
58360 KB |
Output is correct |
10 |
Correct |
329 ms |
61472 KB |
Output is correct |
11 |
Correct |
248 ms |
59956 KB |
Output is correct |
12 |
Correct |
274 ms |
59384 KB |
Output is correct |
13 |
Correct |
253 ms |
61560 KB |
Output is correct |
14 |
Correct |
157 ms |
59768 KB |
Output is correct |
15 |
Correct |
159 ms |
59740 KB |
Output is correct |
16 |
Correct |
156 ms |
59712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
56592 KB |
Output is correct |
2 |
Correct |
56 ms |
56696 KB |
Output is correct |
3 |
Correct |
57 ms |
56696 KB |
Output is correct |
4 |
Correct |
51 ms |
56824 KB |
Output is correct |
5 |
Correct |
50 ms |
56696 KB |
Output is correct |
6 |
Correct |
52 ms |
56796 KB |
Output is correct |
7 |
Correct |
53 ms |
56696 KB |
Output is correct |
8 |
Correct |
55 ms |
56952 KB |
Output is correct |
9 |
Correct |
53 ms |
56952 KB |
Output is correct |
10 |
Correct |
100 ms |
58232 KB |
Output is correct |
11 |
Correct |
138 ms |
59512 KB |
Output is correct |
12 |
Correct |
179 ms |
61276 KB |
Output is correct |
13 |
Correct |
188 ms |
61364 KB |
Output is correct |
14 |
Correct |
155 ms |
61240 KB |
Output is correct |
15 |
Correct |
122 ms |
59512 KB |
Output is correct |
16 |
Correct |
121 ms |
59588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
56592 KB |
Output is correct |
2 |
Correct |
56 ms |
56696 KB |
Output is correct |
3 |
Correct |
57 ms |
56696 KB |
Output is correct |
4 |
Correct |
51 ms |
56824 KB |
Output is correct |
5 |
Correct |
50 ms |
56696 KB |
Output is correct |
6 |
Correct |
52 ms |
56796 KB |
Output is correct |
7 |
Correct |
53 ms |
56696 KB |
Output is correct |
8 |
Correct |
55 ms |
56952 KB |
Output is correct |
9 |
Correct |
53 ms |
56952 KB |
Output is correct |
10 |
Correct |
100 ms |
58232 KB |
Output is correct |
11 |
Correct |
138 ms |
59512 KB |
Output is correct |
12 |
Correct |
179 ms |
61276 KB |
Output is correct |
13 |
Correct |
188 ms |
61364 KB |
Output is correct |
14 |
Correct |
155 ms |
61240 KB |
Output is correct |
15 |
Correct |
122 ms |
59512 KB |
Output is correct |
16 |
Correct |
121 ms |
59588 KB |
Output is correct |
17 |
Correct |
163 ms |
59828 KB |
Output is correct |
18 |
Correct |
51 ms |
56696 KB |
Output is correct |
19 |
Correct |
51 ms |
56696 KB |
Output is correct |
20 |
Correct |
58 ms |
56700 KB |
Output is correct |
21 |
Correct |
51 ms |
56740 KB |
Output is correct |
22 |
Correct |
61 ms |
56848 KB |
Output is correct |
23 |
Correct |
53 ms |
56696 KB |
Output is correct |
24 |
Correct |
54 ms |
56824 KB |
Output is correct |
25 |
Correct |
56 ms |
56824 KB |
Output is correct |
26 |
Correct |
55 ms |
56824 KB |
Output is correct |
27 |
Correct |
119 ms |
58232 KB |
Output is correct |
28 |
Correct |
203 ms |
61344 KB |
Output is correct |
29 |
Correct |
221 ms |
61432 KB |
Output is correct |
30 |
Correct |
182 ms |
61304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
58488 KB |
Output is correct |
2 |
Correct |
51 ms |
56696 KB |
Output is correct |
3 |
Correct |
50 ms |
56824 KB |
Output is correct |
4 |
Correct |
53 ms |
56700 KB |
Output is correct |
5 |
Correct |
52 ms |
56824 KB |
Output is correct |
6 |
Correct |
52 ms |
56952 KB |
Output is correct |
7 |
Correct |
53 ms |
56824 KB |
Output is correct |
8 |
Correct |
168 ms |
58492 KB |
Output is correct |
9 |
Correct |
161 ms |
58360 KB |
Output is correct |
10 |
Correct |
137 ms |
56844 KB |
Output is correct |
11 |
Correct |
254 ms |
59844 KB |
Output is correct |
12 |
Correct |
55 ms |
56700 KB |
Output is correct |
13 |
Correct |
51 ms |
56796 KB |
Output is correct |
14 |
Correct |
52 ms |
56664 KB |
Output is correct |
15 |
Correct |
51 ms |
56696 KB |
Output is correct |
16 |
Correct |
51 ms |
56696 KB |
Output is correct |
17 |
Correct |
57 ms |
56828 KB |
Output is correct |
18 |
Correct |
58 ms |
56876 KB |
Output is correct |
19 |
Correct |
194 ms |
58360 KB |
Output is correct |
20 |
Correct |
329 ms |
61472 KB |
Output is correct |
21 |
Correct |
248 ms |
59956 KB |
Output is correct |
22 |
Correct |
274 ms |
59384 KB |
Output is correct |
23 |
Correct |
253 ms |
61560 KB |
Output is correct |
24 |
Correct |
157 ms |
59768 KB |
Output is correct |
25 |
Correct |
159 ms |
59740 KB |
Output is correct |
26 |
Correct |
156 ms |
59712 KB |
Output is correct |
27 |
Correct |
56 ms |
56592 KB |
Output is correct |
28 |
Correct |
56 ms |
56696 KB |
Output is correct |
29 |
Correct |
57 ms |
56696 KB |
Output is correct |
30 |
Correct |
51 ms |
56824 KB |
Output is correct |
31 |
Correct |
50 ms |
56696 KB |
Output is correct |
32 |
Correct |
52 ms |
56796 KB |
Output is correct |
33 |
Correct |
53 ms |
56696 KB |
Output is correct |
34 |
Correct |
55 ms |
56952 KB |
Output is correct |
35 |
Correct |
53 ms |
56952 KB |
Output is correct |
36 |
Correct |
100 ms |
58232 KB |
Output is correct |
37 |
Correct |
138 ms |
59512 KB |
Output is correct |
38 |
Correct |
179 ms |
61276 KB |
Output is correct |
39 |
Correct |
188 ms |
61364 KB |
Output is correct |
40 |
Correct |
155 ms |
61240 KB |
Output is correct |
41 |
Correct |
122 ms |
59512 KB |
Output is correct |
42 |
Correct |
121 ms |
59588 KB |
Output is correct |
43 |
Correct |
163 ms |
59828 KB |
Output is correct |
44 |
Correct |
51 ms |
56696 KB |
Output is correct |
45 |
Correct |
51 ms |
56696 KB |
Output is correct |
46 |
Correct |
58 ms |
56700 KB |
Output is correct |
47 |
Correct |
51 ms |
56740 KB |
Output is correct |
48 |
Correct |
61 ms |
56848 KB |
Output is correct |
49 |
Correct |
53 ms |
56696 KB |
Output is correct |
50 |
Correct |
54 ms |
56824 KB |
Output is correct |
51 |
Correct |
56 ms |
56824 KB |
Output is correct |
52 |
Correct |
55 ms |
56824 KB |
Output is correct |
53 |
Correct |
119 ms |
58232 KB |
Output is correct |
54 |
Correct |
203 ms |
61344 KB |
Output is correct |
55 |
Correct |
221 ms |
61432 KB |
Output is correct |
56 |
Correct |
182 ms |
61304 KB |
Output is correct |
57 |
Correct |
368 ms |
63056 KB |
Output is correct |
58 |
Correct |
163 ms |
58360 KB |
Output is correct |
59 |
Correct |
216 ms |
59992 KB |
Output is correct |