#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define in(a, b) for (ll i = (a); i <= (b); i++) // in using i
#define inj(a, b) for (ll j = (a); j <= (b); j++) // in using j
#define ink(a, b) for (ll k = (a); k <= (b); k++) // in using k
#define inl(a, b) for (ll l = (a); l <= (b); l++) // in using l
#define inr(a, b) for(ll i = (a); i >= (b); i--) // in reverse
#define inrj(a, b) for(ll j = (a); j >= (b); j--) // in reverse
#define inrk(a, b) for(ll k = (a); k >= (b); k--) // in reverse
#define inrl(a, b) for(ll l = (a); l >= (b); l--) // in reverse
#define tt ll tcs; cin>>tcs; while(tcs--) // include test cases
#define ina(arr,n) ll arr[(n+1)]={0}; in(1,n) cin>>arr[i] // input arr of n elements
#define inv(vec,n) vector<ll> vec(n+1); vec[0]=0; in(1,n) cin>>vec[i]; // input vector of n elements
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pll pair <ll,ll>
#define vpll vector <pll>
#define sll set <ll>
#define vll vector<ll>
#define mll map <ll,ll>
#define all(x) x.begin(), x.end()
const ll mod=1e9+7;
#define vvll vector<vll>
#define pref(p,a,n) vll p(n+1); in(1,n) p[i]=p[i-1]+a[i];
#define vec2(a,n,m) vvll a(n+1,vll(m+1))
// #define vec2(a,n,m,val) vvll a(n+1,vll(m+1,val))
// #define vec3(a,l,m,n) vector<vvll>a(l+1,vvll(m+1,vll(n+1)));
#define vec3(a,l,m,n,val) vector<vvll>a(l+1,vvll(m+1,vll(n+1,val)));
# define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
vvll mult(const vvll &a, const vvll &b, ll k){
vvll ans(k,vll(k,1e13));
for(int i=0;i<k;i++){
for(int j=0;j<k;j++){
for(int s=0;s<k;s++){
ans[i][j]=min(ans[i][j],a[i][s]+b[s][j]);
}
}
}
return ans;
}
//0..4 5..9 10..14
int main(){
FAST
ll k,n,m,o;
cin>>k>>n>>m>>o;
//n=next multiple of k
// n+=(n%k);
ll grp=(n/k)+1;
vec3(mat,grp,k,k,1e13);
//0...k-1 k...2k-1
for(int i=0;i<m;i++){
int a,b,t;
cin>>a>>b>>t;
mat[a/k][a%k][b%k]=t;
}
vector<vector<vector<vector<ll>>>>st(grp+1,vector<vector<vector<ll>>>(log2(grp)+1));
vvll nxt(grp+1,vll(log2(grp)+1));
for(int i=0;i<grp;i++){
st[i][0]=mat[i];
nxt[i][0]=i+1;
}
st[grp][0].resize(k,vll(k));
for(int i=0;i<k;i++){
for(int j=0;j<k;j++){
st[grp][0][i][j]=1e14;
}
}
nxt[grp][0]=grp;
for(int l=1;l<=log2(grp);l++){
for(int i=0;i<=grp;i++){
nxt[i][l]=nxt[nxt[i][l-1]][l-1];
st[i][l]=mult(st[i][l-1],st[nxt[i][l-1]][l-1],k);
}
}
vvll iden(k,vll(k,1e14));
for(int i=0;i<k;i++){
iden[i][i]=0;
}
while(o--){
int a,b;
cin>>a>>b;
int g1=a/k;
int g2=b/k;
vvll mt=iden;
for(int j=log2(grp);j>=0;j--){
if(nxt[g1][j]<=g2){
mt=mult(mt,st[g1][j],k);
g1=nxt[g1][j];
}
}
ll ans=mt[a%k][b%k];
if(ans<=1e10){
cout<<ans<<endl;
}
else{
cout<<-1<<endl;
}
}
}
# | 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... |