#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb emplace_back
#define ii pair<int, int>
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define sz(s) (int)((s).size())
#define all(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define F0R(i, b) for (int i = 0, _b = (b); i < _b; ++i)
#define FORd(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define F0Rd(i, b) for (int i = (b)-1; i >= 0; i--)
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using namespace std;
template<typename T1,typename T2> bool ckmax(T1 &x,const T2 &y){if(x<y){x=y; return 1;} return 0;}
template<typename T1,typename T2> bool ckmin(T1 &x,const T2 &y){if(y<x){x=y; return 1;} return 0;}
const int MOD = (int)1e9 + 7;
const int mod = 998244353;
const int N = 5e4 + 10, M = 18;
const int INF = (int)1e9 + 11;
/*
/\_/\
(= ._.)
/ >? \>$
*/
int n, m, q, k;
int res[5][5], T[5][5];
int F[N][18][5][5];
void comb(int a[5][5], int b[5][5], int c[5][5]){
F0R(x, k) F0R(y, k) F0R(z, k) a[x][y] = min(a[x][y], b[x][z] + c[z][y]);
}
void sol(void){
cin >> k >> n >> m >> q;
memset(F, 0x3f, sizeof(F));
FOR(i, 1, m){
int u, v, w; cin >> u >> v >> w;
F[u/k][0][u % k][v % k] = w;
}
int d = (n+k-1) / k, lg = __lg(d+1);
FOR(j, 1, lg) FOR(i, 0, d-MASK(j)) comb(F[i][j], F[i][j-1], F[i+MASK(j-1)][j-1]);
while(q--){
int u, v; cin >> u >> v;
int s = u / k, t = v / k;
if(s == t){
cout << -1 << '\n';
continue;
}
memset(res, 0, sizeof(res));
F0Rd(i, lg+1) if(s + MASK(i) <= t){
memset(T, 0x3f, sizeof(T));
comb(T, res, F[s][i]);
F0R(i, k) F0R(j, k) res[i][j] = T[i][j];
s += MASK(i);
}
cout << (res[u % k][v % k] > INF ? -1 : res[u % k][v % k]) << '\n';
}
}
signed main(void){
#define TASK "nhap"
if(fopen(TASK".inp", "r")){
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int t = 1;
// cin >> t;
while(t--) sol();
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << " ms\n";
}
Compilation message (stderr)
toll.cpp: In function 'int main()':
toll.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | 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... |