#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define el cout << '\n'
#define f1(i,n) for(int i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 5e4+5, inf=1e18;
struct Matrix{
int r, c;
vector<vector<ll>> data;
Matrix(){};
Matrix(int r, int c): r(r), c(c), data(r, vector<ll>(c, 1e9)){};
Matrix(vector<vector<ll>> a): r(a.size()), c(a.empty() ? 0 : a[0].size()), data(a){};
Matrix operator*(const Matrix &rhs) const{
assert(c == rhs.r);
Matrix res(r, rhs.c);
// cout << (*this) << "TIMES\n" << rhs;el;
for(int i=0;i<r;i++){
for(int j=0;j<rhs.c;j++){
for(int k=0;k<c;k++){
res.data[i][j] = min(res.data[i][j], data[i][k] + rhs.data[k][j]);
}
}
}
return res;
}
Matrix operator^(const long long &e) const{
assert(r == c);
Matrix res(r,c), base(*this);
for(int i=0;i<r;i++) res.data[i][i] = 1;
long long _e = e;
while(_e){
if(_e & 1) res = res * base;
base = base * base;
_e >>= 1;
}
return res;
}
int get(int _r, int _c){
// 1-indexed
return data[_r-1][_c-1];
}
friend ostream& operator<<(ostream& ouf, const Matrix &rhs){
for(int i=0;i<rhs.r;i++){
for(int j=0;j<rhs.c;j++){
ouf << rhs.data[i][j] << ' ';
}
ouf << '\n';
}
return ouf;
}
};
Matrix st[4*maxn];
int k, n, m, o, id[maxn];
void build(int idx, int l, int r, bool flag = true){
if(l == r){
if(flag){
id[l] = idx;
st[idx] = Matrix(k, k);
// f1(i,k) f1(j,k) st[idx].data[i-1][j-1] = 1e9;
}
return;
}
int mid = (l + r) / 2;
build(idx*2, l, mid, flag);
build(idx*2+1, mid+1, r, flag);
st[idx] = st[idx*2] * st[idx*2+1];
}
Matrix get(int idx, int l, int r, int u, int v){
if(v < l || r < u) {
Matrix res(k, k);
f1(i, k) f1(j, k){
if(i == j) res.data[i-1][j-1] = 0;
}
return res;
}
if(u <= l && r <= v) return st[idx];
int mid = (l + r) / 2;
return get(idx*2, l, mid, u, v) * get(idx*2+1, mid+1, r, u, v);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if(fopen(__file_name ".inp", "r")){
freopen(__file_name ".inp","r",stdin);
freopen(__file_name ".out","w",stdout);
}
// code here
cin >> k >> n >> m >> o;
build(1, 0, n / k);
f1(i,m){
int a, b, t;
cin >> a >> b >> t;
int bl = a/k;
// cout << id[bl] << ' ' << a % k << ' ' << b % k << ' ' << t;el;
st[id[bl]].data[a % k][b % k] = min((int)st[id[bl]].data[a % k][b % k], t);
}
build(1, 0, n / k, false);
// cout << "BUILD COMPLETE\n\n";
// for(int i=0;i<=n/k;i++) cout << st[id[i]] << "\n\n";
while(o--){
int a, b; cin >> a >> b;
if(a / k == b / k){
if(a == b) cout << 0;
else cout << -1;
el;
continue;
}
int bla = a / k, blb = b / k;
Matrix res = Matrix(1, k);
for(int i=0;i<k;i++) res.data[0][i] = 1e9;
res.data[0][a % k] = 0;
// for(int b=bla;b<blb;b++) {
// res = res * st[id[b]];
// cout << b << ' ' << id[b];el;
// for(int i=0;i<k;i++) cout << res.data[0][i] << ' ';el;
// }
// for(int i=0;i<k;i++) cout << get(1,0,n/k,bla,blb-1).data[0][i] << ' ';el;
res = res * get(1,0,n/k,bla,blb-1);
// for(int i=0;i<k;i++) cout << get(1,0,n/k,bla,blb-1).data[0][i] << ' ';el;
if(res.data[0][b % k] >= 1e9) cout << -1;
else cout << res.data[0][b % k];
el;
}
return 0;
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen(__file_name ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen(__file_name ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
13180 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
2 ms |
6736 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
4 ms |
6736 KB |
Output is correct |
6 |
Correct |
4 ms |
6736 KB |
Output is correct |
7 |
Correct |
4 ms |
6736 KB |
Output is correct |
8 |
Correct |
54 ms |
13136 KB |
Output is correct |
9 |
Correct |
53 ms |
13184 KB |
Output is correct |
10 |
Correct |
48 ms |
13136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
13128 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
2 ms |
6736 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
13 ms |
6736 KB |
Output is correct |
8 |
Correct |
16 ms |
6736 KB |
Output is correct |
9 |
Correct |
54 ms |
13136 KB |
Output is correct |
10 |
Correct |
77 ms |
12808 KB |
Output is correct |
11 |
Correct |
59 ms |
13132 KB |
Output is correct |
12 |
Correct |
56 ms |
12368 KB |
Output is correct |
13 |
Correct |
52 ms |
11256 KB |
Output is correct |
14 |
Correct |
37 ms |
10064 KB |
Output is correct |
15 |
Correct |
41 ms |
11088 KB |
Output is correct |
16 |
Correct |
40 ms |
11092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6908 KB |
Output is correct |
2 |
Correct |
2 ms |
6592 KB |
Output is correct |
3 |
Correct |
2 ms |
6736 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
3 ms |
6736 KB |
Output is correct |
7 |
Correct |
3 ms |
6736 KB |
Output is correct |
8 |
Correct |
3 ms |
6736 KB |
Output is correct |
9 |
Correct |
3 ms |
6916 KB |
Output is correct |
10 |
Correct |
29 ms |
13200 KB |
Output is correct |
11 |
Correct |
36 ms |
12880 KB |
Output is correct |
12 |
Correct |
40 ms |
12368 KB |
Output is correct |
13 |
Correct |
43 ms |
12548 KB |
Output is correct |
14 |
Correct |
37 ms |
12368 KB |
Output is correct |
15 |
Correct |
27 ms |
11256 KB |
Output is correct |
16 |
Correct |
27 ms |
11088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6908 KB |
Output is correct |
2 |
Correct |
2 ms |
6592 KB |
Output is correct |
3 |
Correct |
2 ms |
6736 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
3 ms |
6736 KB |
Output is correct |
7 |
Correct |
3 ms |
6736 KB |
Output is correct |
8 |
Correct |
3 ms |
6736 KB |
Output is correct |
9 |
Correct |
3 ms |
6916 KB |
Output is correct |
10 |
Correct |
29 ms |
13200 KB |
Output is correct |
11 |
Correct |
36 ms |
12880 KB |
Output is correct |
12 |
Correct |
40 ms |
12368 KB |
Output is correct |
13 |
Correct |
43 ms |
12548 KB |
Output is correct |
14 |
Correct |
37 ms |
12368 KB |
Output is correct |
15 |
Correct |
27 ms |
11256 KB |
Output is correct |
16 |
Correct |
27 ms |
11088 KB |
Output is correct |
17 |
Correct |
48 ms |
13104 KB |
Output is correct |
18 |
Correct |
2 ms |
6736 KB |
Output is correct |
19 |
Correct |
2 ms |
6736 KB |
Output is correct |
20 |
Correct |
3 ms |
6736 KB |
Output is correct |
21 |
Correct |
2 ms |
6736 KB |
Output is correct |
22 |
Correct |
2 ms |
6736 KB |
Output is correct |
23 |
Correct |
8 ms |
6736 KB |
Output is correct |
24 |
Correct |
9 ms |
6736 KB |
Output is correct |
25 |
Correct |
17 ms |
6736 KB |
Output is correct |
26 |
Correct |
16 ms |
6736 KB |
Output is correct |
27 |
Correct |
39 ms |
13176 KB |
Output is correct |
28 |
Correct |
58 ms |
12548 KB |
Output is correct |
29 |
Correct |
65 ms |
12368 KB |
Output is correct |
30 |
Correct |
54 ms |
12620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
13180 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
2 ms |
6736 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
4 ms |
6736 KB |
Output is correct |
6 |
Correct |
4 ms |
6736 KB |
Output is correct |
7 |
Correct |
4 ms |
6736 KB |
Output is correct |
8 |
Correct |
54 ms |
13136 KB |
Output is correct |
9 |
Correct |
53 ms |
13184 KB |
Output is correct |
10 |
Correct |
48 ms |
13136 KB |
Output is correct |
11 |
Correct |
62 ms |
13128 KB |
Output is correct |
12 |
Correct |
2 ms |
6736 KB |
Output is correct |
13 |
Correct |
2 ms |
6736 KB |
Output is correct |
14 |
Correct |
2 ms |
6736 KB |
Output is correct |
15 |
Correct |
2 ms |
6736 KB |
Output is correct |
16 |
Correct |
2 ms |
6736 KB |
Output is correct |
17 |
Correct |
13 ms |
6736 KB |
Output is correct |
18 |
Correct |
16 ms |
6736 KB |
Output is correct |
19 |
Correct |
54 ms |
13136 KB |
Output is correct |
20 |
Correct |
77 ms |
12808 KB |
Output is correct |
21 |
Correct |
59 ms |
13132 KB |
Output is correct |
22 |
Correct |
56 ms |
12368 KB |
Output is correct |
23 |
Correct |
52 ms |
11256 KB |
Output is correct |
24 |
Correct |
37 ms |
10064 KB |
Output is correct |
25 |
Correct |
41 ms |
11088 KB |
Output is correct |
26 |
Correct |
40 ms |
11092 KB |
Output is correct |
27 |
Correct |
2 ms |
6908 KB |
Output is correct |
28 |
Correct |
2 ms |
6592 KB |
Output is correct |
29 |
Correct |
2 ms |
6736 KB |
Output is correct |
30 |
Correct |
2 ms |
6736 KB |
Output is correct |
31 |
Correct |
2 ms |
6736 KB |
Output is correct |
32 |
Correct |
3 ms |
6736 KB |
Output is correct |
33 |
Correct |
3 ms |
6736 KB |
Output is correct |
34 |
Correct |
3 ms |
6736 KB |
Output is correct |
35 |
Correct |
3 ms |
6916 KB |
Output is correct |
36 |
Correct |
29 ms |
13200 KB |
Output is correct |
37 |
Correct |
36 ms |
12880 KB |
Output is correct |
38 |
Correct |
40 ms |
12368 KB |
Output is correct |
39 |
Correct |
43 ms |
12548 KB |
Output is correct |
40 |
Correct |
37 ms |
12368 KB |
Output is correct |
41 |
Correct |
27 ms |
11256 KB |
Output is correct |
42 |
Correct |
27 ms |
11088 KB |
Output is correct |
43 |
Correct |
48 ms |
13104 KB |
Output is correct |
44 |
Correct |
2 ms |
6736 KB |
Output is correct |
45 |
Correct |
2 ms |
6736 KB |
Output is correct |
46 |
Correct |
3 ms |
6736 KB |
Output is correct |
47 |
Correct |
2 ms |
6736 KB |
Output is correct |
48 |
Correct |
2 ms |
6736 KB |
Output is correct |
49 |
Correct |
8 ms |
6736 KB |
Output is correct |
50 |
Correct |
9 ms |
6736 KB |
Output is correct |
51 |
Correct |
17 ms |
6736 KB |
Output is correct |
52 |
Correct |
16 ms |
6736 KB |
Output is correct |
53 |
Correct |
39 ms |
13176 KB |
Output is correct |
54 |
Correct |
58 ms |
12548 KB |
Output is correct |
55 |
Correct |
65 ms |
12368 KB |
Output is correct |
56 |
Correct |
54 ms |
12620 KB |
Output is correct |
57 |
Correct |
142 ms |
14408 KB |
Output is correct |
58 |
Correct |
68 ms |
13184 KB |
Output is correct |
59 |
Correct |
82 ms |
13128 KB |
Output is correct |