/*
#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 |
160260 KB |
Output is correct |
2 |
Correct |
61 ms |
156876 KB |
Output is correct |
3 |
Correct |
63 ms |
156752 KB |
Output is correct |
4 |
Correct |
62 ms |
156868 KB |
Output is correct |
5 |
Correct |
65 ms |
156928 KB |
Output is correct |
6 |
Correct |
67 ms |
156876 KB |
Output is correct |
7 |
Correct |
67 ms |
157016 KB |
Output is correct |
8 |
Correct |
350 ms |
160144 KB |
Output is correct |
9 |
Correct |
346 ms |
160076 KB |
Output is correct |
10 |
Correct |
329 ms |
160072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
160104 KB |
Output is correct |
2 |
Correct |
61 ms |
156820 KB |
Output is correct |
3 |
Correct |
60 ms |
156820 KB |
Output is correct |
4 |
Correct |
59 ms |
156788 KB |
Output is correct |
5 |
Correct |
63 ms |
156804 KB |
Output is correct |
6 |
Correct |
62 ms |
156880 KB |
Output is correct |
7 |
Correct |
76 ms |
157024 KB |
Output is correct |
8 |
Correct |
83 ms |
157020 KB |
Output is correct |
9 |
Correct |
338 ms |
160272 KB |
Output is correct |
10 |
Correct |
194 ms |
160260 KB |
Output is correct |
11 |
Correct |
229 ms |
160256 KB |
Output is correct |
12 |
Correct |
180 ms |
160248 KB |
Output is correct |
13 |
Correct |
127 ms |
159076 KB |
Output is correct |
14 |
Correct |
133 ms |
158924 KB |
Output is correct |
15 |
Correct |
113 ms |
159016 KB |
Output is correct |
16 |
Correct |
110 ms |
158988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
156876 KB |
Output is correct |
2 |
Correct |
61 ms |
156852 KB |
Output is correct |
3 |
Correct |
61 ms |
156856 KB |
Output is correct |
4 |
Correct |
60 ms |
156832 KB |
Output is correct |
5 |
Correct |
61 ms |
156768 KB |
Output is correct |
6 |
Correct |
69 ms |
156848 KB |
Output is correct |
7 |
Correct |
63 ms |
156968 KB |
Output is correct |
8 |
Correct |
63 ms |
156916 KB |
Output is correct |
9 |
Correct |
64 ms |
156980 KB |
Output is correct |
10 |
Correct |
323 ms |
160184 KB |
Output is correct |
11 |
Correct |
201 ms |
160200 KB |
Output is correct |
12 |
Correct |
180 ms |
160252 KB |
Output is correct |
13 |
Correct |
187 ms |
160204 KB |
Output is correct |
14 |
Correct |
169 ms |
160180 KB |
Output is correct |
15 |
Correct |
105 ms |
158964 KB |
Output is correct |
16 |
Correct |
106 ms |
158956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
156876 KB |
Output is correct |
2 |
Correct |
61 ms |
156852 KB |
Output is correct |
3 |
Correct |
61 ms |
156856 KB |
Output is correct |
4 |
Correct |
60 ms |
156832 KB |
Output is correct |
5 |
Correct |
61 ms |
156768 KB |
Output is correct |
6 |
Correct |
69 ms |
156848 KB |
Output is correct |
7 |
Correct |
63 ms |
156968 KB |
Output is correct |
8 |
Correct |
63 ms |
156916 KB |
Output is correct |
9 |
Correct |
64 ms |
156980 KB |
Output is correct |
10 |
Correct |
323 ms |
160184 KB |
Output is correct |
11 |
Correct |
201 ms |
160200 KB |
Output is correct |
12 |
Correct |
180 ms |
160252 KB |
Output is correct |
13 |
Correct |
187 ms |
160204 KB |
Output is correct |
14 |
Correct |
169 ms |
160180 KB |
Output is correct |
15 |
Correct |
105 ms |
158964 KB |
Output is correct |
16 |
Correct |
106 ms |
158956 KB |
Output is correct |
17 |
Correct |
213 ms |
160220 KB |
Output is correct |
18 |
Correct |
62 ms |
156872 KB |
Output is correct |
19 |
Correct |
61 ms |
156876 KB |
Output is correct |
20 |
Correct |
65 ms |
156824 KB |
Output is correct |
21 |
Correct |
61 ms |
156864 KB |
Output is correct |
22 |
Correct |
61 ms |
156876 KB |
Output is correct |
23 |
Correct |
68 ms |
156956 KB |
Output is correct |
24 |
Correct |
67 ms |
157004 KB |
Output is correct |
25 |
Correct |
66 ms |
157016 KB |
Output is correct |
26 |
Correct |
67 ms |
157004 KB |
Output is correct |
27 |
Correct |
364 ms |
160244 KB |
Output is correct |
28 |
Correct |
184 ms |
160260 KB |
Output is correct |
29 |
Correct |
180 ms |
160260 KB |
Output is correct |
30 |
Correct |
177 ms |
160132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
350 ms |
160260 KB |
Output is correct |
2 |
Correct |
61 ms |
156876 KB |
Output is correct |
3 |
Correct |
63 ms |
156752 KB |
Output is correct |
4 |
Correct |
62 ms |
156868 KB |
Output is correct |
5 |
Correct |
65 ms |
156928 KB |
Output is correct |
6 |
Correct |
67 ms |
156876 KB |
Output is correct |
7 |
Correct |
67 ms |
157016 KB |
Output is correct |
8 |
Correct |
350 ms |
160144 KB |
Output is correct |
9 |
Correct |
346 ms |
160076 KB |
Output is correct |
10 |
Correct |
329 ms |
160072 KB |
Output is correct |
11 |
Correct |
231 ms |
160104 KB |
Output is correct |
12 |
Correct |
61 ms |
156820 KB |
Output is correct |
13 |
Correct |
60 ms |
156820 KB |
Output is correct |
14 |
Correct |
59 ms |
156788 KB |
Output is correct |
15 |
Correct |
63 ms |
156804 KB |
Output is correct |
16 |
Correct |
62 ms |
156880 KB |
Output is correct |
17 |
Correct |
76 ms |
157024 KB |
Output is correct |
18 |
Correct |
83 ms |
157020 KB |
Output is correct |
19 |
Correct |
338 ms |
160272 KB |
Output is correct |
20 |
Correct |
194 ms |
160260 KB |
Output is correct |
21 |
Correct |
229 ms |
160256 KB |
Output is correct |
22 |
Correct |
180 ms |
160248 KB |
Output is correct |
23 |
Correct |
127 ms |
159076 KB |
Output is correct |
24 |
Correct |
133 ms |
158924 KB |
Output is correct |
25 |
Correct |
113 ms |
159016 KB |
Output is correct |
26 |
Correct |
110 ms |
158988 KB |
Output is correct |
27 |
Correct |
60 ms |
156876 KB |
Output is correct |
28 |
Correct |
61 ms |
156852 KB |
Output is correct |
29 |
Correct |
61 ms |
156856 KB |
Output is correct |
30 |
Correct |
60 ms |
156832 KB |
Output is correct |
31 |
Correct |
61 ms |
156768 KB |
Output is correct |
32 |
Correct |
69 ms |
156848 KB |
Output is correct |
33 |
Correct |
63 ms |
156968 KB |
Output is correct |
34 |
Correct |
63 ms |
156916 KB |
Output is correct |
35 |
Correct |
64 ms |
156980 KB |
Output is correct |
36 |
Correct |
323 ms |
160184 KB |
Output is correct |
37 |
Correct |
201 ms |
160200 KB |
Output is correct |
38 |
Correct |
180 ms |
160252 KB |
Output is correct |
39 |
Correct |
187 ms |
160204 KB |
Output is correct |
40 |
Correct |
169 ms |
160180 KB |
Output is correct |
41 |
Correct |
105 ms |
158964 KB |
Output is correct |
42 |
Correct |
106 ms |
158956 KB |
Output is correct |
43 |
Correct |
213 ms |
160220 KB |
Output is correct |
44 |
Correct |
62 ms |
156872 KB |
Output is correct |
45 |
Correct |
61 ms |
156876 KB |
Output is correct |
46 |
Correct |
65 ms |
156824 KB |
Output is correct |
47 |
Correct |
61 ms |
156864 KB |
Output is correct |
48 |
Correct |
61 ms |
156876 KB |
Output is correct |
49 |
Correct |
68 ms |
156956 KB |
Output is correct |
50 |
Correct |
67 ms |
157004 KB |
Output is correct |
51 |
Correct |
66 ms |
157016 KB |
Output is correct |
52 |
Correct |
67 ms |
157004 KB |
Output is correct |
53 |
Correct |
364 ms |
160244 KB |
Output is correct |
54 |
Correct |
184 ms |
160260 KB |
Output is correct |
55 |
Correct |
180 ms |
160260 KB |
Output is correct |
56 |
Correct |
177 ms |
160132 KB |
Output is correct |
57 |
Correct |
187 ms |
160204 KB |
Output is correct |
58 |
Correct |
345 ms |
160132 KB |
Output is correct |
59 |
Correct |
227 ms |
160152 KB |
Output is correct |