#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=5e4+5, kx=16;
ll dp[nx][kx][5], sz, n, m, q, a, b, t;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>sz>>n>>m>>q;
for (int i=0; i<nx; i++) for (int j=0; j<kx; j++) for (int k=0; k<sz; k++) dp[i][j][k]=1e18;
for (int i=0; i<m; i++) cin>>a>>b>>t, dp[a][0][b%sz]=t;
for (int i=1; i<kx; i++) for (int j=0; j<n; j++) for (int k=0; k<sz; k++) for (int l=0; l<sz; l++) dp[j][i][k]=min(dp[j][i][k], dp[j][i-1][l]+dp[min(l+((j/sz)+(1<<(i-1)))*sz, (ll)nx-1)][i-1][k]);
//for (int i=0; i<n; i++) for (int j=0; j<3; j++) for (int k=0; k<sz; k++) cout<<i<<' '<<j<<' '<<k<<' '<<dp[i][j][k]<<'\n';
while (q--)
{
cin>>a>>b;
int c=a/sz, tg=b/sz;
vector<pair<ll, ll>> v;
v.push_back({0, a});
if (c==tg)
{
cout<<-1<<'\n';
continue;
}
for (int i=kx-1; i>=0; i--)
{
if (c+(1<<i)>tg) continue;
if (v.empty())
{
cout<<-1<<'\n';
break;
}
if (c+(1<<i)==tg)
{
ll res=1e18;
for (auto x:v) res=min(res, x.first+dp[x.second][i][b%sz]);
if (res==1e18) cout<<-1<<'\n';
else cout<<res<<'\n';
break;
}
vector<ll> tmp(sz, 1e18);
for (int j=0; j<sz; j++) for (auto x:v) tmp[j]=min(tmp[j], x.first+dp[x.second][i][j]);
v.clear();
for (int j=0; j<sz; j++) if (tmp[j]!=1e18) v.push_back({tmp[j], (c+(1<<i))*sz+j});
c+=(1<<i);
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
31760 KB |
Output is correct |
2 |
Correct |
5 ms |
31580 KB |
Output is correct |
3 |
Correct |
4 ms |
31580 KB |
Output is correct |
4 |
Correct |
4 ms |
31580 KB |
Output is correct |
5 |
Correct |
6 ms |
31580 KB |
Output is correct |
6 |
Correct |
5 ms |
31580 KB |
Output is correct |
7 |
Correct |
7 ms |
31580 KB |
Output is correct |
8 |
Correct |
33 ms |
31764 KB |
Output is correct |
9 |
Correct |
30 ms |
31580 KB |
Output is correct |
10 |
Correct |
17 ms |
31580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
31576 KB |
Output is correct |
2 |
Correct |
5 ms |
31580 KB |
Output is correct |
3 |
Correct |
5 ms |
31764 KB |
Output is correct |
4 |
Correct |
5 ms |
31580 KB |
Output is correct |
5 |
Correct |
5 ms |
31580 KB |
Output is correct |
6 |
Correct |
7 ms |
31580 KB |
Output is correct |
7 |
Correct |
8 ms |
31868 KB |
Output is correct |
8 |
Correct |
10 ms |
31836 KB |
Output is correct |
9 |
Correct |
29 ms |
32652 KB |
Output is correct |
10 |
Correct |
67 ms |
34000 KB |
Output is correct |
11 |
Correct |
51 ms |
33688 KB |
Output is correct |
12 |
Correct |
52 ms |
32860 KB |
Output is correct |
13 |
Correct |
71 ms |
34132 KB |
Output is correct |
14 |
Correct |
51 ms |
33116 KB |
Output is correct |
15 |
Correct |
57 ms |
32968 KB |
Output is correct |
16 |
Correct |
59 ms |
32860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31580 KB |
Output is correct |
2 |
Correct |
6 ms |
31580 KB |
Output is correct |
3 |
Correct |
5 ms |
31620 KB |
Output is correct |
4 |
Correct |
6 ms |
31580 KB |
Output is correct |
5 |
Correct |
7 ms |
31580 KB |
Output is correct |
6 |
Correct |
6 ms |
31664 KB |
Output is correct |
7 |
Correct |
6 ms |
31576 KB |
Output is correct |
8 |
Correct |
8 ms |
31672 KB |
Output is correct |
9 |
Correct |
8 ms |
31576 KB |
Output is correct |
10 |
Correct |
35 ms |
32516 KB |
Output is correct |
11 |
Correct |
52 ms |
33272 KB |
Output is correct |
12 |
Correct |
62 ms |
33800 KB |
Output is correct |
13 |
Correct |
69 ms |
34128 KB |
Output is correct |
14 |
Correct |
59 ms |
33368 KB |
Output is correct |
15 |
Correct |
57 ms |
32936 KB |
Output is correct |
16 |
Correct |
57 ms |
32848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31580 KB |
Output is correct |
2 |
Correct |
6 ms |
31580 KB |
Output is correct |
3 |
Correct |
5 ms |
31620 KB |
Output is correct |
4 |
Correct |
6 ms |
31580 KB |
Output is correct |
5 |
Correct |
7 ms |
31580 KB |
Output is correct |
6 |
Correct |
6 ms |
31664 KB |
Output is correct |
7 |
Correct |
6 ms |
31576 KB |
Output is correct |
8 |
Correct |
8 ms |
31672 KB |
Output is correct |
9 |
Correct |
8 ms |
31576 KB |
Output is correct |
10 |
Correct |
35 ms |
32516 KB |
Output is correct |
11 |
Correct |
52 ms |
33272 KB |
Output is correct |
12 |
Correct |
62 ms |
33800 KB |
Output is correct |
13 |
Correct |
69 ms |
34128 KB |
Output is correct |
14 |
Correct |
59 ms |
33368 KB |
Output is correct |
15 |
Correct |
57 ms |
32936 KB |
Output is correct |
16 |
Correct |
57 ms |
32848 KB |
Output is correct |
17 |
Correct |
54 ms |
33104 KB |
Output is correct |
18 |
Correct |
6 ms |
31696 KB |
Output is correct |
19 |
Correct |
6 ms |
31672 KB |
Output is correct |
20 |
Correct |
5 ms |
31664 KB |
Output is correct |
21 |
Correct |
5 ms |
31580 KB |
Output is correct |
22 |
Correct |
6 ms |
31696 KB |
Output is correct |
23 |
Correct |
7 ms |
31580 KB |
Output is correct |
24 |
Correct |
7 ms |
31668 KB |
Output is correct |
25 |
Correct |
10 ms |
31836 KB |
Output is correct |
26 |
Correct |
8 ms |
31832 KB |
Output is correct |
27 |
Correct |
27 ms |
33112 KB |
Output is correct |
28 |
Correct |
72 ms |
33872 KB |
Output is correct |
29 |
Correct |
72 ms |
34192 KB |
Output is correct |
30 |
Correct |
63 ms |
33364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
31760 KB |
Output is correct |
2 |
Correct |
5 ms |
31580 KB |
Output is correct |
3 |
Correct |
4 ms |
31580 KB |
Output is correct |
4 |
Correct |
4 ms |
31580 KB |
Output is correct |
5 |
Correct |
6 ms |
31580 KB |
Output is correct |
6 |
Correct |
5 ms |
31580 KB |
Output is correct |
7 |
Correct |
7 ms |
31580 KB |
Output is correct |
8 |
Correct |
33 ms |
31764 KB |
Output is correct |
9 |
Correct |
30 ms |
31580 KB |
Output is correct |
10 |
Correct |
17 ms |
31580 KB |
Output is correct |
11 |
Correct |
48 ms |
31576 KB |
Output is correct |
12 |
Correct |
5 ms |
31580 KB |
Output is correct |
13 |
Correct |
5 ms |
31764 KB |
Output is correct |
14 |
Correct |
5 ms |
31580 KB |
Output is correct |
15 |
Correct |
5 ms |
31580 KB |
Output is correct |
16 |
Correct |
7 ms |
31580 KB |
Output is correct |
17 |
Correct |
8 ms |
31868 KB |
Output is correct |
18 |
Correct |
10 ms |
31836 KB |
Output is correct |
19 |
Correct |
29 ms |
32652 KB |
Output is correct |
20 |
Correct |
67 ms |
34000 KB |
Output is correct |
21 |
Correct |
51 ms |
33688 KB |
Output is correct |
22 |
Correct |
52 ms |
32860 KB |
Output is correct |
23 |
Correct |
71 ms |
34132 KB |
Output is correct |
24 |
Correct |
51 ms |
33116 KB |
Output is correct |
25 |
Correct |
57 ms |
32968 KB |
Output is correct |
26 |
Correct |
59 ms |
32860 KB |
Output is correct |
27 |
Correct |
5 ms |
31580 KB |
Output is correct |
28 |
Correct |
6 ms |
31580 KB |
Output is correct |
29 |
Correct |
5 ms |
31620 KB |
Output is correct |
30 |
Correct |
6 ms |
31580 KB |
Output is correct |
31 |
Correct |
7 ms |
31580 KB |
Output is correct |
32 |
Correct |
6 ms |
31664 KB |
Output is correct |
33 |
Correct |
6 ms |
31576 KB |
Output is correct |
34 |
Correct |
8 ms |
31672 KB |
Output is correct |
35 |
Correct |
8 ms |
31576 KB |
Output is correct |
36 |
Correct |
35 ms |
32516 KB |
Output is correct |
37 |
Correct |
52 ms |
33272 KB |
Output is correct |
38 |
Correct |
62 ms |
33800 KB |
Output is correct |
39 |
Correct |
69 ms |
34128 KB |
Output is correct |
40 |
Correct |
59 ms |
33368 KB |
Output is correct |
41 |
Correct |
57 ms |
32936 KB |
Output is correct |
42 |
Correct |
57 ms |
32848 KB |
Output is correct |
43 |
Correct |
54 ms |
33104 KB |
Output is correct |
44 |
Correct |
6 ms |
31696 KB |
Output is correct |
45 |
Correct |
6 ms |
31672 KB |
Output is correct |
46 |
Correct |
5 ms |
31664 KB |
Output is correct |
47 |
Correct |
5 ms |
31580 KB |
Output is correct |
48 |
Correct |
6 ms |
31696 KB |
Output is correct |
49 |
Correct |
7 ms |
31580 KB |
Output is correct |
50 |
Correct |
7 ms |
31668 KB |
Output is correct |
51 |
Correct |
10 ms |
31836 KB |
Output is correct |
52 |
Correct |
8 ms |
31832 KB |
Output is correct |
53 |
Correct |
27 ms |
33112 KB |
Output is correct |
54 |
Correct |
72 ms |
33872 KB |
Output is correct |
55 |
Correct |
72 ms |
34192 KB |
Output is correct |
56 |
Correct |
63 ms |
33364 KB |
Output is correct |
57 |
Correct |
107 ms |
34900 KB |
Output is correct |
58 |
Correct |
33 ms |
32600 KB |
Output is correct |
59 |
Correct |
55 ms |
33504 KB |
Output is correct |