#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l; i>=(r); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define fi first
#define se second
const int INF = 1e9;
const int N = 50005;
const int LOG = 17;
using dt = pair<ii, int>;
int k, n, m, o, ret[N];
int f[5][N], g[5][N];
vector<ii> go[N], back[N];
vector<dt> querys;
void divide(int lx, int rx, vector<dt> &ask) {
if (ask.empty()) return;
int mx = (lx+rx)/2;
int st = lx*k, en = min(n, (rx+1)*k);
for(int t=0; t<k; ++t) {
for(int i=st; i<en; ++i) {
f[t][i] = g[t][i] = INF;
}
}
int l = mx*k, r = min(n, l+k);
for(int m=l; m<r; ++m) {
f[m-l][m] = g[m-l][m] = 0;
for(int i=m; i>st; --i) {
for(ii x : back[i]) {
f[m-l][x.fi] = min(f[m-l][x.fi], f[m-l][i] + x.se);
}
}
for(int i=m; i<en-1; ++i) {
for(ii x : go[i]) {
g[m-l][x.fi] = min(g[m-l][x.fi], g[m-l][i] + x.se);
}
}
}
vector<dt> _left, _right;
for(auto x : ask) {
if (x.fi.se/k < mx) _left.emplace_back(x);
else if (x.fi.fi/k > mx) _right.emplace_back(x);
else {
for(int m=l; m<r; ++m) {
ret[x.se] = min(ret[x.se], f[m-l][x.fi.fi] + g[m-l][x.fi.se]);
}
}
}
divide(lx, mx-1, _left);
divide(mx+1, rx, _right);
}
void solve() {
cin >> k >> n >> m >> o;
for(int i=1; i<=m; ++i) {
int u, v, t;
cin >> u >> v >> t;
go[u].emplace_back(v, t);
back[v].emplace_back(u, t);
}
for(int i=1; i<=o; ++i) {
int u, v;
cin >> u >> v;
querys.push_back({{u, v}, i});
}
memset(ret, 0x3f, sizeof(ret));
divide(0, (n-1)/k, querys);
for(int i=1; i<=o; ++i) {
if (ret[i] < INF) cout << ret[i] << "\n";
else cout << "-1\n";
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen("input.inp", "r")) {
freopen("input.inp", "r", stdin);
freopen("input.out", "w", stdout);
}
solve();
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:100:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | freopen("input.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:101:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | freopen("input.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
6680 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
1 ms |
2772 KB |
Output is correct |
4 |
Correct |
1 ms |
2808 KB |
Output is correct |
5 |
Correct |
2 ms |
2900 KB |
Output is correct |
6 |
Correct |
2 ms |
2956 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
18 ms |
7516 KB |
Output is correct |
9 |
Correct |
18 ms |
7320 KB |
Output is correct |
10 |
Correct |
4 ms |
3660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
7056 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
2 ms |
2804 KB |
Output is correct |
5 |
Correct |
2 ms |
2808 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
3 ms |
3224 KB |
Output is correct |
8 |
Correct |
4 ms |
3344 KB |
Output is correct |
9 |
Correct |
17 ms |
7508 KB |
Output is correct |
10 |
Correct |
47 ms |
11480 KB |
Output is correct |
11 |
Correct |
30 ms |
8736 KB |
Output is correct |
12 |
Correct |
26 ms |
8540 KB |
Output is correct |
13 |
Correct |
51 ms |
11908 KB |
Output is correct |
14 |
Correct |
30 ms |
8352 KB |
Output is correct |
15 |
Correct |
25 ms |
8004 KB |
Output is correct |
16 |
Correct |
31 ms |
7900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2804 KB |
Output is correct |
3 |
Correct |
1 ms |
2772 KB |
Output is correct |
4 |
Correct |
1 ms |
2900 KB |
Output is correct |
5 |
Correct |
2 ms |
2900 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
3 ms |
3156 KB |
Output is correct |
9 |
Correct |
2 ms |
3080 KB |
Output is correct |
10 |
Correct |
15 ms |
7116 KB |
Output is correct |
11 |
Correct |
30 ms |
8220 KB |
Output is correct |
12 |
Correct |
45 ms |
11020 KB |
Output is correct |
13 |
Correct |
50 ms |
11844 KB |
Output is correct |
14 |
Correct |
38 ms |
9984 KB |
Output is correct |
15 |
Correct |
28 ms |
7872 KB |
Output is correct |
16 |
Correct |
28 ms |
7772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2804 KB |
Output is correct |
3 |
Correct |
1 ms |
2772 KB |
Output is correct |
4 |
Correct |
1 ms |
2900 KB |
Output is correct |
5 |
Correct |
2 ms |
2900 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
3 ms |
3156 KB |
Output is correct |
9 |
Correct |
2 ms |
3080 KB |
Output is correct |
10 |
Correct |
15 ms |
7116 KB |
Output is correct |
11 |
Correct |
30 ms |
8220 KB |
Output is correct |
12 |
Correct |
45 ms |
11020 KB |
Output is correct |
13 |
Correct |
50 ms |
11844 KB |
Output is correct |
14 |
Correct |
38 ms |
9984 KB |
Output is correct |
15 |
Correct |
28 ms |
7872 KB |
Output is correct |
16 |
Correct |
28 ms |
7772 KB |
Output is correct |
17 |
Correct |
31 ms |
8348 KB |
Output is correct |
18 |
Correct |
1 ms |
2772 KB |
Output is correct |
19 |
Correct |
1 ms |
2772 KB |
Output is correct |
20 |
Correct |
1 ms |
2772 KB |
Output is correct |
21 |
Correct |
1 ms |
2900 KB |
Output is correct |
22 |
Correct |
2 ms |
2816 KB |
Output is correct |
23 |
Correct |
2 ms |
3028 KB |
Output is correct |
24 |
Correct |
3 ms |
3076 KB |
Output is correct |
25 |
Correct |
3 ms |
3204 KB |
Output is correct |
26 |
Correct |
3 ms |
3156 KB |
Output is correct |
27 |
Correct |
17 ms |
7252 KB |
Output is correct |
28 |
Correct |
48 ms |
11272 KB |
Output is correct |
29 |
Correct |
58 ms |
11884 KB |
Output is correct |
30 |
Correct |
41 ms |
10140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
6680 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
1 ms |
2772 KB |
Output is correct |
4 |
Correct |
1 ms |
2808 KB |
Output is correct |
5 |
Correct |
2 ms |
2900 KB |
Output is correct |
6 |
Correct |
2 ms |
2956 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
18 ms |
7516 KB |
Output is correct |
9 |
Correct |
18 ms |
7320 KB |
Output is correct |
10 |
Correct |
4 ms |
3660 KB |
Output is correct |
11 |
Correct |
37 ms |
7056 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2804 KB |
Output is correct |
15 |
Correct |
2 ms |
2808 KB |
Output is correct |
16 |
Correct |
2 ms |
2900 KB |
Output is correct |
17 |
Correct |
3 ms |
3224 KB |
Output is correct |
18 |
Correct |
4 ms |
3344 KB |
Output is correct |
19 |
Correct |
17 ms |
7508 KB |
Output is correct |
20 |
Correct |
47 ms |
11480 KB |
Output is correct |
21 |
Correct |
30 ms |
8736 KB |
Output is correct |
22 |
Correct |
26 ms |
8540 KB |
Output is correct |
23 |
Correct |
51 ms |
11908 KB |
Output is correct |
24 |
Correct |
30 ms |
8352 KB |
Output is correct |
25 |
Correct |
25 ms |
8004 KB |
Output is correct |
26 |
Correct |
31 ms |
7900 KB |
Output is correct |
27 |
Correct |
1 ms |
2772 KB |
Output is correct |
28 |
Correct |
1 ms |
2804 KB |
Output is correct |
29 |
Correct |
1 ms |
2772 KB |
Output is correct |
30 |
Correct |
1 ms |
2900 KB |
Output is correct |
31 |
Correct |
2 ms |
2900 KB |
Output is correct |
32 |
Correct |
2 ms |
2900 KB |
Output is correct |
33 |
Correct |
2 ms |
2900 KB |
Output is correct |
34 |
Correct |
3 ms |
3156 KB |
Output is correct |
35 |
Correct |
2 ms |
3080 KB |
Output is correct |
36 |
Correct |
15 ms |
7116 KB |
Output is correct |
37 |
Correct |
30 ms |
8220 KB |
Output is correct |
38 |
Correct |
45 ms |
11020 KB |
Output is correct |
39 |
Correct |
50 ms |
11844 KB |
Output is correct |
40 |
Correct |
38 ms |
9984 KB |
Output is correct |
41 |
Correct |
28 ms |
7872 KB |
Output is correct |
42 |
Correct |
28 ms |
7772 KB |
Output is correct |
43 |
Correct |
31 ms |
8348 KB |
Output is correct |
44 |
Correct |
1 ms |
2772 KB |
Output is correct |
45 |
Correct |
1 ms |
2772 KB |
Output is correct |
46 |
Correct |
1 ms |
2772 KB |
Output is correct |
47 |
Correct |
1 ms |
2900 KB |
Output is correct |
48 |
Correct |
2 ms |
2816 KB |
Output is correct |
49 |
Correct |
2 ms |
3028 KB |
Output is correct |
50 |
Correct |
3 ms |
3076 KB |
Output is correct |
51 |
Correct |
3 ms |
3204 KB |
Output is correct |
52 |
Correct |
3 ms |
3156 KB |
Output is correct |
53 |
Correct |
17 ms |
7252 KB |
Output is correct |
54 |
Correct |
48 ms |
11272 KB |
Output is correct |
55 |
Correct |
58 ms |
11884 KB |
Output is correct |
56 |
Correct |
41 ms |
10140 KB |
Output is correct |
57 |
Correct |
73 ms |
13388 KB |
Output is correct |
58 |
Correct |
20 ms |
7572 KB |
Output is correct |
59 |
Correct |
35 ms |
8816 KB |
Output is correct |