#include<bits/stdc++.h>
using namespace std;
#define maxn 50005
#define maxl 20
#define maxk 5
const int inf=1e9;
int dp[maxn][maxl][maxk],n,m,k,t,Min[maxk],nMin[maxk];
int query(int u,int v){
for(int i=0;i<k;i++) Min[i]=nMin[i]=inf;
Min[u%k]=0;
int ub=u/k,vb=v/k;
for(int i=15;i>=0;i--){
if((vb-ub)&(1<<i)){
for(int j=0;j<k;j++) nMin[j]=inf;
for(int p=0;p<k;p++){
for(int j=0;j<k;j++){
int cur=ub*k+p;
nMin[j]=min(nMin[j],Min[p]+dp[cur][i][j]);
}
}
ub+=(1<<i);
for(int j=0;j<k;j++) Min[j]=nMin[j];
}
}
return Min[v%k];
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
cin >> k >> n >> m >> t;
for(int u=0;u<n;u++){
for(int i=0;i<=15;i++){
for(int j=0;j<k;j++) dp[u][i][j]=inf;
}
}
for(int i=1;i<=m;i++){
int u,v,w;cin >> u >> v >> w;
dp[u][0][v%k]=w;
}
for(int i=1;i<=15;i++){
for(int u=0;u<n;u++){
for(int j=0;j<k;j++){
dp[u][i][j]=inf;
for(int p=0;p<k;p++){
int nxt=((u/k)+(1<<(i-1)))*k+p;
if(nxt>=n) continue;
dp[u][i][j]=min(dp[u][i][j],dp[u][i-1][p]+dp[nxt][i-1][j]);
}
}
}
}
for(int i=1;i<=t;i++){
int u,v;cin >> u >> v;
int x=query(u,v);
if(x==inf) cout << -1 << '\n';
else cout << x << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
20684 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
728 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
46 ms |
20516 KB |
Output is correct |
9 |
Correct |
42 ms |
20472 KB |
Output is correct |
10 |
Correct |
25 ms |
20024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
20684 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
4 ms |
772 KB |
Output is correct |
8 |
Correct |
4 ms |
852 KB |
Output is correct |
9 |
Correct |
38 ms |
20544 KB |
Output is correct |
10 |
Correct |
91 ms |
20584 KB |
Output is correct |
11 |
Correct |
64 ms |
20556 KB |
Output is correct |
12 |
Correct |
60 ms |
20532 KB |
Output is correct |
13 |
Correct |
71 ms |
12708 KB |
Output is correct |
14 |
Correct |
44 ms |
12656 KB |
Output is correct |
15 |
Correct |
56 ms |
12696 KB |
Output is correct |
16 |
Correct |
59 ms |
12656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
728 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
728 KB |
Output is correct |
10 |
Correct |
32 ms |
20564 KB |
Output is correct |
11 |
Correct |
55 ms |
20556 KB |
Output is correct |
12 |
Correct |
84 ms |
20636 KB |
Output is correct |
13 |
Correct |
73 ms |
20536 KB |
Output is correct |
14 |
Correct |
62 ms |
20648 KB |
Output is correct |
15 |
Correct |
55 ms |
13012 KB |
Output is correct |
16 |
Correct |
52 ms |
12800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
728 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
728 KB |
Output is correct |
10 |
Correct |
32 ms |
20564 KB |
Output is correct |
11 |
Correct |
55 ms |
20556 KB |
Output is correct |
12 |
Correct |
84 ms |
20636 KB |
Output is correct |
13 |
Correct |
73 ms |
20536 KB |
Output is correct |
14 |
Correct |
62 ms |
20648 KB |
Output is correct |
15 |
Correct |
55 ms |
13012 KB |
Output is correct |
16 |
Correct |
52 ms |
12800 KB |
Output is correct |
17 |
Correct |
57 ms |
20648 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
2 ms |
764 KB |
Output is correct |
24 |
Correct |
2 ms |
728 KB |
Output is correct |
25 |
Correct |
4 ms |
724 KB |
Output is correct |
26 |
Correct |
3 ms |
724 KB |
Output is correct |
27 |
Correct |
39 ms |
20520 KB |
Output is correct |
28 |
Correct |
84 ms |
20448 KB |
Output is correct |
29 |
Correct |
93 ms |
20520 KB |
Output is correct |
30 |
Correct |
74 ms |
20428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
20684 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
728 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
46 ms |
20516 KB |
Output is correct |
9 |
Correct |
42 ms |
20472 KB |
Output is correct |
10 |
Correct |
25 ms |
20024 KB |
Output is correct |
11 |
Correct |
57 ms |
20684 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
4 ms |
772 KB |
Output is correct |
18 |
Correct |
4 ms |
852 KB |
Output is correct |
19 |
Correct |
38 ms |
20544 KB |
Output is correct |
20 |
Correct |
91 ms |
20584 KB |
Output is correct |
21 |
Correct |
64 ms |
20556 KB |
Output is correct |
22 |
Correct |
60 ms |
20532 KB |
Output is correct |
23 |
Correct |
71 ms |
12708 KB |
Output is correct |
24 |
Correct |
44 ms |
12656 KB |
Output is correct |
25 |
Correct |
56 ms |
12696 KB |
Output is correct |
26 |
Correct |
59 ms |
12656 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
728 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
2 ms |
724 KB |
Output is correct |
35 |
Correct |
2 ms |
728 KB |
Output is correct |
36 |
Correct |
32 ms |
20564 KB |
Output is correct |
37 |
Correct |
55 ms |
20556 KB |
Output is correct |
38 |
Correct |
84 ms |
20636 KB |
Output is correct |
39 |
Correct |
73 ms |
20536 KB |
Output is correct |
40 |
Correct |
62 ms |
20648 KB |
Output is correct |
41 |
Correct |
55 ms |
13012 KB |
Output is correct |
42 |
Correct |
52 ms |
12800 KB |
Output is correct |
43 |
Correct |
57 ms |
20648 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
2 ms |
764 KB |
Output is correct |
50 |
Correct |
2 ms |
728 KB |
Output is correct |
51 |
Correct |
4 ms |
724 KB |
Output is correct |
52 |
Correct |
3 ms |
724 KB |
Output is correct |
53 |
Correct |
39 ms |
20520 KB |
Output is correct |
54 |
Correct |
84 ms |
20448 KB |
Output is correct |
55 |
Correct |
93 ms |
20520 KB |
Output is correct |
56 |
Correct |
74 ms |
20428 KB |
Output is correct |
57 |
Correct |
131 ms |
20604 KB |
Output is correct |
58 |
Correct |
38 ms |
20508 KB |
Output is correct |
59 |
Correct |
58 ms |
20528 KB |
Output is correct |