#include <iostream>
#include <vector>
#include <algorithm>
#define int int64_t
using namespace std;
int k;
struct block
{
vector<vector<int>> bl;
block() : bl(vector<vector<int>>(k, vector<int>(k, 1e11))) {}
static block nt()
{
block b;
for (int i = 0; i < k; i++) b.bl[i][i] = 0;
return b;
}
static block merge(const block& b1, const block& b2)
{
block res;
for (int i = 0; i < k; i++)
{
for (int j = 0; j < k; j++)
{
for (int l = 0; l < k; l++)
{
res.bl[i][j] = min(res.bl[i][j], b1.bl[i][l] + b2.bl[l][j]);
}
}
}
return res;
}
void print()
{
return;
for (int i = 0; i < k; i++)
{
for (int j = 0; j < k; j++)
{
cout << bl[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
}
};
struct segtree
{
vector<block> table;
block query(int ql, int qr, int l, int r, int pos)
{
if (ql <= l && r <= qr) return table[pos];
if (qr < l || r < ql) return block::nt();
return block::merge(query(ql, qr, l, (l + r)/2, 2*pos + 1), query(ql, qr, (l + r)/2 + 1, r, 2*pos + 2));
}
void build(const vector<block>& vals, int l, int r, int pos)
{
if (l == r)
{
table[pos] = vals[l];
return;
}
build(vals, l, (l + r)/2, 2*pos + 1);
build(vals, (l + r)/2 + 1, r, 2*pos + 2);
table[pos] = block::merge(table[2*pos + 1], table[2*pos + 2]);
}
};
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, o;
cin >> k >> n >> m >> o;
n += k - (k % n);
int nbl = (n / k);
vector<block> bls(nbl);
for (int i = 0; i < m; i++)
{
int a, b, w;
cin >> a >> b >> w;
int cb = a / k;
a %= k;
b %= k;
bls[cb].bl[a][b] = min(bls[cb].bl[a][b], w);
}
segtree st;
st.table.resize(4*nbl);
st.build(bls, 0, nbl - 1, 0);
for (auto bl : bls)
{
bl.print();
}
for (int i = 0; i < o; i++)
{
int fr, to;
cin >> fr >> to;
if ((fr / k) == (to / k))
{
cout << "-1\n";
continue;
}
auto bl = st.query((fr / k), (to / k) - 1, 0, nbl - 1, 0);
bl.print();
int v = bl.bl[fr % k][to % k];
cout << (v >= 1e11 ? -1 : v) << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
22904 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
10 ms |
768 KB |
Output is correct |
6 |
Correct |
9 ms |
768 KB |
Output is correct |
7 |
Correct |
10 ms |
768 KB |
Output is correct |
8 |
Correct |
147 ms |
22776 KB |
Output is correct |
9 |
Correct |
143 ms |
22776 KB |
Output is correct |
10 |
Correct |
123 ms |
22008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
20624 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
29 ms |
896 KB |
Output is correct |
8 |
Correct |
35 ms |
896 KB |
Output is correct |
9 |
Correct |
109 ms |
22776 KB |
Output is correct |
10 |
Correct |
146 ms |
18936 KB |
Output is correct |
11 |
Correct |
129 ms |
20728 KB |
Output is correct |
12 |
Correct |
124 ms |
18040 KB |
Output is correct |
13 |
Correct |
109 ms |
14328 KB |
Output is correct |
14 |
Correct |
72 ms |
11640 KB |
Output is correct |
15 |
Correct |
84 ms |
13064 KB |
Output is correct |
16 |
Correct |
81 ms |
13176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
768 KB |
Output is correct |
7 |
Correct |
6 ms |
768 KB |
Output is correct |
8 |
Correct |
7 ms |
768 KB |
Output is correct |
9 |
Correct |
7 ms |
768 KB |
Output is correct |
10 |
Correct |
68 ms |
22648 KB |
Output is correct |
11 |
Correct |
78 ms |
20472 KB |
Output is correct |
12 |
Correct |
84 ms |
18808 KB |
Output is correct |
13 |
Correct |
89 ms |
19064 KB |
Output is correct |
14 |
Correct |
82 ms |
18424 KB |
Output is correct |
15 |
Correct |
50 ms |
13048 KB |
Output is correct |
16 |
Correct |
51 ms |
13048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
768 KB |
Output is correct |
7 |
Correct |
6 ms |
768 KB |
Output is correct |
8 |
Correct |
7 ms |
768 KB |
Output is correct |
9 |
Correct |
7 ms |
768 KB |
Output is correct |
10 |
Correct |
68 ms |
22648 KB |
Output is correct |
11 |
Correct |
78 ms |
20472 KB |
Output is correct |
12 |
Correct |
84 ms |
18808 KB |
Output is correct |
13 |
Correct |
89 ms |
19064 KB |
Output is correct |
14 |
Correct |
82 ms |
18424 KB |
Output is correct |
15 |
Correct |
50 ms |
13048 KB |
Output is correct |
16 |
Correct |
51 ms |
13048 KB |
Output is correct |
17 |
Correct |
97 ms |
20600 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
20 ms |
1024 KB |
Output is correct |
24 |
Correct |
21 ms |
768 KB |
Output is correct |
25 |
Correct |
34 ms |
768 KB |
Output is correct |
26 |
Correct |
30 ms |
888 KB |
Output is correct |
27 |
Correct |
92 ms |
22776 KB |
Output is correct |
28 |
Correct |
116 ms |
18936 KB |
Output is correct |
29 |
Correct |
135 ms |
19336 KB |
Output is correct |
30 |
Correct |
112 ms |
18552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
22904 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
10 ms |
768 KB |
Output is correct |
6 |
Correct |
9 ms |
768 KB |
Output is correct |
7 |
Correct |
10 ms |
768 KB |
Output is correct |
8 |
Correct |
147 ms |
22776 KB |
Output is correct |
9 |
Correct |
143 ms |
22776 KB |
Output is correct |
10 |
Correct |
123 ms |
22008 KB |
Output is correct |
11 |
Correct |
135 ms |
20624 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
29 ms |
896 KB |
Output is correct |
18 |
Correct |
35 ms |
896 KB |
Output is correct |
19 |
Correct |
109 ms |
22776 KB |
Output is correct |
20 |
Correct |
146 ms |
18936 KB |
Output is correct |
21 |
Correct |
129 ms |
20728 KB |
Output is correct |
22 |
Correct |
124 ms |
18040 KB |
Output is correct |
23 |
Correct |
109 ms |
14328 KB |
Output is correct |
24 |
Correct |
72 ms |
11640 KB |
Output is correct |
25 |
Correct |
84 ms |
13064 KB |
Output is correct |
26 |
Correct |
81 ms |
13176 KB |
Output is correct |
27 |
Correct |
4 ms |
384 KB |
Output is correct |
28 |
Correct |
4 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
768 KB |
Output is correct |
33 |
Correct |
6 ms |
768 KB |
Output is correct |
34 |
Correct |
7 ms |
768 KB |
Output is correct |
35 |
Correct |
7 ms |
768 KB |
Output is correct |
36 |
Correct |
68 ms |
22648 KB |
Output is correct |
37 |
Correct |
78 ms |
20472 KB |
Output is correct |
38 |
Correct |
84 ms |
18808 KB |
Output is correct |
39 |
Correct |
89 ms |
19064 KB |
Output is correct |
40 |
Correct |
82 ms |
18424 KB |
Output is correct |
41 |
Correct |
50 ms |
13048 KB |
Output is correct |
42 |
Correct |
51 ms |
13048 KB |
Output is correct |
43 |
Correct |
97 ms |
20600 KB |
Output is correct |
44 |
Correct |
5 ms |
384 KB |
Output is correct |
45 |
Correct |
4 ms |
384 KB |
Output is correct |
46 |
Correct |
4 ms |
384 KB |
Output is correct |
47 |
Correct |
5 ms |
384 KB |
Output is correct |
48 |
Correct |
4 ms |
384 KB |
Output is correct |
49 |
Correct |
20 ms |
1024 KB |
Output is correct |
50 |
Correct |
21 ms |
768 KB |
Output is correct |
51 |
Correct |
34 ms |
768 KB |
Output is correct |
52 |
Correct |
30 ms |
888 KB |
Output is correct |
53 |
Correct |
92 ms |
22776 KB |
Output is correct |
54 |
Correct |
116 ms |
18936 KB |
Output is correct |
55 |
Correct |
135 ms |
19336 KB |
Output is correct |
56 |
Correct |
112 ms |
18552 KB |
Output is correct |
57 |
Correct |
265 ms |
23800 KB |
Output is correct |
58 |
Correct |
146 ms |
22776 KB |
Output is correct |
59 |
Correct |
166 ms |
20780 KB |
Output is correct |