#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;
/////////////////////////////////////////////////
int k, n, m, o, ans[5], tmp[5], dp[N][17][5][5];
void sol() {
cin >> k >> n >> m >> o;
forn(i, 0, n) forn(j, 0, 16) rep(k, 0, 5) rep(l, 0, 5) dp[i][j][k][l] = inf;
forn(i, 1, m) {
int u, v, w;
cin >> u >> v >> w;
dp[u / k][0][u % k][v % k] = w;
}
rep(j, 1, 17) for(int i=0; i+(1<<j)<(n+k-1)/k; ++i) {
rep(c, 0, k) rep(a, 0, k) rep(b, 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;
bool first = true;
rep(i, 0, k) ans[i] = inf;
for(int i=16; i>=0; --i) if (cur + (1 << i) <= dest) {
if (first) {
rep(b, 0, k) ans[b] = dp[cur][i][u % k][b];
first = false;
}
else {
rep(j, 0, k) tmp[j] = inf;
rep(a, 0, k) rep(b, 0, k) ckmin(tmp[b], ans[a] + dp[cur][i][a][b]);
rep(j, 0, k) ans[j] = tmp[j];
}
cur += (1 << i);
}
if (ans[v % k] >= inf) cout << -1 << '\n';
else cout << ans[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 |
79 ms |
83624 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
2004 KB |
Output is correct |
8 |
Correct |
77 ms |
83404 KB |
Output is correct |
9 |
Correct |
73 ms |
83476 KB |
Output is correct |
10 |
Correct |
70 ms |
83408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
83476 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
4 ms |
2004 KB |
Output is correct |
8 |
Correct |
7 ms |
2004 KB |
Output is correct |
9 |
Correct |
71 ms |
83472 KB |
Output is correct |
10 |
Correct |
119 ms |
83480 KB |
Output is correct |
11 |
Correct |
92 ms |
83476 KB |
Output is correct |
12 |
Correct |
94 ms |
83480 KB |
Output is correct |
13 |
Correct |
78 ms |
50124 KB |
Output is correct |
14 |
Correct |
62 ms |
50216 KB |
Output is correct |
15 |
Correct |
57 ms |
50224 KB |
Output is correct |
16 |
Correct |
76 ms |
50208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
1876 KB |
Output is correct |
10 |
Correct |
85 ms |
83484 KB |
Output is correct |
11 |
Correct |
95 ms |
83460 KB |
Output is correct |
12 |
Correct |
99 ms |
83420 KB |
Output is correct |
13 |
Correct |
96 ms |
83472 KB |
Output is correct |
14 |
Correct |
89 ms |
83476 KB |
Output is correct |
15 |
Correct |
60 ms |
50216 KB |
Output is correct |
16 |
Correct |
59 ms |
50124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
1876 KB |
Output is correct |
10 |
Correct |
85 ms |
83484 KB |
Output is correct |
11 |
Correct |
95 ms |
83460 KB |
Output is correct |
12 |
Correct |
99 ms |
83420 KB |
Output is correct |
13 |
Correct |
96 ms |
83472 KB |
Output is correct |
14 |
Correct |
89 ms |
83476 KB |
Output is correct |
15 |
Correct |
60 ms |
50216 KB |
Output is correct |
16 |
Correct |
59 ms |
50124 KB |
Output is correct |
17 |
Correct |
87 ms |
83476 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
4 ms |
2004 KB |
Output is correct |
24 |
Correct |
3 ms |
2004 KB |
Output is correct |
25 |
Correct |
4 ms |
2004 KB |
Output is correct |
26 |
Correct |
3 ms |
1924 KB |
Output is correct |
27 |
Correct |
66 ms |
83444 KB |
Output is correct |
28 |
Correct |
97 ms |
83480 KB |
Output is correct |
29 |
Correct |
116 ms |
83484 KB |
Output is correct |
30 |
Correct |
104 ms |
83476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
83624 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
2004 KB |
Output is correct |
8 |
Correct |
77 ms |
83404 KB |
Output is correct |
9 |
Correct |
73 ms |
83476 KB |
Output is correct |
10 |
Correct |
70 ms |
83408 KB |
Output is correct |
11 |
Correct |
97 ms |
83476 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
4 ms |
2004 KB |
Output is correct |
18 |
Correct |
7 ms |
2004 KB |
Output is correct |
19 |
Correct |
71 ms |
83472 KB |
Output is correct |
20 |
Correct |
119 ms |
83480 KB |
Output is correct |
21 |
Correct |
92 ms |
83476 KB |
Output is correct |
22 |
Correct |
94 ms |
83480 KB |
Output is correct |
23 |
Correct |
78 ms |
50124 KB |
Output is correct |
24 |
Correct |
62 ms |
50216 KB |
Output is correct |
25 |
Correct |
57 ms |
50224 KB |
Output is correct |
26 |
Correct |
76 ms |
50208 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
1876 KB |
Output is correct |
33 |
Correct |
1 ms |
1876 KB |
Output is correct |
34 |
Correct |
2 ms |
1876 KB |
Output is correct |
35 |
Correct |
2 ms |
1876 KB |
Output is correct |
36 |
Correct |
85 ms |
83484 KB |
Output is correct |
37 |
Correct |
95 ms |
83460 KB |
Output is correct |
38 |
Correct |
99 ms |
83420 KB |
Output is correct |
39 |
Correct |
96 ms |
83472 KB |
Output is correct |
40 |
Correct |
89 ms |
83476 KB |
Output is correct |
41 |
Correct |
60 ms |
50216 KB |
Output is correct |
42 |
Correct |
59 ms |
50124 KB |
Output is correct |
43 |
Correct |
87 ms |
83476 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
4 ms |
2004 KB |
Output is correct |
50 |
Correct |
3 ms |
2004 KB |
Output is correct |
51 |
Correct |
4 ms |
2004 KB |
Output is correct |
52 |
Correct |
3 ms |
1924 KB |
Output is correct |
53 |
Correct |
66 ms |
83444 KB |
Output is correct |
54 |
Correct |
97 ms |
83480 KB |
Output is correct |
55 |
Correct |
116 ms |
83484 KB |
Output is correct |
56 |
Correct |
104 ms |
83476 KB |
Output is correct |
57 |
Correct |
119 ms |
83516 KB |
Output is correct |
58 |
Correct |
81 ms |
83508 KB |
Output is correct |
59 |
Correct |
107 ms |
83544 KB |
Output is correct |