#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 = 1e12;
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];
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] = 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;
kq.resize(k, mod);
kq[x%k] = 0;
for(int j = 15; j >= 0; j --)
{
if(u + (1<<j) <= v)
{
vector<ll> pre;
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
1 6 6 4
1 2 3
2 3 6
0 1 6
1 2 3
1 2 6
3 4 3
0 4
2 3
1 3
0 2
*/
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 |
Correct |
122 ms |
159276 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
3 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2676 KB |
Output is correct |
5 |
Correct |
4 ms |
5756 KB |
Output is correct |
6 |
Correct |
4 ms |
5836 KB |
Output is correct |
7 |
Correct |
4 ms |
5836 KB |
Output is correct |
8 |
Correct |
119 ms |
160068 KB |
Output is correct |
9 |
Correct |
118 ms |
160088 KB |
Output is correct |
10 |
Correct |
99 ms |
159300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
80936 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2676 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
3 ms |
2764 KB |
Output is correct |
7 |
Correct |
9 ms |
5836 KB |
Output is correct |
8 |
Correct |
9 ms |
4376 KB |
Output is correct |
9 |
Correct |
117 ms |
160088 KB |
Output is correct |
10 |
Correct |
114 ms |
57156 KB |
Output is correct |
11 |
Correct |
122 ms |
82720 KB |
Output is correct |
12 |
Correct |
109 ms |
56180 KB |
Output is correct |
13 |
Correct |
105 ms |
23780 KB |
Output is correct |
14 |
Correct |
64 ms |
35416 KB |
Output is correct |
15 |
Correct |
55 ms |
22592 KB |
Output is correct |
16 |
Correct |
58 ms |
22576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2672 KB |
Output is correct |
5 |
Correct |
3 ms |
2636 KB |
Output is correct |
6 |
Correct |
4 ms |
5836 KB |
Output is correct |
7 |
Correct |
4 ms |
4172 KB |
Output is correct |
8 |
Correct |
4 ms |
3276 KB |
Output is correct |
9 |
Correct |
4 ms |
3404 KB |
Output is correct |
10 |
Correct |
110 ms |
160012 KB |
Output is correct |
11 |
Correct |
109 ms |
82420 KB |
Output is correct |
12 |
Correct |
111 ms |
57012 KB |
Output is correct |
13 |
Correct |
110 ms |
57260 KB |
Output is correct |
14 |
Correct |
96 ms |
56572 KB |
Output is correct |
15 |
Correct |
52 ms |
22596 KB |
Output is correct |
16 |
Correct |
57 ms |
22712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2672 KB |
Output is correct |
5 |
Correct |
3 ms |
2636 KB |
Output is correct |
6 |
Correct |
4 ms |
5836 KB |
Output is correct |
7 |
Correct |
4 ms |
4172 KB |
Output is correct |
8 |
Correct |
4 ms |
3276 KB |
Output is correct |
9 |
Correct |
4 ms |
3404 KB |
Output is correct |
10 |
Correct |
110 ms |
160012 KB |
Output is correct |
11 |
Correct |
109 ms |
82420 KB |
Output is correct |
12 |
Correct |
111 ms |
57012 KB |
Output is correct |
13 |
Correct |
110 ms |
57260 KB |
Output is correct |
14 |
Correct |
96 ms |
56572 KB |
Output is correct |
15 |
Correct |
52 ms |
22596 KB |
Output is correct |
16 |
Correct |
57 ms |
22712 KB |
Output is correct |
17 |
Correct |
117 ms |
82472 KB |
Output is correct |
18 |
Correct |
3 ms |
2636 KB |
Output is correct |
19 |
Correct |
2 ms |
2636 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
7 ms |
5836 KB |
Output is correct |
24 |
Correct |
5 ms |
4300 KB |
Output is correct |
25 |
Correct |
6 ms |
3404 KB |
Output is correct |
26 |
Correct |
6 ms |
3532 KB |
Output is correct |
27 |
Correct |
111 ms |
159972 KB |
Output is correct |
28 |
Correct |
108 ms |
57028 KB |
Output is correct |
29 |
Correct |
116 ms |
57372 KB |
Output is correct |
30 |
Correct |
102 ms |
56604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
159276 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
3 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2676 KB |
Output is correct |
5 |
Correct |
4 ms |
5756 KB |
Output is correct |
6 |
Correct |
4 ms |
5836 KB |
Output is correct |
7 |
Correct |
4 ms |
5836 KB |
Output is correct |
8 |
Correct |
119 ms |
160068 KB |
Output is correct |
9 |
Correct |
118 ms |
160088 KB |
Output is correct |
10 |
Correct |
99 ms |
159300 KB |
Output is correct |
11 |
Correct |
120 ms |
80936 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2676 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
3 ms |
2764 KB |
Output is correct |
17 |
Correct |
9 ms |
5836 KB |
Output is correct |
18 |
Correct |
9 ms |
4376 KB |
Output is correct |
19 |
Correct |
117 ms |
160088 KB |
Output is correct |
20 |
Correct |
114 ms |
57156 KB |
Output is correct |
21 |
Correct |
122 ms |
82720 KB |
Output is correct |
22 |
Correct |
109 ms |
56180 KB |
Output is correct |
23 |
Correct |
105 ms |
23780 KB |
Output is correct |
24 |
Correct |
64 ms |
35416 KB |
Output is correct |
25 |
Correct |
55 ms |
22592 KB |
Output is correct |
26 |
Correct |
58 ms |
22576 KB |
Output is correct |
27 |
Correct |
2 ms |
2636 KB |
Output is correct |
28 |
Correct |
3 ms |
2636 KB |
Output is correct |
29 |
Correct |
2 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2672 KB |
Output is correct |
31 |
Correct |
3 ms |
2636 KB |
Output is correct |
32 |
Correct |
4 ms |
5836 KB |
Output is correct |
33 |
Correct |
4 ms |
4172 KB |
Output is correct |
34 |
Correct |
4 ms |
3276 KB |
Output is correct |
35 |
Correct |
4 ms |
3404 KB |
Output is correct |
36 |
Correct |
110 ms |
160012 KB |
Output is correct |
37 |
Correct |
109 ms |
82420 KB |
Output is correct |
38 |
Correct |
111 ms |
57012 KB |
Output is correct |
39 |
Correct |
110 ms |
57260 KB |
Output is correct |
40 |
Correct |
96 ms |
56572 KB |
Output is correct |
41 |
Correct |
52 ms |
22596 KB |
Output is correct |
42 |
Correct |
57 ms |
22712 KB |
Output is correct |
43 |
Correct |
117 ms |
82472 KB |
Output is correct |
44 |
Correct |
3 ms |
2636 KB |
Output is correct |
45 |
Correct |
2 ms |
2636 KB |
Output is correct |
46 |
Correct |
2 ms |
2636 KB |
Output is correct |
47 |
Correct |
2 ms |
2636 KB |
Output is correct |
48 |
Correct |
2 ms |
2636 KB |
Output is correct |
49 |
Correct |
7 ms |
5836 KB |
Output is correct |
50 |
Correct |
5 ms |
4300 KB |
Output is correct |
51 |
Correct |
6 ms |
3404 KB |
Output is correct |
52 |
Correct |
6 ms |
3532 KB |
Output is correct |
53 |
Correct |
111 ms |
159972 KB |
Output is correct |
54 |
Correct |
108 ms |
57028 KB |
Output is correct |
55 |
Correct |
116 ms |
57372 KB |
Output is correct |
56 |
Correct |
102 ms |
56604 KB |
Output is correct |
57 |
Correct |
147 ms |
45104 KB |
Output is correct |
58 |
Correct |
123 ms |
160072 KB |
Output is correct |
59 |
Correct |
135 ms |
82656 KB |
Output is correct |