#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define read_input(file) if (fopen(file".inp", "r")) freopen(file".inp", "r", stdin);
#define file(file) freopen (file".inp", "r", stdin); freopen (file".out", "w", stdout);
const int N = 5e4, inf = 1e12;
int n, m, o, k;
struct matrix{
vector <vector<int>> d;
void init(int n, int m, int v){
d.resize(n, vector<int>(m, v));
}
};
matrix operator* (const matrix &a, const matrix &b){
matrix c;
c.init(a.d.size(), b.d[0].size(), inf);
for (int i = 0; i < (int)a.d.size(); ++i)
for (int j = 0; j < (int)b.d[0].size(); ++j)
for (int k = 0; k < (int)a.d[0].size(); ++k)
c.d[i][j] = min(c.d[i][j], a.d[i][k] + b.d[k][j]);
return c;
}
matrix M[N + 5];
matrix binlift[N + 5][17];
void solve(){
cin >> k >> n >> m >> o;
int sum = n / k;
for (int i = 0; i <= sum; ++i)
M[i].init(k, k, inf);
for (int i = 1; i <= m; ++i){
int u, v, w;
cin >> u >> v >> w;
M[u / k].d[u % k][v % k] = w;
}
for (int i = 0; i <= sum; ++i) binlift[i][0] = M[i];
for (int i = 1; i <= 16; ++i)
for (int j = 0; j <= sum - (1 << i); ++j)
binlift[j][i] = binlift[j][i - 1] * binlift[j + (1 << (i - 1))][i - 1];
while (o--){
int u, v;
cin >> u >> v;
if (u > v){
cout << "-1\n";
continue;
}
matrix nw;
nw.init(k, k, inf);
nw.d[0][u % k] = 0;
int s = u / k, t = v / k;
for (int i = 16; i >= 0; --i)
if (s + (1 << i) <= t){
nw = nw * binlift[s][i];
s += (1 << i);
}
cout << (nw.d[0][v % k] >= inf ? -1 : nw.d[0][v % k]) << "\n";
}
}
signed main(){
faster;
// file("file");
// read_input("file");
int tt = 1;
// cin >> tt;
while (tt--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
70732 KB |
Output is correct |
2 |
Correct |
5 ms |
21752 KB |
Output is correct |
3 |
Correct |
5 ms |
21584 KB |
Output is correct |
4 |
Correct |
5 ms |
21584 KB |
Output is correct |
5 |
Correct |
7 ms |
22208 KB |
Output is correct |
6 |
Correct |
7 ms |
22264 KB |
Output is correct |
7 |
Correct |
7 ms |
22096 KB |
Output is correct |
8 |
Correct |
119 ms |
70660 KB |
Output is correct |
9 |
Correct |
121 ms |
70560 KB |
Output is correct |
10 |
Correct |
109 ms |
70728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
67472 KB |
Output is correct |
2 |
Correct |
8 ms |
21752 KB |
Output is correct |
3 |
Correct |
6 ms |
21584 KB |
Output is correct |
4 |
Correct |
5 ms |
21584 KB |
Output is correct |
5 |
Correct |
6 ms |
21584 KB |
Output is correct |
6 |
Correct |
6 ms |
21584 KB |
Output is correct |
7 |
Correct |
13 ms |
22096 KB |
Output is correct |
8 |
Correct |
13 ms |
22096 KB |
Output is correct |
9 |
Correct |
123 ms |
70612 KB |
Output is correct |
10 |
Correct |
141 ms |
61836 KB |
Output is correct |
11 |
Correct |
113 ms |
67656 KB |
Output is correct |
12 |
Correct |
128 ms |
61768 KB |
Output is correct |
13 |
Correct |
94 ms |
48724 KB |
Output is correct |
14 |
Correct |
75 ms |
44608 KB |
Output is correct |
15 |
Correct |
78 ms |
48676 KB |
Output is correct |
16 |
Correct |
82 ms |
48844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
21584 KB |
Output is correct |
2 |
Correct |
6 ms |
21584 KB |
Output is correct |
3 |
Correct |
6 ms |
21584 KB |
Output is correct |
4 |
Correct |
5 ms |
21584 KB |
Output is correct |
5 |
Correct |
6 ms |
21584 KB |
Output is correct |
6 |
Correct |
7 ms |
22116 KB |
Output is correct |
7 |
Correct |
7 ms |
22096 KB |
Output is correct |
8 |
Correct |
7 ms |
22096 KB |
Output is correct |
9 |
Correct |
8 ms |
22096 KB |
Output is correct |
10 |
Correct |
113 ms |
70548 KB |
Output is correct |
11 |
Correct |
99 ms |
67404 KB |
Output is correct |
12 |
Correct |
117 ms |
61772 KB |
Output is correct |
13 |
Correct |
119 ms |
61668 KB |
Output is correct |
14 |
Correct |
111 ms |
61824 KB |
Output is correct |
15 |
Correct |
72 ms |
48716 KB |
Output is correct |
16 |
Correct |
74 ms |
48712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
21584 KB |
Output is correct |
2 |
Correct |
6 ms |
21584 KB |
Output is correct |
3 |
Correct |
6 ms |
21584 KB |
Output is correct |
4 |
Correct |
5 ms |
21584 KB |
Output is correct |
5 |
Correct |
6 ms |
21584 KB |
Output is correct |
6 |
Correct |
7 ms |
22116 KB |
Output is correct |
7 |
Correct |
7 ms |
22096 KB |
Output is correct |
8 |
Correct |
7 ms |
22096 KB |
Output is correct |
9 |
Correct |
8 ms |
22096 KB |
Output is correct |
10 |
Correct |
113 ms |
70548 KB |
Output is correct |
11 |
Correct |
99 ms |
67404 KB |
Output is correct |
12 |
Correct |
117 ms |
61772 KB |
Output is correct |
13 |
Correct |
119 ms |
61668 KB |
Output is correct |
14 |
Correct |
111 ms |
61824 KB |
Output is correct |
15 |
Correct |
72 ms |
48716 KB |
Output is correct |
16 |
Correct |
74 ms |
48712 KB |
Output is correct |
17 |
Correct |
107 ms |
67404 KB |
Output is correct |
18 |
Correct |
6 ms |
21584 KB |
Output is correct |
19 |
Correct |
6 ms |
21584 KB |
Output is correct |
20 |
Correct |
6 ms |
21584 KB |
Output is correct |
21 |
Correct |
6 ms |
21584 KB |
Output is correct |
22 |
Correct |
6 ms |
21584 KB |
Output is correct |
23 |
Correct |
9 ms |
22096 KB |
Output is correct |
24 |
Correct |
9 ms |
22096 KB |
Output is correct |
25 |
Correct |
11 ms |
22096 KB |
Output is correct |
26 |
Correct |
10 ms |
22096 KB |
Output is correct |
27 |
Correct |
113 ms |
70692 KB |
Output is correct |
28 |
Correct |
123 ms |
61768 KB |
Output is correct |
29 |
Correct |
133 ms |
61768 KB |
Output is correct |
30 |
Correct |
147 ms |
61768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
70732 KB |
Output is correct |
2 |
Correct |
5 ms |
21752 KB |
Output is correct |
3 |
Correct |
5 ms |
21584 KB |
Output is correct |
4 |
Correct |
5 ms |
21584 KB |
Output is correct |
5 |
Correct |
7 ms |
22208 KB |
Output is correct |
6 |
Correct |
7 ms |
22264 KB |
Output is correct |
7 |
Correct |
7 ms |
22096 KB |
Output is correct |
8 |
Correct |
119 ms |
70660 KB |
Output is correct |
9 |
Correct |
121 ms |
70560 KB |
Output is correct |
10 |
Correct |
109 ms |
70728 KB |
Output is correct |
11 |
Correct |
110 ms |
67472 KB |
Output is correct |
12 |
Correct |
8 ms |
21752 KB |
Output is correct |
13 |
Correct |
6 ms |
21584 KB |
Output is correct |
14 |
Correct |
5 ms |
21584 KB |
Output is correct |
15 |
Correct |
6 ms |
21584 KB |
Output is correct |
16 |
Correct |
6 ms |
21584 KB |
Output is correct |
17 |
Correct |
13 ms |
22096 KB |
Output is correct |
18 |
Correct |
13 ms |
22096 KB |
Output is correct |
19 |
Correct |
123 ms |
70612 KB |
Output is correct |
20 |
Correct |
141 ms |
61836 KB |
Output is correct |
21 |
Correct |
113 ms |
67656 KB |
Output is correct |
22 |
Correct |
128 ms |
61768 KB |
Output is correct |
23 |
Correct |
94 ms |
48724 KB |
Output is correct |
24 |
Correct |
75 ms |
44608 KB |
Output is correct |
25 |
Correct |
78 ms |
48676 KB |
Output is correct |
26 |
Correct |
82 ms |
48844 KB |
Output is correct |
27 |
Correct |
5 ms |
21584 KB |
Output is correct |
28 |
Correct |
6 ms |
21584 KB |
Output is correct |
29 |
Correct |
6 ms |
21584 KB |
Output is correct |
30 |
Correct |
5 ms |
21584 KB |
Output is correct |
31 |
Correct |
6 ms |
21584 KB |
Output is correct |
32 |
Correct |
7 ms |
22116 KB |
Output is correct |
33 |
Correct |
7 ms |
22096 KB |
Output is correct |
34 |
Correct |
7 ms |
22096 KB |
Output is correct |
35 |
Correct |
8 ms |
22096 KB |
Output is correct |
36 |
Correct |
113 ms |
70548 KB |
Output is correct |
37 |
Correct |
99 ms |
67404 KB |
Output is correct |
38 |
Correct |
117 ms |
61772 KB |
Output is correct |
39 |
Correct |
119 ms |
61668 KB |
Output is correct |
40 |
Correct |
111 ms |
61824 KB |
Output is correct |
41 |
Correct |
72 ms |
48716 KB |
Output is correct |
42 |
Correct |
74 ms |
48712 KB |
Output is correct |
43 |
Correct |
107 ms |
67404 KB |
Output is correct |
44 |
Correct |
6 ms |
21584 KB |
Output is correct |
45 |
Correct |
6 ms |
21584 KB |
Output is correct |
46 |
Correct |
6 ms |
21584 KB |
Output is correct |
47 |
Correct |
6 ms |
21584 KB |
Output is correct |
48 |
Correct |
6 ms |
21584 KB |
Output is correct |
49 |
Correct |
9 ms |
22096 KB |
Output is correct |
50 |
Correct |
9 ms |
22096 KB |
Output is correct |
51 |
Correct |
11 ms |
22096 KB |
Output is correct |
52 |
Correct |
10 ms |
22096 KB |
Output is correct |
53 |
Correct |
113 ms |
70692 KB |
Output is correct |
54 |
Correct |
123 ms |
61768 KB |
Output is correct |
55 |
Correct |
133 ms |
61768 KB |
Output is correct |
56 |
Correct |
147 ms |
61768 KB |
Output is correct |
57 |
Correct |
166 ms |
72520 KB |
Output is correct |
58 |
Correct |
139 ms |
70600 KB |
Output is correct |
59 |
Correct |
120 ms |
67508 KB |
Output is correct |