#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int nmax = 5e4 + 1;
int dist[nmax][41][5];
int k;
int get(int a, int b){
int A = a / k;
int B = b / k;
if(A >= B){
return -1;
}
vector <pii> vec;
vec.pb({a, 0});
while(B > A + 40){
vector <pii> nw;
int d[5];
fill(d, d + 5, 1e9 + 1);
for(auto x : vec){
int v = x.f, D = x.s;
for(int j = 0; j < k; j++){
d[j] = min(d[j], dist[v][40][j] + D);
}
}
A += 40;
vec.clear();
for(int j = 0; j < k; j++){
if(d[j] != 1e9 + 1)
vec.pb({A * k + j, d[j]});
}
}
int ans = 1e9 + 1;
for(auto x : vec){
int v = x.f, D = x.s;
ans = min(ans, dist[v][B - A][b % k] + D);
}
if(ans == 1e9 + 1) return -1;
return ans;
}
int main(){
int n, m, o; cin >> k >> n >> m >> o;
vector <vector <pii> > g(n + 1);
for(int i = 1; i <= m; i++){
int u, v; cin >> u >> v;
int t; cin >> t;
g[u].pb({v, t});
}
for(int i = n - 1; i >= 0; i--){
for(int j = 0; j <= 40; j++){
for(int x = 0; x < k; x++)
dist[i][j][x] = 1e9 + 1;
}
for(auto ed : g[i]){
int to = ed.f, x = ed.s;
dist[i][1][to % k] = x;
}
for(int j = 2; j <= 40; j++){
for(int x = 0; x < k; x++){
for(auto ed : g[i]){
int to = ed.f;
dist[i][j][x] = min(dist[i][j][x], dist[to][j - 1][x] + dist[i][1][to % k]);
}
}
}
}
while(o--){
int a, b; cin >> a >> b;
cout << get(a, b) << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
337 ms |
44140 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
4 ms |
1084 KB |
Output is correct |
6 |
Correct |
3 ms |
1092 KB |
Output is correct |
7 |
Correct |
5 ms |
1108 KB |
Output is correct |
8 |
Correct |
123 ms |
44076 KB |
Output is correct |
9 |
Correct |
110 ms |
43980 KB |
Output is correct |
10 |
Correct |
58 ms |
41640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
250 ms |
45892 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
22 ms |
1216 KB |
Output is correct |
8 |
Correct |
27 ms |
1292 KB |
Output is correct |
9 |
Correct |
119 ms |
44064 KB |
Output is correct |
10 |
Correct |
336 ms |
47796 KB |
Output is correct |
11 |
Correct |
279 ms |
45976 KB |
Output is correct |
12 |
Correct |
192 ms |
45004 KB |
Output is correct |
13 |
Correct |
337 ms |
32280 KB |
Output is correct |
14 |
Correct |
176 ms |
29132 KB |
Output is correct |
15 |
Correct |
189 ms |
28360 KB |
Output is correct |
16 |
Correct |
188 ms |
28232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
1108 KB |
Output is correct |
7 |
Correct |
3 ms |
1236 KB |
Output is correct |
8 |
Correct |
10 ms |
1348 KB |
Output is correct |
9 |
Correct |
6 ms |
1168 KB |
Output is correct |
10 |
Correct |
81 ms |
43972 KB |
Output is correct |
11 |
Correct |
154 ms |
45688 KB |
Output is correct |
12 |
Correct |
247 ms |
47564 KB |
Output is correct |
13 |
Correct |
268 ms |
48376 KB |
Output is correct |
14 |
Correct |
234 ms |
46540 KB |
Output is correct |
15 |
Correct |
167 ms |
28248 KB |
Output is correct |
16 |
Correct |
164 ms |
28280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
1108 KB |
Output is correct |
7 |
Correct |
3 ms |
1236 KB |
Output is correct |
8 |
Correct |
10 ms |
1348 KB |
Output is correct |
9 |
Correct |
6 ms |
1168 KB |
Output is correct |
10 |
Correct |
81 ms |
43972 KB |
Output is correct |
11 |
Correct |
154 ms |
45688 KB |
Output is correct |
12 |
Correct |
247 ms |
47564 KB |
Output is correct |
13 |
Correct |
268 ms |
48376 KB |
Output is correct |
14 |
Correct |
234 ms |
46540 KB |
Output is correct |
15 |
Correct |
167 ms |
28248 KB |
Output is correct |
16 |
Correct |
164 ms |
28280 KB |
Output is correct |
17 |
Correct |
187 ms |
45660 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
7 ms |
1160 KB |
Output is correct |
24 |
Correct |
8 ms |
1236 KB |
Output is correct |
25 |
Correct |
13 ms |
1364 KB |
Output is correct |
26 |
Correct |
11 ms |
1312 KB |
Output is correct |
27 |
Correct |
91 ms |
43996 KB |
Output is correct |
28 |
Correct |
282 ms |
47632 KB |
Output is correct |
29 |
Correct |
300 ms |
48372 KB |
Output is correct |
30 |
Correct |
225 ms |
46540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
337 ms |
44140 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
4 ms |
1084 KB |
Output is correct |
6 |
Correct |
3 ms |
1092 KB |
Output is correct |
7 |
Correct |
5 ms |
1108 KB |
Output is correct |
8 |
Correct |
123 ms |
44076 KB |
Output is correct |
9 |
Correct |
110 ms |
43980 KB |
Output is correct |
10 |
Correct |
58 ms |
41640 KB |
Output is correct |
11 |
Correct |
250 ms |
45892 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
22 ms |
1216 KB |
Output is correct |
18 |
Correct |
27 ms |
1292 KB |
Output is correct |
19 |
Correct |
119 ms |
44064 KB |
Output is correct |
20 |
Correct |
336 ms |
47796 KB |
Output is correct |
21 |
Correct |
279 ms |
45976 KB |
Output is correct |
22 |
Correct |
192 ms |
45004 KB |
Output is correct |
23 |
Correct |
337 ms |
32280 KB |
Output is correct |
24 |
Correct |
176 ms |
29132 KB |
Output is correct |
25 |
Correct |
189 ms |
28360 KB |
Output is correct |
26 |
Correct |
188 ms |
28232 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
312 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
3 ms |
1236 KB |
Output is correct |
34 |
Correct |
10 ms |
1348 KB |
Output is correct |
35 |
Correct |
6 ms |
1168 KB |
Output is correct |
36 |
Correct |
81 ms |
43972 KB |
Output is correct |
37 |
Correct |
154 ms |
45688 KB |
Output is correct |
38 |
Correct |
247 ms |
47564 KB |
Output is correct |
39 |
Correct |
268 ms |
48376 KB |
Output is correct |
40 |
Correct |
234 ms |
46540 KB |
Output is correct |
41 |
Correct |
167 ms |
28248 KB |
Output is correct |
42 |
Correct |
164 ms |
28280 KB |
Output is correct |
43 |
Correct |
187 ms |
45660 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
312 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
7 ms |
1160 KB |
Output is correct |
50 |
Correct |
8 ms |
1236 KB |
Output is correct |
51 |
Correct |
13 ms |
1364 KB |
Output is correct |
52 |
Correct |
11 ms |
1312 KB |
Output is correct |
53 |
Correct |
91 ms |
43996 KB |
Output is correct |
54 |
Correct |
282 ms |
47632 KB |
Output is correct |
55 |
Correct |
300 ms |
48372 KB |
Output is correct |
56 |
Correct |
225 ms |
46540 KB |
Output is correct |
57 |
Correct |
491 ms |
49176 KB |
Output is correct |
58 |
Correct |
123 ms |
44092 KB |
Output is correct |
59 |
Correct |
313 ms |
45992 KB |
Output is correct |