#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define Neco "Toll"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 5e4 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r)
{
return uniform_int_distribution<ll> (l, r)(rng);
}
int n, m, k, q;
int nex[maxn][20][5];
int dp[maxn][2];
int calc(int x, int y)
{
int pre = 0, curr = 1;
fi(i, 0, k - 1) dp[i][0] = dp[i][1] = 1e9;
dp[x % k][0] = 0;
int kc = y/k - x/k, layer = x/k;
fi(t, 0, 20)
{
if(!getbit(kc, t)) continue;
fi(u, 0, k - 1) fi(v, 0, k - 1)
{
int a = k * layer + u;
dp[v][curr] = min(dp[v][curr], dp[u][pre] + nex[a][t][v]);
}
swap(pre, curr);
layer += (1 << t);
fi(i, 0, k - 1) dp[i][curr] = 1e9;
}
return dp[y % k][pre] >= 1e9 ? -1 : dp[y % k][pre];
}
void solve()
{
memset(nex, 60, sizeof nex);
cin >> k >> n >> m >> q;
fi(i, 1, m) {
int x, y, w;
cin >> x >> y >> w;
nex[x][0][y % k] = w;
}
fi(j, 1, 20) fi(i, 0, n)
{
int P = i / k;
fi(u, 0, k - 1) fi(v, 0, k - 1)
{
int x = k * (P + (1 << (j - 1))) + u;
// if(i == 0) cout << j << " " << u << " " << v << " " << x << '\n';
if(x <= n) nex[i][j][v] = min(nex[i][j][v], nex[i][j - 1][u] + nex[x][j - 1][v]);
}
}
fi(i, 1, q) {
int x, y;
cin >> x >> y;
cout << calc(x, y) << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
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(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
21060 KB |
Output is correct |
2 |
Correct |
4 ms |
20060 KB |
Output is correct |
3 |
Correct |
3 ms |
20060 KB |
Output is correct |
4 |
Correct |
4 ms |
20068 KB |
Output is correct |
5 |
Correct |
4 ms |
20060 KB |
Output is correct |
6 |
Correct |
4 ms |
20060 KB |
Output is correct |
7 |
Correct |
4 ms |
20056 KB |
Output is correct |
8 |
Correct |
25 ms |
21080 KB |
Output is correct |
9 |
Correct |
27 ms |
21080 KB |
Output is correct |
10 |
Correct |
16 ms |
20316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
21772 KB |
Output is correct |
2 |
Correct |
3 ms |
20056 KB |
Output is correct |
3 |
Correct |
3 ms |
20060 KB |
Output is correct |
4 |
Correct |
3 ms |
20060 KB |
Output is correct |
5 |
Correct |
3 ms |
20060 KB |
Output is correct |
6 |
Correct |
3 ms |
20204 KB |
Output is correct |
7 |
Correct |
6 ms |
20316 KB |
Output is correct |
8 |
Correct |
7 ms |
20572 KB |
Output is correct |
9 |
Correct |
29 ms |
21340 KB |
Output is correct |
10 |
Correct |
52 ms |
22480 KB |
Output is correct |
11 |
Correct |
54 ms |
21840 KB |
Output is correct |
12 |
Correct |
37 ms |
21340 KB |
Output is correct |
13 |
Correct |
63 ms |
22808 KB |
Output is correct |
14 |
Correct |
30 ms |
21596 KB |
Output is correct |
15 |
Correct |
37 ms |
21340 KB |
Output is correct |
16 |
Correct |
47 ms |
21340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20060 KB |
Output is correct |
2 |
Correct |
3 ms |
20252 KB |
Output is correct |
3 |
Correct |
3 ms |
20056 KB |
Output is correct |
4 |
Correct |
3 ms |
20060 KB |
Output is correct |
5 |
Correct |
4 ms |
20060 KB |
Output is correct |
6 |
Correct |
4 ms |
20060 KB |
Output is correct |
7 |
Correct |
4 ms |
20060 KB |
Output is correct |
8 |
Correct |
4 ms |
20060 KB |
Output is correct |
9 |
Correct |
5 ms |
20292 KB |
Output is correct |
10 |
Correct |
23 ms |
21056 KB |
Output is correct |
11 |
Correct |
35 ms |
21596 KB |
Output is correct |
12 |
Correct |
45 ms |
22404 KB |
Output is correct |
13 |
Correct |
46 ms |
22640 KB |
Output is correct |
14 |
Correct |
41 ms |
21840 KB |
Output is correct |
15 |
Correct |
45 ms |
21420 KB |
Output is correct |
16 |
Correct |
35 ms |
21336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20060 KB |
Output is correct |
2 |
Correct |
3 ms |
20252 KB |
Output is correct |
3 |
Correct |
3 ms |
20056 KB |
Output is correct |
4 |
Correct |
3 ms |
20060 KB |
Output is correct |
5 |
Correct |
4 ms |
20060 KB |
Output is correct |
6 |
Correct |
4 ms |
20060 KB |
Output is correct |
7 |
Correct |
4 ms |
20060 KB |
Output is correct |
8 |
Correct |
4 ms |
20060 KB |
Output is correct |
9 |
Correct |
5 ms |
20292 KB |
Output is correct |
10 |
Correct |
23 ms |
21056 KB |
Output is correct |
11 |
Correct |
35 ms |
21596 KB |
Output is correct |
12 |
Correct |
45 ms |
22404 KB |
Output is correct |
13 |
Correct |
46 ms |
22640 KB |
Output is correct |
14 |
Correct |
41 ms |
21840 KB |
Output is correct |
15 |
Correct |
45 ms |
21420 KB |
Output is correct |
16 |
Correct |
35 ms |
21336 KB |
Output is correct |
17 |
Correct |
53 ms |
21588 KB |
Output is correct |
18 |
Correct |
4 ms |
20060 KB |
Output is correct |
19 |
Correct |
3 ms |
20060 KB |
Output is correct |
20 |
Correct |
3 ms |
20060 KB |
Output is correct |
21 |
Correct |
3 ms |
20056 KB |
Output is correct |
22 |
Correct |
3 ms |
20060 KB |
Output is correct |
23 |
Correct |
4 ms |
20060 KB |
Output is correct |
24 |
Correct |
4 ms |
20316 KB |
Output is correct |
25 |
Correct |
5 ms |
20260 KB |
Output is correct |
26 |
Correct |
5 ms |
20316 KB |
Output is correct |
27 |
Correct |
23 ms |
21072 KB |
Output is correct |
28 |
Correct |
53 ms |
22360 KB |
Output is correct |
29 |
Correct |
75 ms |
22516 KB |
Output is correct |
30 |
Correct |
45 ms |
21852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
21060 KB |
Output is correct |
2 |
Correct |
4 ms |
20060 KB |
Output is correct |
3 |
Correct |
3 ms |
20060 KB |
Output is correct |
4 |
Correct |
4 ms |
20068 KB |
Output is correct |
5 |
Correct |
4 ms |
20060 KB |
Output is correct |
6 |
Correct |
4 ms |
20060 KB |
Output is correct |
7 |
Correct |
4 ms |
20056 KB |
Output is correct |
8 |
Correct |
25 ms |
21080 KB |
Output is correct |
9 |
Correct |
27 ms |
21080 KB |
Output is correct |
10 |
Correct |
16 ms |
20316 KB |
Output is correct |
11 |
Correct |
36 ms |
21772 KB |
Output is correct |
12 |
Correct |
3 ms |
20056 KB |
Output is correct |
13 |
Correct |
3 ms |
20060 KB |
Output is correct |
14 |
Correct |
3 ms |
20060 KB |
Output is correct |
15 |
Correct |
3 ms |
20060 KB |
Output is correct |
16 |
Correct |
3 ms |
20204 KB |
Output is correct |
17 |
Correct |
6 ms |
20316 KB |
Output is correct |
18 |
Correct |
7 ms |
20572 KB |
Output is correct |
19 |
Correct |
29 ms |
21340 KB |
Output is correct |
20 |
Correct |
52 ms |
22480 KB |
Output is correct |
21 |
Correct |
54 ms |
21840 KB |
Output is correct |
22 |
Correct |
37 ms |
21340 KB |
Output is correct |
23 |
Correct |
63 ms |
22808 KB |
Output is correct |
24 |
Correct |
30 ms |
21596 KB |
Output is correct |
25 |
Correct |
37 ms |
21340 KB |
Output is correct |
26 |
Correct |
47 ms |
21340 KB |
Output is correct |
27 |
Correct |
3 ms |
20060 KB |
Output is correct |
28 |
Correct |
3 ms |
20252 KB |
Output is correct |
29 |
Correct |
3 ms |
20056 KB |
Output is correct |
30 |
Correct |
3 ms |
20060 KB |
Output is correct |
31 |
Correct |
4 ms |
20060 KB |
Output is correct |
32 |
Correct |
4 ms |
20060 KB |
Output is correct |
33 |
Correct |
4 ms |
20060 KB |
Output is correct |
34 |
Correct |
4 ms |
20060 KB |
Output is correct |
35 |
Correct |
5 ms |
20292 KB |
Output is correct |
36 |
Correct |
23 ms |
21056 KB |
Output is correct |
37 |
Correct |
35 ms |
21596 KB |
Output is correct |
38 |
Correct |
45 ms |
22404 KB |
Output is correct |
39 |
Correct |
46 ms |
22640 KB |
Output is correct |
40 |
Correct |
41 ms |
21840 KB |
Output is correct |
41 |
Correct |
45 ms |
21420 KB |
Output is correct |
42 |
Correct |
35 ms |
21336 KB |
Output is correct |
43 |
Correct |
53 ms |
21588 KB |
Output is correct |
44 |
Correct |
4 ms |
20060 KB |
Output is correct |
45 |
Correct |
3 ms |
20060 KB |
Output is correct |
46 |
Correct |
3 ms |
20060 KB |
Output is correct |
47 |
Correct |
3 ms |
20056 KB |
Output is correct |
48 |
Correct |
3 ms |
20060 KB |
Output is correct |
49 |
Correct |
4 ms |
20060 KB |
Output is correct |
50 |
Correct |
4 ms |
20316 KB |
Output is correct |
51 |
Correct |
5 ms |
20260 KB |
Output is correct |
52 |
Correct |
5 ms |
20316 KB |
Output is correct |
53 |
Correct |
23 ms |
21072 KB |
Output is correct |
54 |
Correct |
53 ms |
22360 KB |
Output is correct |
55 |
Correct |
75 ms |
22516 KB |
Output is correct |
56 |
Correct |
45 ms |
21852 KB |
Output is correct |
57 |
Correct |
74 ms |
23428 KB |
Output is correct |
58 |
Correct |
36 ms |
21116 KB |
Output is correct |
59 |
Correct |
44 ms |
22020 KB |
Output is correct |