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 <iostream>
using namespace std;
const int stala=5e4+10;
int tab[stala][6][16];
long long pom[10][16];
void prepare(int ile,int k)
{
for(int z=1;z<16;z++){
for(int i=0;i<ile;i++){
int pocz=i/k;
int sr=pocz+(1<<(z-1));
int kon=pocz+(1<<z);
if(kon*k<=ile){
int sr_x=min(sr*k,ile-1);
int sr_y=min((sr+1)*k-1,ile-1);
int kon_x=min(kon*k,ile-1);
int kon_y=min((kon+1)*k-1,ile-1);
for(int x=sr_x;x<=sr_y;x++){
for(int y=kon_x;y<=kon_y;y++){
tab[i][y%k][z]=min(tab[i][y%k][z],tab[i][x%k][z-1]+tab[x][y%k][z-1]);
}
}
}
}
}
}
int find_path(int x,int y,int k,int ile)
{
for(int i=0;i<k;i++){
for(int j=1;j<16;j++){
pom[i][j]=1e9;
}
}
int group_x=x/k;
int group_y=y/k;
if(group_x>=group_y){
return -1;
}
int roznica=group_y-group_x;
int l=0;
for(int i=15;i>=0;i--){
if((roznica&(1<<i))>0){
l++;
if(l==1){
for(int j=0;j<k;j++){
pom[j][l]=tab[x][j][i];
}
group_x+=(1<<i);
}
else{
int pocz_x=min(group_x*k,ile-1);
int pocz_y=min((group_x+1)*k-1,ile-1);
int kon_x=min(group_y*k,ile-1);
int kon_y=min((group_y+1)*k-1,ile-1);
for(int x=pocz_x;x<=pocz_y;x++){
for(int y=kon_x;y<=kon_y;y++){
pom[y%k][l]=min(pom[y%k][l],pom[x%k][l-1]+tab[x][y%k][i]);
}
}
group_x+=(1<<i);
}
}
}
if(pom[y%k][l]<1e9){
return pom[y%k][l];
}
else{
return -1;
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int k,n,m,o;
cin>>k>>n>>m>>o;
for(int i=0;i<=n;i++){
for(int j=0;j<k;j++){
for(int z=0;z<16;z++){
tab[i][j][z]=1e9;
}
}
}
for(int i=1;i<=m;i++){
int a,b,c;
cin>>a>>b>>c;
tab[a][b%k][0]=c;
}
prepare(n,k);
for(int i=1;i<=o;i++){
int a,b;
cin>>a>>b;
cout<<find_path(a,b,k,n)<<"\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... |