#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pll pair<long long, long long>
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
const ll N = 5e4 + 100;
const ll inf = 1e9;
const ll mod = 1e9 + 7;
const ll block = 450;
ll k,n,m,q;
struct Matrix{
vector<vector<ll>>d;
ll n,m;
void init(ll _n, ll _m){
n = _n;
m = _m;
d.resize(n, vector<ll>(m));
}
ll col() const{
return m;
}
ll row() const{
return n;
}
};
Matrix operator* (const Matrix &a, const Matrix &b){
Matrix ans;
ans.init(a.row(), b.col());
for(int i = 0; i < a.row();i++){
for(int j = 0; j < b.col();j++) ans.d[i][j] = inf;
}
for(int i = 0; i < a.row(); i++){
for(int j = 0; j < b.col();j++){
for(int k = 0; k < a.col();k++){
ans.d[i][j] = min(ans.d[i][j], a.d[i][k] + b.d[k][j]);
}
}
}
return ans;
}
Matrix t[N];
Matrix st[N][17];
void to_thic_cau(){
cin >> k >> n >> m >> q;
ll lim = n / k;
for(int i = 0; i <= lim;i++){
t[i].init(k, k);
for(int j = 0; j < k;j++){
for(int x = 0; x < k;x++) t[i].d[j][x] = inf;
}
}
for(int i = 1; i <= m;i++){
ll u,v,w; cin >> u >> v >> w;
t[v / k].d[u % k][v % k] = w;
}
for(int i = 0; i <= lim;i++) st[i][0] = t[i];
for(int j = 1; j <= 16;j++){
for(int i = 0; i + (1 << j) <= lim + 1;i++) st[i][j] = st[i][j-1] * st[i + (1 << (j - 1))][j-1];
}
while(q--){
ll a,b; cin >> a >> b;
if(a / k == b / k){
if(a != b){
cout << -1 << "\n";
}
else cout << 0 << '\n';
continue;
}
ll l = a / k + 1, r = b / k;
Matrix cur;
cur.init(1, k);
for(int i = 0; i < k;i++) cur.d[0][i] = inf;
cur.d[0][a % k] = 0;
ll d = r - l + 1;
for(int i = 16; i >= 0;i--){
if(d & (1 << i)){
assert(l <= lim);
cur = cur * st[l][i];
l += (1 << i);
}
}
if(cur.d[0][b % k] >= 1e9){
cout << -1 << "\n";
}
else cout << cur.d[0][b % k] << "\n";
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll tc = 1;
//cin >> tc;
while(tc--) to_thic_cau();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
78648 KB |
Output is correct |
2 |
Correct |
13 ms |
28504 KB |
Output is correct |
3 |
Correct |
11 ms |
28508 KB |
Output is correct |
4 |
Correct |
12 ms |
28508 KB |
Output is correct |
5 |
Correct |
14 ms |
29304 KB |
Output is correct |
6 |
Correct |
14 ms |
29276 KB |
Output is correct |
7 |
Correct |
14 ms |
29248 KB |
Output is correct |
8 |
Correct |
176 ms |
78740 KB |
Output is correct |
9 |
Correct |
118 ms |
78676 KB |
Output is correct |
10 |
Correct |
106 ms |
77744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
76080 KB |
Output is correct |
2 |
Correct |
16 ms |
28508 KB |
Output is correct |
3 |
Correct |
17 ms |
28508 KB |
Output is correct |
4 |
Correct |
15 ms |
28504 KB |
Output is correct |
5 |
Correct |
12 ms |
28508 KB |
Output is correct |
6 |
Correct |
15 ms |
28508 KB |
Output is correct |
7 |
Correct |
31 ms |
29276 KB |
Output is correct |
8 |
Correct |
22 ms |
29276 KB |
Output is correct |
9 |
Correct |
140 ms |
78504 KB |
Output is correct |
10 |
Correct |
131 ms |
71252 KB |
Output is correct |
11 |
Correct |
124 ms |
76200 KB |
Output is correct |
12 |
Correct |
159 ms |
70172 KB |
Output is correct |
13 |
Correct |
93 ms |
52304 KB |
Output is correct |
14 |
Correct |
75 ms |
53076 KB |
Output is correct |
15 |
Correct |
79 ms |
51032 KB |
Output is correct |
16 |
Correct |
72 ms |
51028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
28684 KB |
Output is correct |
2 |
Correct |
11 ms |
28608 KB |
Output is correct |
3 |
Correct |
11 ms |
28508 KB |
Output is correct |
4 |
Correct |
17 ms |
28700 KB |
Output is correct |
5 |
Correct |
12 ms |
28508 KB |
Output is correct |
6 |
Correct |
17 ms |
29276 KB |
Output is correct |
7 |
Correct |
12 ms |
29200 KB |
Output is correct |
8 |
Correct |
20 ms |
29044 KB |
Output is correct |
9 |
Correct |
21 ms |
29020 KB |
Output is correct |
10 |
Correct |
111 ms |
78424 KB |
Output is correct |
11 |
Correct |
97 ms |
75956 KB |
Output is correct |
12 |
Correct |
109 ms |
70996 KB |
Output is correct |
13 |
Correct |
112 ms |
71252 KB |
Output is correct |
14 |
Correct |
143 ms |
70716 KB |
Output is correct |
15 |
Correct |
72 ms |
51024 KB |
Output is correct |
16 |
Correct |
95 ms |
51028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
28684 KB |
Output is correct |
2 |
Correct |
11 ms |
28608 KB |
Output is correct |
3 |
Correct |
11 ms |
28508 KB |
Output is correct |
4 |
Correct |
17 ms |
28700 KB |
Output is correct |
5 |
Correct |
12 ms |
28508 KB |
Output is correct |
6 |
Correct |
17 ms |
29276 KB |
Output is correct |
7 |
Correct |
12 ms |
29200 KB |
Output is correct |
8 |
Correct |
20 ms |
29044 KB |
Output is correct |
9 |
Correct |
21 ms |
29020 KB |
Output is correct |
10 |
Correct |
111 ms |
78424 KB |
Output is correct |
11 |
Correct |
97 ms |
75956 KB |
Output is correct |
12 |
Correct |
109 ms |
70996 KB |
Output is correct |
13 |
Correct |
112 ms |
71252 KB |
Output is correct |
14 |
Correct |
143 ms |
70716 KB |
Output is correct |
15 |
Correct |
72 ms |
51024 KB |
Output is correct |
16 |
Correct |
95 ms |
51028 KB |
Output is correct |
17 |
Correct |
102 ms |
76020 KB |
Output is correct |
18 |
Correct |
13 ms |
28472 KB |
Output is correct |
19 |
Correct |
15 ms |
28508 KB |
Output is correct |
20 |
Correct |
14 ms |
28688 KB |
Output is correct |
21 |
Correct |
17 ms |
28508 KB |
Output is correct |
22 |
Correct |
14 ms |
28508 KB |
Output is correct |
23 |
Correct |
14 ms |
29276 KB |
Output is correct |
24 |
Correct |
14 ms |
29332 KB |
Output is correct |
25 |
Correct |
23 ms |
29020 KB |
Output is correct |
26 |
Correct |
19 ms |
29016 KB |
Output is correct |
27 |
Correct |
110 ms |
78416 KB |
Output is correct |
28 |
Correct |
122 ms |
70996 KB |
Output is correct |
29 |
Correct |
137 ms |
71176 KB |
Output is correct |
30 |
Correct |
130 ms |
70736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
78648 KB |
Output is correct |
2 |
Correct |
13 ms |
28504 KB |
Output is correct |
3 |
Correct |
11 ms |
28508 KB |
Output is correct |
4 |
Correct |
12 ms |
28508 KB |
Output is correct |
5 |
Correct |
14 ms |
29304 KB |
Output is correct |
6 |
Correct |
14 ms |
29276 KB |
Output is correct |
7 |
Correct |
14 ms |
29248 KB |
Output is correct |
8 |
Correct |
176 ms |
78740 KB |
Output is correct |
9 |
Correct |
118 ms |
78676 KB |
Output is correct |
10 |
Correct |
106 ms |
77744 KB |
Output is correct |
11 |
Correct |
107 ms |
76080 KB |
Output is correct |
12 |
Correct |
16 ms |
28508 KB |
Output is correct |
13 |
Correct |
17 ms |
28508 KB |
Output is correct |
14 |
Correct |
15 ms |
28504 KB |
Output is correct |
15 |
Correct |
12 ms |
28508 KB |
Output is correct |
16 |
Correct |
15 ms |
28508 KB |
Output is correct |
17 |
Correct |
31 ms |
29276 KB |
Output is correct |
18 |
Correct |
22 ms |
29276 KB |
Output is correct |
19 |
Correct |
140 ms |
78504 KB |
Output is correct |
20 |
Correct |
131 ms |
71252 KB |
Output is correct |
21 |
Correct |
124 ms |
76200 KB |
Output is correct |
22 |
Correct |
159 ms |
70172 KB |
Output is correct |
23 |
Correct |
93 ms |
52304 KB |
Output is correct |
24 |
Correct |
75 ms |
53076 KB |
Output is correct |
25 |
Correct |
79 ms |
51032 KB |
Output is correct |
26 |
Correct |
72 ms |
51028 KB |
Output is correct |
27 |
Correct |
12 ms |
28684 KB |
Output is correct |
28 |
Correct |
11 ms |
28608 KB |
Output is correct |
29 |
Correct |
11 ms |
28508 KB |
Output is correct |
30 |
Correct |
17 ms |
28700 KB |
Output is correct |
31 |
Correct |
12 ms |
28508 KB |
Output is correct |
32 |
Correct |
17 ms |
29276 KB |
Output is correct |
33 |
Correct |
12 ms |
29200 KB |
Output is correct |
34 |
Correct |
20 ms |
29044 KB |
Output is correct |
35 |
Correct |
21 ms |
29020 KB |
Output is correct |
36 |
Correct |
111 ms |
78424 KB |
Output is correct |
37 |
Correct |
97 ms |
75956 KB |
Output is correct |
38 |
Correct |
109 ms |
70996 KB |
Output is correct |
39 |
Correct |
112 ms |
71252 KB |
Output is correct |
40 |
Correct |
143 ms |
70716 KB |
Output is correct |
41 |
Correct |
72 ms |
51024 KB |
Output is correct |
42 |
Correct |
95 ms |
51028 KB |
Output is correct |
43 |
Correct |
102 ms |
76020 KB |
Output is correct |
44 |
Correct |
13 ms |
28472 KB |
Output is correct |
45 |
Correct |
15 ms |
28508 KB |
Output is correct |
46 |
Correct |
14 ms |
28688 KB |
Output is correct |
47 |
Correct |
17 ms |
28508 KB |
Output is correct |
48 |
Correct |
14 ms |
28508 KB |
Output is correct |
49 |
Correct |
14 ms |
29276 KB |
Output is correct |
50 |
Correct |
14 ms |
29332 KB |
Output is correct |
51 |
Correct |
23 ms |
29020 KB |
Output is correct |
52 |
Correct |
19 ms |
29016 KB |
Output is correct |
53 |
Correct |
110 ms |
78416 KB |
Output is correct |
54 |
Correct |
122 ms |
70996 KB |
Output is correct |
55 |
Correct |
137 ms |
71176 KB |
Output is correct |
56 |
Correct |
130 ms |
70736 KB |
Output is correct |
57 |
Correct |
167 ms |
72016 KB |
Output is correct |
58 |
Correct |
140 ms |
78676 KB |
Output is correct |
59 |
Correct |
138 ms |
76340 KB |
Output is correct |