/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
#include <bits/stdc++.h>
#define taskname "disrupt"
#define all(x) x.begin(), x.end()
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
using namespace std;
const int mxN = 5e4 + 5;
const int mod = 1e9 + 7;
const int lim = 16;
const ll oo = 1e18;
struct matrix {
ll a[5][5];
matrix() {memset(a, 0x3f, sizeof a);}
matrix operator + (const matrix &o) {
matrix res;
for(int i = 0; i < 5; ++i)
for(int j = 0; j < 5; ++j)
for(int k = 0; k < 5; ++k) {
if(a[i][j] > oo || o.a[j][k] > oo)
continue;
res.a[i][k] = min(res.a[i][k], a[i][j] + o.a[j][k]);
}
return res;
}
};
int n, m, k, q;
int par[mxN][lim];
matrix path[mxN][lim];
void solve() {
cin >> k >> n >> m >> q;
for(int i = 0; i < n; ++i)
par[i][0] = i + 1;
par[n][0] = n;
for(int i = 1; i <= m; ++i) {
int u, v, t;
cin >> u >> v >> t;
path[u / k][0].a[u % k][v % k] = t;
}
for(int i = 1; i < lim; ++i) {
for(int j = 0; j * k <= n; ++j) {
par[j][i] = par[par[j][i - 1]][i - 1];
path[j][i] = path[j][i - 1] + path[par[j][i - 1]][i - 1];
}
}
while(q--) {
int a, b;
cin >> a >> b;
if(b < a) {
cout << "-1\n";
continue;
}
if(a == b) {
cout << "0\n";
continue;
}
matrix res;
for(int i = 0; i < 5; ++i)
res.a[i][i] = 0;
int floor_a = a / k, floor_b = b / k;
for(int i = lim - 1; i >= 0; --i) {
int en = floor_a + (1 << i);
if(en <= floor_b) {
res = res + path[floor_a][i];
floor_a = par[floor_a][i];
}
}
ll ans = res.a[a % k][b % k];
if(ans > oo)
ans = -1;
cout << ans << "\n";
}
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".in", "r"))
assert(freopen(taskname".in", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
350 ms |
160012 KB |
Output is correct |
2 |
Correct |
58 ms |
156864 KB |
Output is correct |
3 |
Correct |
59 ms |
156752 KB |
Output is correct |
4 |
Correct |
59 ms |
156816 KB |
Output is correct |
5 |
Correct |
62 ms |
156836 KB |
Output is correct |
6 |
Correct |
63 ms |
156924 KB |
Output is correct |
7 |
Correct |
67 ms |
156876 KB |
Output is correct |
8 |
Correct |
344 ms |
160048 KB |
Output is correct |
9 |
Correct |
351 ms |
159948 KB |
Output is correct |
10 |
Correct |
358 ms |
160064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
213 ms |
159948 KB |
Output is correct |
2 |
Correct |
58 ms |
156852 KB |
Output is correct |
3 |
Correct |
60 ms |
156764 KB |
Output is correct |
4 |
Correct |
58 ms |
156844 KB |
Output is correct |
5 |
Correct |
66 ms |
156780 KB |
Output is correct |
6 |
Correct |
60 ms |
156812 KB |
Output is correct |
7 |
Correct |
77 ms |
156940 KB |
Output is correct |
8 |
Correct |
71 ms |
157016 KB |
Output is correct |
9 |
Correct |
340 ms |
160852 KB |
Output is correct |
10 |
Correct |
199 ms |
162252 KB |
Output is correct |
11 |
Correct |
214 ms |
161712 KB |
Output is correct |
12 |
Correct |
179 ms |
161292 KB |
Output is correct |
13 |
Correct |
131 ms |
161144 KB |
Output is correct |
14 |
Correct |
129 ms |
160204 KB |
Output is correct |
15 |
Correct |
112 ms |
159984 KB |
Output is correct |
16 |
Correct |
107 ms |
160076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
156848 KB |
Output is correct |
2 |
Correct |
59 ms |
156844 KB |
Output is correct |
3 |
Correct |
58 ms |
156872 KB |
Output is correct |
4 |
Correct |
57 ms |
156784 KB |
Output is correct |
5 |
Correct |
63 ms |
156828 KB |
Output is correct |
6 |
Correct |
64 ms |
156904 KB |
Output is correct |
7 |
Correct |
62 ms |
157020 KB |
Output is correct |
8 |
Correct |
66 ms |
156952 KB |
Output is correct |
9 |
Correct |
60 ms |
156876 KB |
Output is correct |
10 |
Correct |
314 ms |
160692 KB |
Output is correct |
11 |
Correct |
209 ms |
161452 KB |
Output is correct |
12 |
Correct |
171 ms |
162068 KB |
Output is correct |
13 |
Correct |
176 ms |
162324 KB |
Output is correct |
14 |
Correct |
166 ms |
161708 KB |
Output is correct |
15 |
Correct |
106 ms |
159848 KB |
Output is correct |
16 |
Correct |
104 ms |
159936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
156848 KB |
Output is correct |
2 |
Correct |
59 ms |
156844 KB |
Output is correct |
3 |
Correct |
58 ms |
156872 KB |
Output is correct |
4 |
Correct |
57 ms |
156784 KB |
Output is correct |
5 |
Correct |
63 ms |
156828 KB |
Output is correct |
6 |
Correct |
64 ms |
156904 KB |
Output is correct |
7 |
Correct |
62 ms |
157020 KB |
Output is correct |
8 |
Correct |
66 ms |
156952 KB |
Output is correct |
9 |
Correct |
60 ms |
156876 KB |
Output is correct |
10 |
Correct |
314 ms |
160692 KB |
Output is correct |
11 |
Correct |
209 ms |
161452 KB |
Output is correct |
12 |
Correct |
171 ms |
162068 KB |
Output is correct |
13 |
Correct |
176 ms |
162324 KB |
Output is correct |
14 |
Correct |
166 ms |
161708 KB |
Output is correct |
15 |
Correct |
106 ms |
159848 KB |
Output is correct |
16 |
Correct |
104 ms |
159936 KB |
Output is correct |
17 |
Correct |
224 ms |
161496 KB |
Output is correct |
18 |
Correct |
58 ms |
156880 KB |
Output is correct |
19 |
Correct |
58 ms |
156860 KB |
Output is correct |
20 |
Correct |
58 ms |
156808 KB |
Output is correct |
21 |
Correct |
59 ms |
156788 KB |
Output is correct |
22 |
Correct |
60 ms |
156824 KB |
Output is correct |
23 |
Correct |
71 ms |
156984 KB |
Output is correct |
24 |
Correct |
69 ms |
156912 KB |
Output is correct |
25 |
Correct |
64 ms |
157024 KB |
Output is correct |
26 |
Correct |
67 ms |
157024 KB |
Output is correct |
27 |
Correct |
324 ms |
160840 KB |
Output is correct |
28 |
Correct |
187 ms |
162240 KB |
Output is correct |
29 |
Correct |
185 ms |
162468 KB |
Output is correct |
30 |
Correct |
175 ms |
161856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
350 ms |
160012 KB |
Output is correct |
2 |
Correct |
58 ms |
156864 KB |
Output is correct |
3 |
Correct |
59 ms |
156752 KB |
Output is correct |
4 |
Correct |
59 ms |
156816 KB |
Output is correct |
5 |
Correct |
62 ms |
156836 KB |
Output is correct |
6 |
Correct |
63 ms |
156924 KB |
Output is correct |
7 |
Correct |
67 ms |
156876 KB |
Output is correct |
8 |
Correct |
344 ms |
160048 KB |
Output is correct |
9 |
Correct |
351 ms |
159948 KB |
Output is correct |
10 |
Correct |
358 ms |
160064 KB |
Output is correct |
11 |
Correct |
213 ms |
159948 KB |
Output is correct |
12 |
Correct |
58 ms |
156852 KB |
Output is correct |
13 |
Correct |
60 ms |
156764 KB |
Output is correct |
14 |
Correct |
58 ms |
156844 KB |
Output is correct |
15 |
Correct |
66 ms |
156780 KB |
Output is correct |
16 |
Correct |
60 ms |
156812 KB |
Output is correct |
17 |
Correct |
77 ms |
156940 KB |
Output is correct |
18 |
Correct |
71 ms |
157016 KB |
Output is correct |
19 |
Correct |
340 ms |
160852 KB |
Output is correct |
20 |
Correct |
199 ms |
162252 KB |
Output is correct |
21 |
Correct |
214 ms |
161712 KB |
Output is correct |
22 |
Correct |
179 ms |
161292 KB |
Output is correct |
23 |
Correct |
131 ms |
161144 KB |
Output is correct |
24 |
Correct |
129 ms |
160204 KB |
Output is correct |
25 |
Correct |
112 ms |
159984 KB |
Output is correct |
26 |
Correct |
107 ms |
160076 KB |
Output is correct |
27 |
Correct |
63 ms |
156848 KB |
Output is correct |
28 |
Correct |
59 ms |
156844 KB |
Output is correct |
29 |
Correct |
58 ms |
156872 KB |
Output is correct |
30 |
Correct |
57 ms |
156784 KB |
Output is correct |
31 |
Correct |
63 ms |
156828 KB |
Output is correct |
32 |
Correct |
64 ms |
156904 KB |
Output is correct |
33 |
Correct |
62 ms |
157020 KB |
Output is correct |
34 |
Correct |
66 ms |
156952 KB |
Output is correct |
35 |
Correct |
60 ms |
156876 KB |
Output is correct |
36 |
Correct |
314 ms |
160692 KB |
Output is correct |
37 |
Correct |
209 ms |
161452 KB |
Output is correct |
38 |
Correct |
171 ms |
162068 KB |
Output is correct |
39 |
Correct |
176 ms |
162324 KB |
Output is correct |
40 |
Correct |
166 ms |
161708 KB |
Output is correct |
41 |
Correct |
106 ms |
159848 KB |
Output is correct |
42 |
Correct |
104 ms |
159936 KB |
Output is correct |
43 |
Correct |
224 ms |
161496 KB |
Output is correct |
44 |
Correct |
58 ms |
156880 KB |
Output is correct |
45 |
Correct |
58 ms |
156860 KB |
Output is correct |
46 |
Correct |
58 ms |
156808 KB |
Output is correct |
47 |
Correct |
59 ms |
156788 KB |
Output is correct |
48 |
Correct |
60 ms |
156824 KB |
Output is correct |
49 |
Correct |
71 ms |
156984 KB |
Output is correct |
50 |
Correct |
69 ms |
156912 KB |
Output is correct |
51 |
Correct |
64 ms |
157024 KB |
Output is correct |
52 |
Correct |
67 ms |
157024 KB |
Output is correct |
53 |
Correct |
324 ms |
160840 KB |
Output is correct |
54 |
Correct |
187 ms |
162240 KB |
Output is correct |
55 |
Correct |
185 ms |
162468 KB |
Output is correct |
56 |
Correct |
175 ms |
161856 KB |
Output is correct |
57 |
Correct |
185 ms |
163296 KB |
Output is correct |
58 |
Correct |
355 ms |
160924 KB |
Output is correct |
59 |
Correct |
229 ms |
161760 KB |
Output is correct |