#include<bits/stdc++.h>
#define ll long long
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
//#define int int64_t
using namespace std;
typedef pair<int, int> pii;
const int N = (int)5e4 + 2;
const int inf = 1e9;
int res, k, lim, n, m, o, u, v, t;
int l[N << 2], h[N << 2];
struct TNode {
vector< vector<int> > dis;
TNode() {}
TNode(int k, int c): dis(k, vector<int>(k, c)) {}
void print() {
for(int i = 0; i < k; ++i) {
for(int j = 0; j < k; ++j) {
cout << (dis[i][j] == inf? -1: dis[i][j]) << ' ';
}
cout << '\n';
}
}
} st[N << 2], cost[N];
TNode ans;
bool minimize(int& a, int b) {
if(a <= b) return 0;
a = b; return 1;
}
TNode combine(const TNode& lef, const TNode& rig) {
if(lef.dis[0][0] == -1) return rig;
if(rig.dis[0][0] == -1) return lef;
TNode res = TNode(k, inf);
for(int i = 0; i < k; ++i) {
for(int j = 0; j < k; ++j) {
for(int l = 0; l < k; ++l) {
minimize(res.dis[i][j], lef.dis[i][l] + rig.dis[l][j]);
}
}
}
return res;
}
void build(int x, int low, int high) {
l[x] = low, h[x] = high, st[x] = TNode(k, inf);
if(l[x] == h[x]) {
st[x] = cost[low];
return;
}
int mid = (low + high) >> 1;
build(x << 1, low, mid);
build(x << 1 | 1, mid + 1, high);
st[x] = combine(st[x << 1], st[x << 1 | 1]);
}
TNode get(int x, int low, int high) {
if(l[x] > high || h[x] < low) return TNode(k, -1);
if(l[x] >= low && h[x] <= high) return st[x];
return combine(get(x << 1, low, high), get(x << 1 | 1, low, high));
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define FileName "test"
if(fopen(FileName".inp", "r")) {
freopen(FileName".inp", "r", stdin);
freopen(FileName".out", "w", stdout);
}
cin >> k >> n >> m >> o; lim = n / k + 1;
for(int i = 0; i <= lim; ++i) cost[i] = TNode(k, inf);
for(int i = 1; i <= m; ++i) {
cin >> u >> v >> t;
minimize(cost[u / k].dis[u % k][v % k], t);
}
build(1, 0, lim);
while(o --) {
cin >> u >> v;
if(u / k == v / k) {cout << "-1\n"; continue;}
ans = get(1, u / k, v / k - 1);
res = ans.dis[u % k][v % k];
if(res == inf) cout << "-1\n";
else cout << res << '\n';
}
}
Compilation message
toll.cpp: In function 'int32_t main()':
toll.cpp:74:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FileName".inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:75:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FileName".out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
17656 KB |
Output is correct |
2 |
Correct |
8 ms |
6268 KB |
Output is correct |
3 |
Correct |
9 ms |
6264 KB |
Output is correct |
4 |
Correct |
8 ms |
6264 KB |
Output is correct |
5 |
Correct |
13 ms |
6396 KB |
Output is correct |
6 |
Correct |
13 ms |
6520 KB |
Output is correct |
7 |
Correct |
13 ms |
6392 KB |
Output is correct |
8 |
Correct |
143 ms |
17528 KB |
Output is correct |
9 |
Correct |
134 ms |
17528 KB |
Output is correct |
10 |
Correct |
114 ms |
16864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
121 ms |
17784 KB |
Output is correct |
2 |
Correct |
9 ms |
6264 KB |
Output is correct |
3 |
Correct |
8 ms |
6264 KB |
Output is correct |
4 |
Correct |
8 ms |
6264 KB |
Output is correct |
5 |
Correct |
8 ms |
6264 KB |
Output is correct |
6 |
Correct |
8 ms |
6264 KB |
Output is correct |
7 |
Correct |
31 ms |
6520 KB |
Output is correct |
8 |
Correct |
37 ms |
6648 KB |
Output is correct |
9 |
Correct |
104 ms |
17528 KB |
Output is correct |
10 |
Correct |
151 ms |
17832 KB |
Output is correct |
11 |
Correct |
122 ms |
17912 KB |
Output is correct |
12 |
Correct |
121 ms |
16632 KB |
Output is correct |
13 |
Correct |
96 ms |
13836 KB |
Output is correct |
14 |
Correct |
70 ms |
13260 KB |
Output is correct |
15 |
Correct |
75 ms |
12664 KB |
Output is correct |
16 |
Correct |
77 ms |
12664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6264 KB |
Output is correct |
2 |
Correct |
8 ms |
6264 KB |
Output is correct |
3 |
Correct |
9 ms |
6264 KB |
Output is correct |
4 |
Correct |
8 ms |
6264 KB |
Output is correct |
5 |
Correct |
8 ms |
6264 KB |
Output is correct |
6 |
Correct |
10 ms |
6392 KB |
Output is correct |
7 |
Correct |
10 ms |
6520 KB |
Output is correct |
8 |
Correct |
11 ms |
6392 KB |
Output is correct |
9 |
Correct |
11 ms |
6392 KB |
Output is correct |
10 |
Correct |
71 ms |
17544 KB |
Output is correct |
11 |
Correct |
84 ms |
17656 KB |
Output is correct |
12 |
Correct |
89 ms |
17528 KB |
Output is correct |
13 |
Correct |
91 ms |
17788 KB |
Output is correct |
14 |
Correct |
76 ms |
17148 KB |
Output is correct |
15 |
Correct |
56 ms |
12664 KB |
Output is correct |
16 |
Correct |
52 ms |
12664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6264 KB |
Output is correct |
2 |
Correct |
8 ms |
6264 KB |
Output is correct |
3 |
Correct |
9 ms |
6264 KB |
Output is correct |
4 |
Correct |
8 ms |
6264 KB |
Output is correct |
5 |
Correct |
8 ms |
6264 KB |
Output is correct |
6 |
Correct |
10 ms |
6392 KB |
Output is correct |
7 |
Correct |
10 ms |
6520 KB |
Output is correct |
8 |
Correct |
11 ms |
6392 KB |
Output is correct |
9 |
Correct |
11 ms |
6392 KB |
Output is correct |
10 |
Correct |
71 ms |
17544 KB |
Output is correct |
11 |
Correct |
84 ms |
17656 KB |
Output is correct |
12 |
Correct |
89 ms |
17528 KB |
Output is correct |
13 |
Correct |
91 ms |
17788 KB |
Output is correct |
14 |
Correct |
76 ms |
17148 KB |
Output is correct |
15 |
Correct |
56 ms |
12664 KB |
Output is correct |
16 |
Correct |
52 ms |
12664 KB |
Output is correct |
17 |
Correct |
102 ms |
17656 KB |
Output is correct |
18 |
Correct |
8 ms |
6264 KB |
Output is correct |
19 |
Correct |
8 ms |
6264 KB |
Output is correct |
20 |
Correct |
8 ms |
6264 KB |
Output is correct |
21 |
Correct |
8 ms |
6264 KB |
Output is correct |
22 |
Correct |
8 ms |
6260 KB |
Output is correct |
23 |
Correct |
20 ms |
6648 KB |
Output is correct |
24 |
Correct |
23 ms |
6520 KB |
Output is correct |
25 |
Correct |
34 ms |
6520 KB |
Output is correct |
26 |
Correct |
30 ms |
6520 KB |
Output is correct |
27 |
Correct |
88 ms |
17528 KB |
Output is correct |
28 |
Correct |
125 ms |
17688 KB |
Output is correct |
29 |
Correct |
121 ms |
17784 KB |
Output is correct |
30 |
Correct |
102 ms |
17144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
17656 KB |
Output is correct |
2 |
Correct |
8 ms |
6268 KB |
Output is correct |
3 |
Correct |
9 ms |
6264 KB |
Output is correct |
4 |
Correct |
8 ms |
6264 KB |
Output is correct |
5 |
Correct |
13 ms |
6396 KB |
Output is correct |
6 |
Correct |
13 ms |
6520 KB |
Output is correct |
7 |
Correct |
13 ms |
6392 KB |
Output is correct |
8 |
Correct |
143 ms |
17528 KB |
Output is correct |
9 |
Correct |
134 ms |
17528 KB |
Output is correct |
10 |
Correct |
114 ms |
16864 KB |
Output is correct |
11 |
Correct |
121 ms |
17784 KB |
Output is correct |
12 |
Correct |
9 ms |
6264 KB |
Output is correct |
13 |
Correct |
8 ms |
6264 KB |
Output is correct |
14 |
Correct |
8 ms |
6264 KB |
Output is correct |
15 |
Correct |
8 ms |
6264 KB |
Output is correct |
16 |
Correct |
8 ms |
6264 KB |
Output is correct |
17 |
Correct |
31 ms |
6520 KB |
Output is correct |
18 |
Correct |
37 ms |
6648 KB |
Output is correct |
19 |
Correct |
104 ms |
17528 KB |
Output is correct |
20 |
Correct |
151 ms |
17832 KB |
Output is correct |
21 |
Correct |
122 ms |
17912 KB |
Output is correct |
22 |
Correct |
121 ms |
16632 KB |
Output is correct |
23 |
Correct |
96 ms |
13836 KB |
Output is correct |
24 |
Correct |
70 ms |
13260 KB |
Output is correct |
25 |
Correct |
75 ms |
12664 KB |
Output is correct |
26 |
Correct |
77 ms |
12664 KB |
Output is correct |
27 |
Correct |
8 ms |
6264 KB |
Output is correct |
28 |
Correct |
8 ms |
6264 KB |
Output is correct |
29 |
Correct |
9 ms |
6264 KB |
Output is correct |
30 |
Correct |
8 ms |
6264 KB |
Output is correct |
31 |
Correct |
8 ms |
6264 KB |
Output is correct |
32 |
Correct |
10 ms |
6392 KB |
Output is correct |
33 |
Correct |
10 ms |
6520 KB |
Output is correct |
34 |
Correct |
11 ms |
6392 KB |
Output is correct |
35 |
Correct |
11 ms |
6392 KB |
Output is correct |
36 |
Correct |
71 ms |
17544 KB |
Output is correct |
37 |
Correct |
84 ms |
17656 KB |
Output is correct |
38 |
Correct |
89 ms |
17528 KB |
Output is correct |
39 |
Correct |
91 ms |
17788 KB |
Output is correct |
40 |
Correct |
76 ms |
17148 KB |
Output is correct |
41 |
Correct |
56 ms |
12664 KB |
Output is correct |
42 |
Correct |
52 ms |
12664 KB |
Output is correct |
43 |
Correct |
102 ms |
17656 KB |
Output is correct |
44 |
Correct |
8 ms |
6264 KB |
Output is correct |
45 |
Correct |
8 ms |
6264 KB |
Output is correct |
46 |
Correct |
8 ms |
6264 KB |
Output is correct |
47 |
Correct |
8 ms |
6264 KB |
Output is correct |
48 |
Correct |
8 ms |
6260 KB |
Output is correct |
49 |
Correct |
20 ms |
6648 KB |
Output is correct |
50 |
Correct |
23 ms |
6520 KB |
Output is correct |
51 |
Correct |
34 ms |
6520 KB |
Output is correct |
52 |
Correct |
30 ms |
6520 KB |
Output is correct |
53 |
Correct |
88 ms |
17528 KB |
Output is correct |
54 |
Correct |
125 ms |
17688 KB |
Output is correct |
55 |
Correct |
121 ms |
17784 KB |
Output is correct |
56 |
Correct |
102 ms |
17144 KB |
Output is correct |
57 |
Correct |
256 ms |
18680 KB |
Output is correct |
58 |
Correct |
139 ms |
17656 KB |
Output is correct |
59 |
Correct |
164 ms |
17912 KB |
Output is correct |