// #pragma gcc optimize("Ofast")
// #pragma GCC optimization("Ofast")
// #pragma optimize(Ofast)
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
/**
* author: tpa1410 - futorankusu
* rose, hanni, minji
**/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> pii;
#define rose() ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gcd __gcd
#define getl(s) getline(cin, s);
#define setpre(x) fixed << setprecision(x)
#define mset(a) memset(a, 0, sizeof(a))
#define endl '\n'
const ll hanni = 1e4, minji = 1e9;
struct Matrix{
long long int a[5][5];
Matrix(){
for(int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
a[i][j] = minji;
}
}
}
};
Matrix mul(Matrix a, Matrix b){
Matrix c;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
c.a[i][j] = minji;
for (int k = 0; k < 5; k++) {
c.a[i][j] = min(c.a[i][j], a.a[i][k] + b.a[k][j]);
}
}
}
return c;
}
Matrix up[50005][18];
signed main(){
int k, n, m, o;
cin >> k >> n >> m >> o;
for(int i=1; i<=m; i++) {
int u, v, w;
cin >> u >> v >> w;
// up[j][i]: tầng thứ 2^i bên cạnh tầng j
up[u/k][0].a[u%k][v%k] = w;
// a[x][y]: x là vị trí của đỉnh u trong tầng u nằm, y tương tự là của v
}
for(int i=1; (1 << i) <= n; i++){
for(int j=0; j + (1 << i) - 1 <= n; j++){
up[j][i] = mul(up[j][i-1], up[j+(1<<(i-1))][i-1]);
}
}
for(int i = 0; i<o; i++){
int a, b;
cin >> a >> b;
Matrix res;
for(int j = 0; j < 5; j++){
res.a[j][j] = 0;
}
if(a == b){
cout << 0 << endl;
continue;
}
int div1 = a / k, div2 = b / k;
int dist = div2 - div1;
for(int j = 16; j >= 0; j--){
if(dist & (1 << j)){
res = mul(res, up[div1][j]);
div1 += (1 << j);
}
}
if(res.a[a % k][b % k] == minji) cout << -1 << endl;
else cout << res.a[a % k][b % k] << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
304 ms |
176420 KB |
Output is correct |
2 |
Correct |
70 ms |
176348 KB |
Output is correct |
3 |
Correct |
74 ms |
176372 KB |
Output is correct |
4 |
Correct |
74 ms |
176332 KB |
Output is correct |
5 |
Correct |
79 ms |
176372 KB |
Output is correct |
6 |
Correct |
75 ms |
176424 KB |
Output is correct |
7 |
Correct |
82 ms |
176336 KB |
Output is correct |
8 |
Correct |
306 ms |
176416 KB |
Output is correct |
9 |
Correct |
301 ms |
176420 KB |
Output is correct |
10 |
Correct |
287 ms |
176344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
330 ms |
176576 KB |
Output is correct |
2 |
Correct |
72 ms |
176376 KB |
Output is correct |
3 |
Correct |
72 ms |
176376 KB |
Output is correct |
4 |
Correct |
73 ms |
176420 KB |
Output is correct |
5 |
Correct |
70 ms |
176348 KB |
Output is correct |
6 |
Correct |
75 ms |
176312 KB |
Output is correct |
7 |
Correct |
95 ms |
176408 KB |
Output is correct |
8 |
Correct |
103 ms |
176628 KB |
Output is correct |
9 |
Correct |
346 ms |
177300 KB |
Output is correct |
10 |
Correct |
361 ms |
178788 KB |
Output is correct |
11 |
Correct |
329 ms |
178208 KB |
Output is correct |
12 |
Correct |
319 ms |
177752 KB |
Output is correct |
13 |
Correct |
291 ms |
178836 KB |
Output is correct |
14 |
Correct |
241 ms |
177980 KB |
Output is correct |
15 |
Correct |
250 ms |
177636 KB |
Output is correct |
16 |
Correct |
243 ms |
177528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
176396 KB |
Output is correct |
2 |
Correct |
76 ms |
176384 KB |
Output is correct |
3 |
Correct |
71 ms |
176372 KB |
Output is correct |
4 |
Correct |
72 ms |
176332 KB |
Output is correct |
5 |
Correct |
74 ms |
176320 KB |
Output is correct |
6 |
Correct |
73 ms |
176496 KB |
Output is correct |
7 |
Correct |
77 ms |
176408 KB |
Output is correct |
8 |
Correct |
81 ms |
176464 KB |
Output is correct |
9 |
Correct |
75 ms |
176452 KB |
Output is correct |
10 |
Correct |
264 ms |
177228 KB |
Output is correct |
11 |
Correct |
307 ms |
178000 KB |
Output is correct |
12 |
Correct |
319 ms |
178496 KB |
Output is correct |
13 |
Correct |
380 ms |
178728 KB |
Output is correct |
14 |
Correct |
300 ms |
178208 KB |
Output is correct |
15 |
Correct |
216 ms |
177616 KB |
Output is correct |
16 |
Correct |
211 ms |
177700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
176396 KB |
Output is correct |
2 |
Correct |
76 ms |
176384 KB |
Output is correct |
3 |
Correct |
71 ms |
176372 KB |
Output is correct |
4 |
Correct |
72 ms |
176332 KB |
Output is correct |
5 |
Correct |
74 ms |
176320 KB |
Output is correct |
6 |
Correct |
73 ms |
176496 KB |
Output is correct |
7 |
Correct |
77 ms |
176408 KB |
Output is correct |
8 |
Correct |
81 ms |
176464 KB |
Output is correct |
9 |
Correct |
75 ms |
176452 KB |
Output is correct |
10 |
Correct |
264 ms |
177228 KB |
Output is correct |
11 |
Correct |
307 ms |
178000 KB |
Output is correct |
12 |
Correct |
319 ms |
178496 KB |
Output is correct |
13 |
Correct |
380 ms |
178728 KB |
Output is correct |
14 |
Correct |
300 ms |
178208 KB |
Output is correct |
15 |
Correct |
216 ms |
177616 KB |
Output is correct |
16 |
Correct |
211 ms |
177700 KB |
Output is correct |
17 |
Correct |
316 ms |
177980 KB |
Output is correct |
18 |
Correct |
70 ms |
176332 KB |
Output is correct |
19 |
Correct |
69 ms |
176336 KB |
Output is correct |
20 |
Correct |
71 ms |
176412 KB |
Output is correct |
21 |
Correct |
70 ms |
176324 KB |
Output is correct |
22 |
Correct |
77 ms |
176332 KB |
Output is correct |
23 |
Correct |
79 ms |
176360 KB |
Output is correct |
24 |
Correct |
82 ms |
176460 KB |
Output is correct |
25 |
Correct |
80 ms |
176392 KB |
Output is correct |
26 |
Correct |
83 ms |
176420 KB |
Output is correct |
27 |
Correct |
283 ms |
177168 KB |
Output is correct |
28 |
Correct |
338 ms |
178676 KB |
Output is correct |
29 |
Correct |
349 ms |
178940 KB |
Output is correct |
30 |
Correct |
319 ms |
178340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
304 ms |
176420 KB |
Output is correct |
2 |
Correct |
70 ms |
176348 KB |
Output is correct |
3 |
Correct |
74 ms |
176372 KB |
Output is correct |
4 |
Correct |
74 ms |
176332 KB |
Output is correct |
5 |
Correct |
79 ms |
176372 KB |
Output is correct |
6 |
Correct |
75 ms |
176424 KB |
Output is correct |
7 |
Correct |
82 ms |
176336 KB |
Output is correct |
8 |
Correct |
306 ms |
176416 KB |
Output is correct |
9 |
Correct |
301 ms |
176420 KB |
Output is correct |
10 |
Correct |
287 ms |
176344 KB |
Output is correct |
11 |
Correct |
330 ms |
176576 KB |
Output is correct |
12 |
Correct |
72 ms |
176376 KB |
Output is correct |
13 |
Correct |
72 ms |
176376 KB |
Output is correct |
14 |
Correct |
73 ms |
176420 KB |
Output is correct |
15 |
Correct |
70 ms |
176348 KB |
Output is correct |
16 |
Correct |
75 ms |
176312 KB |
Output is correct |
17 |
Correct |
95 ms |
176408 KB |
Output is correct |
18 |
Correct |
103 ms |
176628 KB |
Output is correct |
19 |
Correct |
346 ms |
177300 KB |
Output is correct |
20 |
Correct |
361 ms |
178788 KB |
Output is correct |
21 |
Correct |
329 ms |
178208 KB |
Output is correct |
22 |
Correct |
319 ms |
177752 KB |
Output is correct |
23 |
Correct |
291 ms |
178836 KB |
Output is correct |
24 |
Correct |
241 ms |
177980 KB |
Output is correct |
25 |
Correct |
250 ms |
177636 KB |
Output is correct |
26 |
Correct |
243 ms |
177528 KB |
Output is correct |
27 |
Correct |
72 ms |
176396 KB |
Output is correct |
28 |
Correct |
76 ms |
176384 KB |
Output is correct |
29 |
Correct |
71 ms |
176372 KB |
Output is correct |
30 |
Correct |
72 ms |
176332 KB |
Output is correct |
31 |
Correct |
74 ms |
176320 KB |
Output is correct |
32 |
Correct |
73 ms |
176496 KB |
Output is correct |
33 |
Correct |
77 ms |
176408 KB |
Output is correct |
34 |
Correct |
81 ms |
176464 KB |
Output is correct |
35 |
Correct |
75 ms |
176452 KB |
Output is correct |
36 |
Correct |
264 ms |
177228 KB |
Output is correct |
37 |
Correct |
307 ms |
178000 KB |
Output is correct |
38 |
Correct |
319 ms |
178496 KB |
Output is correct |
39 |
Correct |
380 ms |
178728 KB |
Output is correct |
40 |
Correct |
300 ms |
178208 KB |
Output is correct |
41 |
Correct |
216 ms |
177616 KB |
Output is correct |
42 |
Correct |
211 ms |
177700 KB |
Output is correct |
43 |
Correct |
316 ms |
177980 KB |
Output is correct |
44 |
Correct |
70 ms |
176332 KB |
Output is correct |
45 |
Correct |
69 ms |
176336 KB |
Output is correct |
46 |
Correct |
71 ms |
176412 KB |
Output is correct |
47 |
Correct |
70 ms |
176324 KB |
Output is correct |
48 |
Correct |
77 ms |
176332 KB |
Output is correct |
49 |
Correct |
79 ms |
176360 KB |
Output is correct |
50 |
Correct |
82 ms |
176460 KB |
Output is correct |
51 |
Correct |
80 ms |
176392 KB |
Output is correct |
52 |
Correct |
83 ms |
176420 KB |
Output is correct |
53 |
Correct |
283 ms |
177168 KB |
Output is correct |
54 |
Correct |
338 ms |
178676 KB |
Output is correct |
55 |
Correct |
349 ms |
178940 KB |
Output is correct |
56 |
Correct |
319 ms |
178340 KB |
Output is correct |
57 |
Correct |
410 ms |
179760 KB |
Output is correct |
58 |
Correct |
332 ms |
177356 KB |
Output is correct |
59 |
Correct |
384 ms |
178284 KB |
Output is correct |