#include <bits/stdc++.h>
#define INF 1000000000
using namespace std;
int k,n,m,o;
int rmq[50005][20][5],lg,i,j,t,x,y,cost,locx,locy,p,nivelsf,start,sfarsit;
int din[10][5];
int main()
{
#ifdef HOME
ifstream cin("date.in");
ofstream cout("date.out");
#endif // HOME
cin>>k>>n>>m>>o;
lg=log2(n);
for (i=0;i<=n;i++)
{
for (j=0;j<=lg;j++)
{
for (t=0;t<k;t++)
{
rmq[i][j][t]=INF;
}
}
}
for (i=1;i<=m;i++)
{
cin>>x>>y>>cost;
locx=x%k;
locy=y%k;
rmq[x][0][locy]=cost;
}
p=1;
for (i=1;i<=lg;i++)
{
for (j=0;j<=n;j++)
{
nivelsf=j/k+p;
if (nivelsf+p>n/k)
{
continue;
}
for (start=0;start<k;start++)
{
for (sfarsit=0;sfarsit<k;sfarsit++)
{
rmq[j][i][sfarsit]=min(rmq[j][i-1][start]+rmq[nivelsf*k+start][i-1][sfarsit],rmq[j][i][sfarsit]);
}
}
}
p=p*2;
}
for (;o--;)
{
int a,b;
cin>>a>>b;
if (a==b)
{
cout<<"0"<<'\n';
continue;
}
int niva,nivb,dif;
niva=a/k;
nivb=b/k;
if (niva>=nivb)
{
cout<<"0"<<'\n';
continue;
}
dif=nivb-niva;
int p=1;
for (i=0;i<k;i++)
{
if (a%k==i)
{
din[i][0]=0;
}
else
{
din[i][0]=INF;
}
din[i][1]=INF;
}
int nivel,pas,acum,inainte;
nivel=a/k;
pas=0;
for (i=0;i<=lg;i++)
{
if (dif&p)
{
pas++;
acum=pas%2;
inainte=1-acum;
for (j=0;j<k;j++)
{
for (t=0;t<k;t++)
{
din[t][acum]=min(din[t][acum],din[j][inainte]+rmq[nivel*k+j][i][t]);
}
}
for (t=0;t<k;t++)
{
din[t][inainte]=INF;
}
nivel=nivel+p;
}
p=p*2;
}
if (din[b%k][acum]==INF)
{
cout<<"-1"<<'\n';
}
else
{
cout<<din[b%k][acum]<<'\n';
}
}
return 0;
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:108:26: warning: 'acum' may be used uninitialized in this function [-Wmaybe-uninitialized]
108 | if (din[b%k][acum]==INF)
| ~~~~~~~~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
19852 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
3 ms |
588 KB |
Output is correct |
6 |
Correct |
3 ms |
716 KB |
Output is correct |
7 |
Correct |
3 ms |
716 KB |
Output is correct |
8 |
Correct |
81 ms |
20676 KB |
Output is correct |
9 |
Correct |
73 ms |
20736 KB |
Output is correct |
10 |
Correct |
36 ms |
19892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
19844 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
19852 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
3 ms |
588 KB |
Output is correct |
6 |
Correct |
3 ms |
716 KB |
Output is correct |
7 |
Correct |
3 ms |
716 KB |
Output is correct |
8 |
Correct |
81 ms |
20676 KB |
Output is correct |
9 |
Correct |
73 ms |
20736 KB |
Output is correct |
10 |
Correct |
36 ms |
19892 KB |
Output is correct |
11 |
Correct |
143 ms |
19844 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |