#include<bits/stdc++.h>
using namespace std;
bool M1;
#define task "aws"
#define sz(a) (int)a.size()
#define all(x) x.begin(),x.end()
#define ii pair<int,int>
#define iii pair<int,ii>
#define iv pair<ii,ii>
#define se second
#define fi first
#define ffi fi.fi
#define sfi se.fi
#define sse se.se
#define fse fi.se
#define lt(i, c, d) for(int i = c; i <= d; ++i)
#define fl(i, c, d) for(int i = d; i >= c; --i)
#define pb push_back
#define emb emplace_back
#define inf 1e18
#define emf emplace_front
#define em emplace
#define look_memory cerr<<abs(&M2-&M1)/1024.0/1024<<'\n'
#define look_time cerr << "TIME : " << clock() * 0.001 << "s" <<'\n'
const int N=5e4+5,lg=15,mod=1e9+7;
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int u,int v){
return u+rd()%(v-u+1);
}
#define int long long
int dx[]={1,0,-1,0,1,1,-1,-1};
int dy[]={0,-1,0,1,1,-1,1,-1};
int node,o,k,edges;
struct pt
{
int x[5][5];
pt(){
for(int i=0;i<5;++i){
for(int j=0;j<5;++j){
x[i][j]=inf;
}
}
}
};
pt dp[N][lg+2];
pt combine(pt x,pt y){
pt res;
for(int i=0;i<k;++i){
for(int j=0;j<k;++j){
for(int u=0;u<k;++u){
res.x[i][j]=min(res.x[i][j],x.x[i][u]+y.x[u][j]);
}
}
}
return res;
}
void build(void){
for(int j=1;j<=15;++j)
for(int i=0;i<=node/k;++i){
dp[i][j]=combine(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
}
int get(int a,int b){
int x=a%k;
int y=b%k;
pt res;
for(int i=0;i<k;++i)res.x[i][i]=0;
a/=k,b/=k;
if(a>=b){
return -1;;
}
int dist=(b-a);
for(int j=0;j<=lg;++j){
if(dist&(1<<j)){
res=combine(res,dp[a][j]);
a+=(1<<j);
}
}
return res.x[x][y];
}
bool M2;
void solve(){
cin >> k >> node >> edges >> o;
for(int i=1;i<=edges;++i){
int u,v;
int w;
cin >> u >> v >> w;
int x=u%k,y=v%k;
dp[u/k][0].x[x][y]=min(dp[u/k][0].x[x][y],w);
}
build();
while(o--){
int a,b;
cin >> a >> b;
int u=get(a,b);
cout <<(u==inf?-1:u)<<'\n';
}
}
main()
{
srand(time(0));
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
if(fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
int t=1;
// cin >> t;
while(t--){
solve();
}
look_memory;
look_time;
}
Compilation message (stderr)
toll.cpp:99:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
99 | main()
| ^~~~
toll.cpp: In function 'int main()':
toll.cpp:106:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:107:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | 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... |