#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 5e4 + 5;
const int oo = 1e9;
int dis[16][N][5];
int lca[16][N][5];
int n,k,m,q;
void build()
{
for (int j = 1; j < 16; j++)
for (int i = 0; i < n; i++)
{
for (int cur = 0; cur < k; cur++)
{
int p = lca[j - 1][i][cur];
if (p < 0)
continue;
for (int nxt = 0; nxt < k; nxt++)
{
int pp = lca[j - 1][p][nxt];
if (pp < 0)
continue;
lca[j][i][nxt] = pp;
dis[j][i][nxt] = min(dis[j][i][nxt], dis[j - 1][i][cur] + dis[j - 1][p][nxt]);
}
}
}
}
void jump(vector <pair <int, int>> &f, int step)
{
for (int i = 0; i < 16; i++) if ((step >> i) & 1)
{
vector <pair <int, int>> g(k, mp(oo, oo));
for (auto pir : f)
{
int u = pir.fi;
int cur = pir.se;
if (u == oo)
continue;
for (int nxt = 0; nxt < k; nxt++)
{
int v = lca[i][u][nxt];
if (v < 0)
continue;
g[nxt] = min(g[nxt], mp(v, cur + dis[i][u][nxt]));
}
}
f = g;
}
}
int solve(int s, int t)
{
int step = (t / k) - (s / k);
if (step < 0)
return -1;
vector <pair <int, int>> res(k, mp(oo, oo));
res[s % k] = mp(s, 0);
jump(res, step);
for (auto pir : res)
if (pir.fi == t)
return pir.se;
return -1;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("file.inp","r",stdin);
cin >> k >> n >> m >> q;
memset(lca, -1, sizeof(lca));
for (int j = 0; j < 16; j++)
for (int i = 0; i < n; i++)
for (int cur = 0; cur < k; cur++)
dis[j][i][cur] = oo;
for (int i = 1; i <= m; i++)
{
int u,v,w;
cin >> u >> v >> w;
lca[0][u][v % k] = v;
dis[0][u][v % k] = w;
}
build();
while (q--)
{
int u,v;
cin >> u >> v;
cout << solve(u, v) << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
32620 KB |
Output is correct |
2 |
Correct |
9 ms |
16128 KB |
Output is correct |
3 |
Correct |
9 ms |
16108 KB |
Output is correct |
4 |
Correct |
9 ms |
16108 KB |
Output is correct |
5 |
Correct |
10 ms |
16364 KB |
Output is correct |
6 |
Correct |
10 ms |
16364 KB |
Output is correct |
7 |
Correct |
10 ms |
16384 KB |
Output is correct |
8 |
Correct |
52 ms |
32620 KB |
Output is correct |
9 |
Correct |
43 ms |
32620 KB |
Output is correct |
10 |
Correct |
26 ms |
31852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
33388 KB |
Output is correct |
2 |
Correct |
9 ms |
16108 KB |
Output is correct |
3 |
Correct |
10 ms |
16108 KB |
Output is correct |
4 |
Correct |
11 ms |
16108 KB |
Output is correct |
5 |
Correct |
9 ms |
16108 KB |
Output is correct |
6 |
Correct |
11 ms |
16108 KB |
Output is correct |
7 |
Correct |
15 ms |
16492 KB |
Output is correct |
8 |
Correct |
19 ms |
16768 KB |
Output is correct |
9 |
Correct |
47 ms |
32620 KB |
Output is correct |
10 |
Correct |
90 ms |
34028 KB |
Output is correct |
11 |
Correct |
66 ms |
33388 KB |
Output is correct |
12 |
Correct |
62 ms |
33004 KB |
Output is correct |
13 |
Correct |
82 ms |
27884 KB |
Output is correct |
14 |
Correct |
50 ms |
26988 KB |
Output is correct |
15 |
Correct |
61 ms |
26732 KB |
Output is correct |
16 |
Correct |
63 ms |
26732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16108 KB |
Output is correct |
2 |
Correct |
9 ms |
16108 KB |
Output is correct |
3 |
Correct |
10 ms |
16108 KB |
Output is correct |
4 |
Correct |
10 ms |
16124 KB |
Output is correct |
5 |
Correct |
10 ms |
16108 KB |
Output is correct |
6 |
Correct |
10 ms |
16364 KB |
Output is correct |
7 |
Correct |
10 ms |
16364 KB |
Output is correct |
8 |
Correct |
11 ms |
16492 KB |
Output is correct |
9 |
Correct |
11 ms |
16492 KB |
Output is correct |
10 |
Correct |
38 ms |
32492 KB |
Output is correct |
11 |
Correct |
68 ms |
33260 KB |
Output is correct |
12 |
Correct |
74 ms |
33900 KB |
Output is correct |
13 |
Correct |
79 ms |
34028 KB |
Output is correct |
14 |
Correct |
70 ms |
33516 KB |
Output is correct |
15 |
Correct |
61 ms |
26604 KB |
Output is correct |
16 |
Correct |
71 ms |
26604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16108 KB |
Output is correct |
2 |
Correct |
9 ms |
16108 KB |
Output is correct |
3 |
Correct |
10 ms |
16108 KB |
Output is correct |
4 |
Correct |
10 ms |
16124 KB |
Output is correct |
5 |
Correct |
10 ms |
16108 KB |
Output is correct |
6 |
Correct |
10 ms |
16364 KB |
Output is correct |
7 |
Correct |
10 ms |
16364 KB |
Output is correct |
8 |
Correct |
11 ms |
16492 KB |
Output is correct |
9 |
Correct |
11 ms |
16492 KB |
Output is correct |
10 |
Correct |
38 ms |
32492 KB |
Output is correct |
11 |
Correct |
68 ms |
33260 KB |
Output is correct |
12 |
Correct |
74 ms |
33900 KB |
Output is correct |
13 |
Correct |
79 ms |
34028 KB |
Output is correct |
14 |
Correct |
70 ms |
33516 KB |
Output is correct |
15 |
Correct |
61 ms |
26604 KB |
Output is correct |
16 |
Correct |
71 ms |
26604 KB |
Output is correct |
17 |
Correct |
66 ms |
33260 KB |
Output is correct |
18 |
Correct |
9 ms |
16108 KB |
Output is correct |
19 |
Correct |
9 ms |
16108 KB |
Output is correct |
20 |
Correct |
9 ms |
16108 KB |
Output is correct |
21 |
Correct |
9 ms |
16108 KB |
Output is correct |
22 |
Correct |
9 ms |
16108 KB |
Output is correct |
23 |
Correct |
12 ms |
16492 KB |
Output is correct |
24 |
Correct |
12 ms |
16492 KB |
Output is correct |
25 |
Correct |
13 ms |
16492 KB |
Output is correct |
26 |
Correct |
13 ms |
16492 KB |
Output is correct |
27 |
Correct |
44 ms |
32492 KB |
Output is correct |
28 |
Correct |
82 ms |
33900 KB |
Output is correct |
29 |
Correct |
99 ms |
34284 KB |
Output is correct |
30 |
Correct |
71 ms |
33536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
32620 KB |
Output is correct |
2 |
Correct |
9 ms |
16128 KB |
Output is correct |
3 |
Correct |
9 ms |
16108 KB |
Output is correct |
4 |
Correct |
9 ms |
16108 KB |
Output is correct |
5 |
Correct |
10 ms |
16364 KB |
Output is correct |
6 |
Correct |
10 ms |
16364 KB |
Output is correct |
7 |
Correct |
10 ms |
16384 KB |
Output is correct |
8 |
Correct |
52 ms |
32620 KB |
Output is correct |
9 |
Correct |
43 ms |
32620 KB |
Output is correct |
10 |
Correct |
26 ms |
31852 KB |
Output is correct |
11 |
Correct |
62 ms |
33388 KB |
Output is correct |
12 |
Correct |
9 ms |
16108 KB |
Output is correct |
13 |
Correct |
10 ms |
16108 KB |
Output is correct |
14 |
Correct |
11 ms |
16108 KB |
Output is correct |
15 |
Correct |
9 ms |
16108 KB |
Output is correct |
16 |
Correct |
11 ms |
16108 KB |
Output is correct |
17 |
Correct |
15 ms |
16492 KB |
Output is correct |
18 |
Correct |
19 ms |
16768 KB |
Output is correct |
19 |
Correct |
47 ms |
32620 KB |
Output is correct |
20 |
Correct |
90 ms |
34028 KB |
Output is correct |
21 |
Correct |
66 ms |
33388 KB |
Output is correct |
22 |
Correct |
62 ms |
33004 KB |
Output is correct |
23 |
Correct |
82 ms |
27884 KB |
Output is correct |
24 |
Correct |
50 ms |
26988 KB |
Output is correct |
25 |
Correct |
61 ms |
26732 KB |
Output is correct |
26 |
Correct |
63 ms |
26732 KB |
Output is correct |
27 |
Correct |
11 ms |
16108 KB |
Output is correct |
28 |
Correct |
9 ms |
16108 KB |
Output is correct |
29 |
Correct |
10 ms |
16108 KB |
Output is correct |
30 |
Correct |
10 ms |
16124 KB |
Output is correct |
31 |
Correct |
10 ms |
16108 KB |
Output is correct |
32 |
Correct |
10 ms |
16364 KB |
Output is correct |
33 |
Correct |
10 ms |
16364 KB |
Output is correct |
34 |
Correct |
11 ms |
16492 KB |
Output is correct |
35 |
Correct |
11 ms |
16492 KB |
Output is correct |
36 |
Correct |
38 ms |
32492 KB |
Output is correct |
37 |
Correct |
68 ms |
33260 KB |
Output is correct |
38 |
Correct |
74 ms |
33900 KB |
Output is correct |
39 |
Correct |
79 ms |
34028 KB |
Output is correct |
40 |
Correct |
70 ms |
33516 KB |
Output is correct |
41 |
Correct |
61 ms |
26604 KB |
Output is correct |
42 |
Correct |
71 ms |
26604 KB |
Output is correct |
43 |
Correct |
66 ms |
33260 KB |
Output is correct |
44 |
Correct |
9 ms |
16108 KB |
Output is correct |
45 |
Correct |
9 ms |
16108 KB |
Output is correct |
46 |
Correct |
9 ms |
16108 KB |
Output is correct |
47 |
Correct |
9 ms |
16108 KB |
Output is correct |
48 |
Correct |
9 ms |
16108 KB |
Output is correct |
49 |
Correct |
12 ms |
16492 KB |
Output is correct |
50 |
Correct |
12 ms |
16492 KB |
Output is correct |
51 |
Correct |
13 ms |
16492 KB |
Output is correct |
52 |
Correct |
13 ms |
16492 KB |
Output is correct |
53 |
Correct |
44 ms |
32492 KB |
Output is correct |
54 |
Correct |
82 ms |
33900 KB |
Output is correct |
55 |
Correct |
99 ms |
34284 KB |
Output is correct |
56 |
Correct |
71 ms |
33536 KB |
Output is correct |
57 |
Correct |
117 ms |
35072 KB |
Output is correct |
58 |
Correct |
50 ms |
32620 KB |
Output is correct |
59 |
Correct |
72 ms |
33516 KB |
Output is correct |