#include <bits/stdc++.h>
#define forn(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define rep(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define ll long long
#define pb push_back
using namespace std;
template<class X, class Y> bool ckmin(X &x, Y y) {
if (x > y) {
x = y; return true;
}
return false;
}
template<class X, class Y> bool ckmax(X &x, Y y) {
if (x < y) {
x = y; return true;
}
return false;
}
const int N = 5e4 + 25;
const ll linf = 1e16;
const int inf = (int)1e9 + 25;
/////////////////////////////////////////////////
const int LOG = 16;
int k, n, m, o, dp[50000][LOG + 1][5][5], ans[5][5], tmp[5][5];
void sol() {
cin >> k >> n >> m >> o;
memset(dp, 0x3f, sizeof dp);
forn(i, 1, m) {
int u, v, w;
cin >> u >> v >> w;
dp[u / k][0][u % k][v % k] = w;
}
forn(j, 1, LOG) for(int i=0; i+(1<<j)<(n+k-1)/k; ++i) {
rep(a, 0, k) rep(b, 0, k) rep(c, 0, k) {
ckmin(dp[i][j][a][b], dp[i][j - 1][a][c] + dp[i + (1 << (j - 1))][j - 1][c][b]);
}
}
while (o--) {
int u, v;
cin >> u >> v;
int cur = u / k, dest = v / k;
memset(ans, 0x3f, sizeof ans);
rep(i, 0, k) ans[i][i] = 0;
for(int i=LOG; i>=0; --i) if (cur + (1 << i) <= dest) {
memset(tmp, 0x3f, sizeof tmp);
rep(a, 0, k) rep(b, 0, k) rep(c, 0, k) {
ckmin(tmp[a][b], ans[a][c] + dp[cur][i][c][b]);
}
memcpy(ans, tmp, sizeof ans);
cur += (1 << i);
}
cout << (ans[u % k][v % k] == 0x3f3f3f3f ? -1 : ans[u % k][v % k]) << '\n';
}
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
#ifdef DHT
freopen("hehe.inp", "r", stdin);
#else
//freopen("banhkhuc.inp", "r", stdin); freopen("banhkhuc.out", "w", stdout);
#endif
int t = 1;
// cin >> t;
forn(tt, 1, t) sol();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
83532 KB |
Output is correct |
2 |
Correct |
39 ms |
83472 KB |
Output is correct |
3 |
Correct |
33 ms |
83380 KB |
Output is correct |
4 |
Correct |
31 ms |
83452 KB |
Output is correct |
5 |
Correct |
32 ms |
83388 KB |
Output is correct |
6 |
Correct |
32 ms |
83404 KB |
Output is correct |
7 |
Correct |
37 ms |
83484 KB |
Output is correct |
8 |
Correct |
67 ms |
83604 KB |
Output is correct |
9 |
Correct |
62 ms |
83472 KB |
Output is correct |
10 |
Correct |
47 ms |
83400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
83500 KB |
Output is correct |
2 |
Correct |
32 ms |
83448 KB |
Output is correct |
3 |
Correct |
37 ms |
83412 KB |
Output is correct |
4 |
Correct |
37 ms |
83412 KB |
Output is correct |
5 |
Correct |
35 ms |
83404 KB |
Output is correct |
6 |
Correct |
32 ms |
83348 KB |
Output is correct |
7 |
Correct |
36 ms |
83500 KB |
Output is correct |
8 |
Correct |
44 ms |
83532 KB |
Output is correct |
9 |
Correct |
65 ms |
83484 KB |
Output is correct |
10 |
Correct |
97 ms |
83532 KB |
Output is correct |
11 |
Correct |
83 ms |
83564 KB |
Output is correct |
12 |
Correct |
89 ms |
83512 KB |
Output is correct |
13 |
Correct |
90 ms |
83412 KB |
Output is correct |
14 |
Correct |
65 ms |
83480 KB |
Output is correct |
15 |
Correct |
88 ms |
83592 KB |
Output is correct |
16 |
Correct |
76 ms |
83476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
83396 KB |
Output is correct |
2 |
Correct |
35 ms |
83432 KB |
Output is correct |
3 |
Correct |
32 ms |
83400 KB |
Output is correct |
4 |
Correct |
34 ms |
83424 KB |
Output is correct |
5 |
Correct |
32 ms |
83360 KB |
Output is correct |
6 |
Correct |
33 ms |
83400 KB |
Output is correct |
7 |
Correct |
32 ms |
83420 KB |
Output is correct |
8 |
Correct |
33 ms |
83396 KB |
Output is correct |
9 |
Correct |
32 ms |
83468 KB |
Output is correct |
10 |
Correct |
59 ms |
83488 KB |
Output is correct |
11 |
Correct |
86 ms |
83472 KB |
Output is correct |
12 |
Correct |
88 ms |
83380 KB |
Output is correct |
13 |
Correct |
92 ms |
83472 KB |
Output is correct |
14 |
Correct |
113 ms |
83352 KB |
Output is correct |
15 |
Correct |
81 ms |
83412 KB |
Output is correct |
16 |
Correct |
74 ms |
83384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
83396 KB |
Output is correct |
2 |
Correct |
35 ms |
83432 KB |
Output is correct |
3 |
Correct |
32 ms |
83400 KB |
Output is correct |
4 |
Correct |
34 ms |
83424 KB |
Output is correct |
5 |
Correct |
32 ms |
83360 KB |
Output is correct |
6 |
Correct |
33 ms |
83400 KB |
Output is correct |
7 |
Correct |
32 ms |
83420 KB |
Output is correct |
8 |
Correct |
33 ms |
83396 KB |
Output is correct |
9 |
Correct |
32 ms |
83468 KB |
Output is correct |
10 |
Correct |
59 ms |
83488 KB |
Output is correct |
11 |
Correct |
86 ms |
83472 KB |
Output is correct |
12 |
Correct |
88 ms |
83380 KB |
Output is correct |
13 |
Correct |
92 ms |
83472 KB |
Output is correct |
14 |
Correct |
113 ms |
83352 KB |
Output is correct |
15 |
Correct |
81 ms |
83412 KB |
Output is correct |
16 |
Correct |
74 ms |
83384 KB |
Output is correct |
17 |
Correct |
82 ms |
83468 KB |
Output is correct |
18 |
Correct |
31 ms |
83404 KB |
Output is correct |
19 |
Correct |
30 ms |
83448 KB |
Output is correct |
20 |
Correct |
40 ms |
83360 KB |
Output is correct |
21 |
Correct |
42 ms |
83460 KB |
Output is correct |
22 |
Correct |
38 ms |
83416 KB |
Output is correct |
23 |
Correct |
34 ms |
83412 KB |
Output is correct |
24 |
Correct |
34 ms |
83492 KB |
Output is correct |
25 |
Correct |
44 ms |
83456 KB |
Output is correct |
26 |
Correct |
35 ms |
83404 KB |
Output is correct |
27 |
Correct |
58 ms |
83472 KB |
Output is correct |
28 |
Correct |
94 ms |
83412 KB |
Output is correct |
29 |
Correct |
94 ms |
83396 KB |
Output is correct |
30 |
Correct |
88 ms |
83472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
83532 KB |
Output is correct |
2 |
Correct |
39 ms |
83472 KB |
Output is correct |
3 |
Correct |
33 ms |
83380 KB |
Output is correct |
4 |
Correct |
31 ms |
83452 KB |
Output is correct |
5 |
Correct |
32 ms |
83388 KB |
Output is correct |
6 |
Correct |
32 ms |
83404 KB |
Output is correct |
7 |
Correct |
37 ms |
83484 KB |
Output is correct |
8 |
Correct |
67 ms |
83604 KB |
Output is correct |
9 |
Correct |
62 ms |
83472 KB |
Output is correct |
10 |
Correct |
47 ms |
83400 KB |
Output is correct |
11 |
Correct |
86 ms |
83500 KB |
Output is correct |
12 |
Correct |
32 ms |
83448 KB |
Output is correct |
13 |
Correct |
37 ms |
83412 KB |
Output is correct |
14 |
Correct |
37 ms |
83412 KB |
Output is correct |
15 |
Correct |
35 ms |
83404 KB |
Output is correct |
16 |
Correct |
32 ms |
83348 KB |
Output is correct |
17 |
Correct |
36 ms |
83500 KB |
Output is correct |
18 |
Correct |
44 ms |
83532 KB |
Output is correct |
19 |
Correct |
65 ms |
83484 KB |
Output is correct |
20 |
Correct |
97 ms |
83532 KB |
Output is correct |
21 |
Correct |
83 ms |
83564 KB |
Output is correct |
22 |
Correct |
89 ms |
83512 KB |
Output is correct |
23 |
Correct |
90 ms |
83412 KB |
Output is correct |
24 |
Correct |
65 ms |
83480 KB |
Output is correct |
25 |
Correct |
88 ms |
83592 KB |
Output is correct |
26 |
Correct |
76 ms |
83476 KB |
Output is correct |
27 |
Correct |
37 ms |
83396 KB |
Output is correct |
28 |
Correct |
35 ms |
83432 KB |
Output is correct |
29 |
Correct |
32 ms |
83400 KB |
Output is correct |
30 |
Correct |
34 ms |
83424 KB |
Output is correct |
31 |
Correct |
32 ms |
83360 KB |
Output is correct |
32 |
Correct |
33 ms |
83400 KB |
Output is correct |
33 |
Correct |
32 ms |
83420 KB |
Output is correct |
34 |
Correct |
33 ms |
83396 KB |
Output is correct |
35 |
Correct |
32 ms |
83468 KB |
Output is correct |
36 |
Correct |
59 ms |
83488 KB |
Output is correct |
37 |
Correct |
86 ms |
83472 KB |
Output is correct |
38 |
Correct |
88 ms |
83380 KB |
Output is correct |
39 |
Correct |
92 ms |
83472 KB |
Output is correct |
40 |
Correct |
113 ms |
83352 KB |
Output is correct |
41 |
Correct |
81 ms |
83412 KB |
Output is correct |
42 |
Correct |
74 ms |
83384 KB |
Output is correct |
43 |
Correct |
82 ms |
83468 KB |
Output is correct |
44 |
Correct |
31 ms |
83404 KB |
Output is correct |
45 |
Correct |
30 ms |
83448 KB |
Output is correct |
46 |
Correct |
40 ms |
83360 KB |
Output is correct |
47 |
Correct |
42 ms |
83460 KB |
Output is correct |
48 |
Correct |
38 ms |
83416 KB |
Output is correct |
49 |
Correct |
34 ms |
83412 KB |
Output is correct |
50 |
Correct |
34 ms |
83492 KB |
Output is correct |
51 |
Correct |
44 ms |
83456 KB |
Output is correct |
52 |
Correct |
35 ms |
83404 KB |
Output is correct |
53 |
Correct |
58 ms |
83472 KB |
Output is correct |
54 |
Correct |
94 ms |
83412 KB |
Output is correct |
55 |
Correct |
94 ms |
83396 KB |
Output is correct |
56 |
Correct |
88 ms |
83472 KB |
Output is correct |
57 |
Correct |
123 ms |
83492 KB |
Output is correct |
58 |
Correct |
65 ms |
83476 KB |
Output is correct |
59 |
Correct |
91 ms |
83472 KB |
Output is correct |