#include<bits/stdc++.h>
using namespace std;
#define task "task"
#define ii pair<int,int>
#define fi first
#define se second
//#define int long long
#define ll long long
#define ld double
#define mp make_pair
#define lg2 20
#define iii pair<int,ii>
#define iiii pair<ii,ii>
#define base 29
#define eps 1e-8
int dx[]= {0LL,0LL,1,-1,1,1,-1,-1};
int dy[]= {1,-1,0LL,0LL,1,-1,1,-1};
const int maxn=1e5+5;
const int mod=1e9+7;
int k,m,n,o,dp[50005][lg2+1][5][5],kq[5][5],tmp[5][5];
void xet(int ans[5][5],int a[5][5],int b[5][5])
{
for(int x=0;x<k;x++)
{
for(int y=0;y<k;y++)
{
for(int z=0;z<k;z++)
{
ans[x][y]=min(ans[x][y],a[x][z]+b[z][y]);
}
}
}
}
void init()
{
cin>>k>>n>>m>>o;
memset(dp,0x3f,sizeof(dp));
while(m--)
{
int u,v,w;
cin>>u>>v>>w;
dp[u/k][0][u%k][v%k]=w;
}
for(int j=1;j<=lg2;j++)
{
for(int i=0;i+(1<<j)<=n/k;i++)
{
xet(dp[i][j],dp[i][j-1],dp[i+(1<<j)-1][j-1]);
}
}
}
void process()
{
while(o--)
{
int a,b;
cin>>a>>b;
int x=a/k,y=b/k;
memset(kq,0x3f,sizeof(kq));
for(int i=0;i<k;i++)kq[i][i]=0;
for(int j=lg2;j>=0;j--)
{
if(x+(1<<j)<=y)
{
memset(tmp,0x3f,sizeof(tmp));
xet(tmp,kq,dp[x][j]);
swap(kq,tmp);
x+=(1<<j);
}
}
if(kq[a%k][b%k]==0x3f3f3f3f)cout<<-1<<'\n';
else
cout<<kq[a%k][b%k]<<'\n';
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
init();
process();
cerr <<endl<< "TIME : " << clock() * 0.001 << "s" << endl ;
}
Compilation message (stderr)
toll.cpp: In function 'int main()':
toll.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:84:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |