#include<bits/stdc++.h>
#define pll pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define task "SKILEVEL"
#define pii pair<ll, pll>
using namespace std;
using ll = long long;
const int N = 1e5+5;
const ll mod = 1e15;
const ll base = 350;
const ll cs = 331;
ll m, n, t, k, a[N], tong, b[N], dp[N][16][5][5], q, ans;
vector<ll> adj[N], kq;
ll pw(ll k, ll n)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n & 1)total = total * k % mod;
k = k * k % mod;
}
return total;
}
void sol()
{
cin >> k >> n >> m >> q;
for(int i = 0; i < n/k; i ++)
{
for(int j = 0; j < 16; j ++)
{
for(int l = 0; l < k; l ++)
{
for(int r = 0; r < k; r ++)dp[i][j][l][r] = mod;
}
}
}
while(m -- > 0)
{
ll x, y, w;
cin >> x >> y >> w;
dp[x/k][0][x%k][y%k] = min(dp[x/k][0][x%k][y%k], w);
}
for(int j = 1; j < 16; j ++)
{
for(int i = 0; i + (1<<j) - 1< n/k; i ++)
{
for(int l = 0; l < k; l ++)
{
for(int r = 0; r < k; r ++)
{
for(int p = 0; p < k; p ++)
{
dp[i][j][l][r] = min(dp[i][j][l][r], dp[i][j-1][l][p] + dp[i+(1<<(j-1))][j-1][p][r]);
}
}
}
}
}
while(q -- > 0)
{
ll x, y;
cin >> x >> y;
ll u = x / k, v = y / k;
vector<ll> kq, pre;
kq.resize(k, mod);
kq[x%k] = 0;
ans = mod;
for(int j = 15; j >= 0; j --)
{
if(u + (1<<j) <= v)
{
pre.resize(k, mod);
for(int l = 0; l < k; l ++)
{
for(int r = 0; r < k; r ++)pre[r] = min(pre[r], kq[l] + dp[u][j][l][r]);
}
swap(kq, pre);
u += (1<<j);
}
}
ans = kq[y%k];
if(ans >= mod)ans = -1;
cout << ans << '\n';
}
}
int 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);
}
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
/*
2
6 113 3
1 2 3 4 5 6
1 4
6 113 70
1 2 3 4 5 6
1 4 3
*/
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
116 ms |
160068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
120 ms |
82480 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
116 ms |
160068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |