#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<bitset>
#include<queue>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<unordered_map>
#include<list>
#include<utility>
#include<stack>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
#define cl clear
#define F first
#define S second
#define pb push_back
#define Sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define faster ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const int MAX_N = 2e5 + 25;
const ll INF = 1e10;
const int inf = 1e9;
const int lgg = 17;
int k, n, m, q;
vector<pii> adj[MAX_N];
ll lift[MAX_N][lgg][5];
map<pii, ll> mp;
void free() {
#ifndef ONLINE_JUDGE
freopen("inputi.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
}
void build() {
for(int i = 0; i < lgg; i++) {
for(int j = 0; j < n; j++) {
if(i == 0) {
for(auto p : adj[j]) {
int x = p.first%k;
lift[j][i][x] = p.second;
}
continue;
}
for(int p = 0; p < k; p++) {
for(int o = 0; o < k; o++) {
int x = j / k + (1 << (i - 1));
x = (x * k);
x+= o;
lift[j][i][p] = min(lift[j][i][p], lift[j][i-1][o] + lift[x][i - 1][p]);
}
}
}
}
}
void setdef() {
for(int i = 0; i < n; i++) {
for(int j = 0; j < lgg; j++) {
for(int p = 0; p < k; p++)
lift[i][j][p] = INF;
}
}
}
ll get_min(int x, int h, int p, int dest) {
if(h == 0) {
if(x%k == p)
return 0;
else
return INF;
}
int chap = 31 - __builtin_clz(h);
ll res = INF;
for(int i = 0; i < k; i++) {
int y = x / k + (1 << (chap));
y = (y * k);
y+= i;
if(mp[{y, dest}] != 0)
res = min(res, lift[x][chap][i] + mp[{y, dest}]);
else
res = min(res, lift[x][chap][i] + get_min(y, h - (1 << chap), p, dest));
}
return mp[{x, dest}] = res;
}
void solve() {
build();
while(q--) {
int x, y; cin >> x >> y;
int hx = x / k;
int hy = y / k;
if(hy < hx) {
cout << -1 << '\n';
}
else {
ll res = get_min(x, hy - hx, y%k, y);
if(res >= INF)
cout << -1 << '\n';
else
cout << res << '\n';
}
}
}
void input() {
cin >> k >> n >> m >> q;
for(int i = 1; i <= m; i++) {
int x, y, z; cin >> x >> y >> z;// aha are.
adj[x].pb({y, z});
}
}
int main() {
faster; //free();
input();
setdef();
solve();
}
Compilation message
toll.cpp: In function 'void free()':
toll.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen("inputi.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen("out.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
45248 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
6 ms |
5972 KB |
Output is correct |
6 |
Correct |
5 ms |
5972 KB |
Output is correct |
7 |
Correct |
6 ms |
5972 KB |
Output is correct |
8 |
Correct |
82 ms |
45236 KB |
Output is correct |
9 |
Correct |
80 ms |
45184 KB |
Output is correct |
10 |
Correct |
66 ms |
43480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
48932 KB |
Output is correct |
2 |
Correct |
3 ms |
5036 KB |
Output is correct |
3 |
Correct |
3 ms |
5036 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
9 ms |
6192 KB |
Output is correct |
8 |
Correct |
15 ms |
6484 KB |
Output is correct |
9 |
Correct |
74 ms |
45024 KB |
Output is correct |
10 |
Correct |
236 ms |
53560 KB |
Output is correct |
11 |
Correct |
150 ms |
48840 KB |
Output is correct |
12 |
Correct |
216 ms |
52448 KB |
Output is correct |
13 |
Correct |
174 ms |
33612 KB |
Output is correct |
14 |
Correct |
98 ms |
31724 KB |
Output is correct |
15 |
Correct |
154 ms |
33344 KB |
Output is correct |
16 |
Correct |
149 ms |
33264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
5092 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
5680 KB |
Output is correct |
7 |
Correct |
5 ms |
5812 KB |
Output is correct |
8 |
Correct |
6 ms |
5844 KB |
Output is correct |
9 |
Correct |
6 ms |
5844 KB |
Output is correct |
10 |
Correct |
46 ms |
40152 KB |
Output is correct |
11 |
Correct |
71 ms |
40264 KB |
Output is correct |
12 |
Correct |
107 ms |
41140 KB |
Output is correct |
13 |
Correct |
112 ms |
41428 KB |
Output is correct |
14 |
Correct |
110 ms |
40672 KB |
Output is correct |
15 |
Correct |
89 ms |
26672 KB |
Output is correct |
16 |
Correct |
85 ms |
26724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
5092 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
5680 KB |
Output is correct |
7 |
Correct |
5 ms |
5812 KB |
Output is correct |
8 |
Correct |
6 ms |
5844 KB |
Output is correct |
9 |
Correct |
6 ms |
5844 KB |
Output is correct |
10 |
Correct |
46 ms |
40152 KB |
Output is correct |
11 |
Correct |
71 ms |
40264 KB |
Output is correct |
12 |
Correct |
107 ms |
41140 KB |
Output is correct |
13 |
Correct |
112 ms |
41428 KB |
Output is correct |
14 |
Correct |
110 ms |
40672 KB |
Output is correct |
15 |
Correct |
89 ms |
26672 KB |
Output is correct |
16 |
Correct |
85 ms |
26724 KB |
Output is correct |
17 |
Correct |
99 ms |
42756 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5036 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
4948 KB |
Output is correct |
23 |
Correct |
9 ms |
6484 KB |
Output is correct |
24 |
Correct |
15 ms |
6996 KB |
Output is correct |
25 |
Correct |
32 ms |
7952 KB |
Output is correct |
26 |
Correct |
25 ms |
7636 KB |
Output is correct |
27 |
Correct |
53 ms |
41548 KB |
Output is correct |
28 |
Correct |
138 ms |
44748 KB |
Output is correct |
29 |
Correct |
163 ms |
44988 KB |
Output is correct |
30 |
Correct |
131 ms |
44368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
45248 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
6 ms |
5972 KB |
Output is correct |
6 |
Correct |
5 ms |
5972 KB |
Output is correct |
7 |
Correct |
6 ms |
5972 KB |
Output is correct |
8 |
Correct |
82 ms |
45236 KB |
Output is correct |
9 |
Correct |
80 ms |
45184 KB |
Output is correct |
10 |
Correct |
66 ms |
43480 KB |
Output is correct |
11 |
Correct |
146 ms |
48932 KB |
Output is correct |
12 |
Correct |
3 ms |
5036 KB |
Output is correct |
13 |
Correct |
3 ms |
5036 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
9 ms |
6192 KB |
Output is correct |
18 |
Correct |
15 ms |
6484 KB |
Output is correct |
19 |
Correct |
74 ms |
45024 KB |
Output is correct |
20 |
Correct |
236 ms |
53560 KB |
Output is correct |
21 |
Correct |
150 ms |
48840 KB |
Output is correct |
22 |
Correct |
216 ms |
52448 KB |
Output is correct |
23 |
Correct |
174 ms |
33612 KB |
Output is correct |
24 |
Correct |
98 ms |
31724 KB |
Output is correct |
25 |
Correct |
154 ms |
33344 KB |
Output is correct |
26 |
Correct |
149 ms |
33264 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
4 ms |
5092 KB |
Output is correct |
29 |
Correct |
3 ms |
5028 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
3 ms |
4948 KB |
Output is correct |
32 |
Correct |
4 ms |
5680 KB |
Output is correct |
33 |
Correct |
5 ms |
5812 KB |
Output is correct |
34 |
Correct |
6 ms |
5844 KB |
Output is correct |
35 |
Correct |
6 ms |
5844 KB |
Output is correct |
36 |
Correct |
46 ms |
40152 KB |
Output is correct |
37 |
Correct |
71 ms |
40264 KB |
Output is correct |
38 |
Correct |
107 ms |
41140 KB |
Output is correct |
39 |
Correct |
112 ms |
41428 KB |
Output is correct |
40 |
Correct |
110 ms |
40672 KB |
Output is correct |
41 |
Correct |
89 ms |
26672 KB |
Output is correct |
42 |
Correct |
85 ms |
26724 KB |
Output is correct |
43 |
Correct |
99 ms |
42756 KB |
Output is correct |
44 |
Correct |
3 ms |
4948 KB |
Output is correct |
45 |
Correct |
3 ms |
4948 KB |
Output is correct |
46 |
Correct |
3 ms |
5036 KB |
Output is correct |
47 |
Correct |
3 ms |
4948 KB |
Output is correct |
48 |
Correct |
3 ms |
4948 KB |
Output is correct |
49 |
Correct |
9 ms |
6484 KB |
Output is correct |
50 |
Correct |
15 ms |
6996 KB |
Output is correct |
51 |
Correct |
32 ms |
7952 KB |
Output is correct |
52 |
Correct |
25 ms |
7636 KB |
Output is correct |
53 |
Correct |
53 ms |
41548 KB |
Output is correct |
54 |
Correct |
138 ms |
44748 KB |
Output is correct |
55 |
Correct |
163 ms |
44988 KB |
Output is correct |
56 |
Correct |
131 ms |
44368 KB |
Output is correct |
57 |
Correct |
359 ms |
60328 KB |
Output is correct |
58 |
Correct |
78 ms |
45816 KB |
Output is correct |
59 |
Correct |
171 ms |
50680 KB |
Output is correct |