#include <bits/stdc++.h>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define pi pair<int,int>
#define pii pair<int,pi>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(a) (int) a.size()
#define endl '\n'
#define data "data"
using namespace std;
const ll linf = 1e18;
const int inf = 1e9;
const int MOD = 1e9 + 7, N = 5e4;
void add(int &a, int b)
{
a += b;
if(a>=MOD)
a-=MOD;
if(a<0)
a += MOD;
}
int modulo(int x)
{
if(x<=1)
return 1;
return (MOD - MOD/x) * modulo(MOD/x) % MOD;
}
int mul(int a, int b)
{
return (1ll *a%MOD * b%MOD) % MOD;
}
int k, n, m, q;
ll dp[N+3][20][5][5];
ll ans[5][5], tmp[5][5];
void inp(void)
{
cin >> k >> n >> m >> q;
memset(dp, 0x3f, sizeof(dp));
FOR(i, 1, m)
{
int u, v, w; cin >> u >> v >> w;
dp[u/k][0][u%k][v%k] = 1ll * w;
}
}
void compute(ll ret[5][5], ll L[5][5], ll R[5][5])
{
FOR(a, 0, k - 1)
{
FOR(b, 0, k - 1)
{
FOR(c, 0, k - 1)
ret[a][b] = min(ret[a][b], L[a][c] + R[c][b]);
}
}
}
void solve(void)
{
FOR(j, 1, 17)
{
for(int i = 0; i + (1 << j) -1 <= (n + k - 1) / k; i++)
{
compute(dp[i][j], dp[i][j-1], dp[i + (1 << (j-1))][j-1]);
}
}
FOR(_, 1, q)
{
int a, b; cin >> a >> b;
memset(ans, 0x3f,sizeof(ans));
FOR(i, 0, 4) ans[i][i] = 0;
for(int i = 17, cur = a/k, edn = b / k; i >= 0; --i)
{
if(cur + (1 << i) <= edn)
{
memset(tmp, 0x3f, sizeof(tmp));
compute(tmp, ans, dp[cur][i]);
FOR(x, 0, k - 1)
{
FOR(y, 0, k - 1) ans[x][y] = tmp[x][y];
}
cur += (1 << i);
}
}
cout << (ans[a%k][b%k] < linf ? ans[a%k][b%k] : -1) << '\n';
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if(fopen(data".inp", "r"))
{
freopen(data".inp","r",stdin);
freopen(data".out","w",stdout);
}
inp();
solve();
return 0;
}
Compilation message (stderr)
toll.cpp: In function 'int main()':
toll.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen(data".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | freopen(data".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... |