#include <bits/stdc++.h>
using namespace std;
int const N=1e5;
int const inf=1e9+5;
int wei[N][20][6];
int main(){
int k,n,m,o;
cin>>k>>n>>m>>o;
for(int i=0;i<n;i++)
for(int j=0;j<=16;j++)
for(int kk=0;kk<=5;kk++)
wei[i][j][kk]=inf;
for(int i=0;i<m;i++){
int a,b,w;
cin>>a>>b>>w;
// swap(a,b);//switch directions;
wei[a][0][b%k]=w;
}
//compute
int midnode=0;
for(int par=1;par<=15;par++){
for(int node=0;node<n;node++){
midnode=(((1<<(par-1))+(node/k))*k);
for(int md=0;md<k;md++){
for(int d=0;d<k;d++){
wei[node][par][md]=min(wei[node][par][md],(wei[node][par-1][d]+wei[midnode+d][par-1][md]));
}
}
}
}
while(o--){
int a,b;
cin>>a>>b;
int d=(b/k)-(a/k);
if(d<0){
cout<<-1<<endl;
continue;
}
int w[k],ww[k];
for(int i=0;i<k;i++)
w[i]=inf;
w[a%k]=0;
int cur=a/k;
while(d){
int par=31-(__builtin_clz(d));
d-=(1<<par);
// cout<<"par "<<par<<endl;
for(int i=0;i<k;i++){
int mn=inf;
for(int j=0;j<k;j++)
mn=min(mn,w[j]+wei[(cur*k)+j][par][i]);
ww[i]=mn;
}
for(int i=0;i<k;i++)
w[i]=ww[i];
cur+=(1<<par);
}
if(w[b%k]>=inf)
w[b%k]=-1;
cout<<w[b%k]<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
25176 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
2652 KB |
Output is correct |
6 |
Correct |
3 ms |
2652 KB |
Output is correct |
7 |
Correct |
3 ms |
2652 KB |
Output is correct |
8 |
Correct |
63 ms |
25232 KB |
Output is correct |
9 |
Correct |
58 ms |
25176 KB |
Output is correct |
10 |
Correct |
29 ms |
25176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
25684 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
15 ms |
2652 KB |
Output is correct |
8 |
Correct |
22 ms |
2648 KB |
Output is correct |
9 |
Correct |
61 ms |
25240 KB |
Output is correct |
10 |
Correct |
117 ms |
25176 KB |
Output is correct |
11 |
Correct |
88 ms |
25232 KB |
Output is correct |
12 |
Correct |
91 ms |
25236 KB |
Output is correct |
13 |
Runtime error |
113 ms |
30056 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2672 KB |
Output is correct |
8 |
Correct |
4 ms |
2904 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
41 ms |
25180 KB |
Output is correct |
11 |
Correct |
69 ms |
25240 KB |
Output is correct |
12 |
Correct |
130 ms |
25216 KB |
Output is correct |
13 |
Correct |
110 ms |
25180 KB |
Output is correct |
14 |
Correct |
85 ms |
25176 KB |
Output is correct |
15 |
Runtime error |
72 ms |
30036 KB |
Execution killed with signal 11 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2672 KB |
Output is correct |
8 |
Correct |
4 ms |
2904 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
41 ms |
25180 KB |
Output is correct |
11 |
Correct |
69 ms |
25240 KB |
Output is correct |
12 |
Correct |
130 ms |
25216 KB |
Output is correct |
13 |
Correct |
110 ms |
25180 KB |
Output is correct |
14 |
Correct |
85 ms |
25176 KB |
Output is correct |
15 |
Runtime error |
72 ms |
30036 KB |
Execution killed with signal 11 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
25176 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
2652 KB |
Output is correct |
6 |
Correct |
3 ms |
2652 KB |
Output is correct |
7 |
Correct |
3 ms |
2652 KB |
Output is correct |
8 |
Correct |
63 ms |
25232 KB |
Output is correct |
9 |
Correct |
58 ms |
25176 KB |
Output is correct |
10 |
Correct |
29 ms |
25176 KB |
Output is correct |
11 |
Correct |
92 ms |
25684 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
15 ms |
2652 KB |
Output is correct |
18 |
Correct |
22 ms |
2648 KB |
Output is correct |
19 |
Correct |
61 ms |
25240 KB |
Output is correct |
20 |
Correct |
117 ms |
25176 KB |
Output is correct |
21 |
Correct |
88 ms |
25232 KB |
Output is correct |
22 |
Correct |
91 ms |
25236 KB |
Output is correct |
23 |
Runtime error |
113 ms |
30056 KB |
Execution killed with signal 11 |
24 |
Halted |
0 ms |
0 KB |
- |