This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
const int INF = 1000000000;
ll dp[50500][20][5][5];
const int MXLOG = 20;
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int k, n, q, m;
cin >> k >> n >> m >> q;
for(int i=0; i<=n/k; i++){
for(int j=0; j<k; j++){
for(int g=0; g<k; g++){
dp[i][0][j][g] = INF;
}
}
}
for(int i=0; i<m; i++){
int a, b, c;
cin >> a >> b >> c;
dp[a/k][0][a%k][b%k] = c;
}
for(int j=1; j<MXLOG; j++){
for(int i=0; i<=n/k; i++){
for(int md=0; md<k; md++){
for(int fn=0; fn<k; fn++){
dp[i][j][md][fn] = INF;
if(i+(1<<j) > n/k) continue;
for(int sr=0; sr<k; sr++){
dp[i][j][md][fn] = min(dp[i][j][md][fn], dp[i][j-1][md][sr] + dp[i+(1<<(j-1))][j-1][sr][fn]);
}
}
}
}
}
while(q--){
int a, b;
cin >> a >> b;
int x = a/k;
int y = b/k;
vector <ll> res;
for(int i=0; i<k; i++) res.push_back(INF);
res[a%k] = 0;
for(int st=MXLOG-1; st>=0; st--){
if(y-x >= (1<<st)){
vector <ll> nres;
for(int gg=0; gg<k; gg++) nres.push_back(INF);
for(int pc=0; pc<k; pc++){
for(int fn=0; fn<k; fn++){
nres[fn] = min(nres[fn], res[pc] + dp[x][st][pc][fn]);
}
}
res = nres;
x += (1<<st);
}
}
if(res[b%k] >= INF) cout << "-1\n";
else cout << res[b%k] << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |