#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];
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) {
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;
res = min(res, lift[x][chap][i] + get_min(y, h - (1 << chap), p));
}
return 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);
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;
adj[x].pb({y, z});
}
}
int main() {
faster; //free();
input();
setdef();
solve();
}
Compilation message
toll.cpp: In function 'void free()':
toll.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen("inputi.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
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("out.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
39884 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
3 ms |
5032 KB |
Output is correct |
5 |
Correct |
4 ms |
5676 KB |
Output is correct |
6 |
Correct |
4 ms |
5716 KB |
Output is correct |
7 |
Correct |
4 ms |
5680 KB |
Output is correct |
8 |
Correct |
50 ms |
40236 KB |
Output is correct |
9 |
Correct |
57 ms |
40136 KB |
Output is correct |
10 |
Correct |
34 ms |
38436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
39964 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
5028 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 |
6 ms |
5844 KB |
Output is correct |
8 |
Correct |
11 ms |
5904 KB |
Output is correct |
9 |
Correct |
50 ms |
40276 KB |
Output is correct |
10 |
Correct |
1818 ms |
41364 KB |
Output is correct |
11 |
Correct |
121 ms |
40392 KB |
Output is correct |
12 |
Correct |
1815 ms |
40288 KB |
Output is correct |
13 |
Execution timed out |
3071 ms |
28320 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
5 ms |
5716 KB |
Output is correct |
7 |
Correct |
7 ms |
5684 KB |
Output is correct |
8 |
Correct |
8 ms |
5820 KB |
Output is correct |
9 |
Correct |
7 ms |
5716 KB |
Output is correct |
10 |
Correct |
52 ms |
40120 KB |
Output is correct |
11 |
Correct |
74 ms |
40160 KB |
Output is correct |
12 |
Correct |
115 ms |
41012 KB |
Output is correct |
13 |
Correct |
149 ms |
41160 KB |
Output is correct |
14 |
Correct |
122 ms |
40476 KB |
Output is correct |
15 |
Correct |
281 ms |
26296 KB |
Output is correct |
16 |
Correct |
651 ms |
26416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
5 ms |
5716 KB |
Output is correct |
7 |
Correct |
7 ms |
5684 KB |
Output is correct |
8 |
Correct |
8 ms |
5820 KB |
Output is correct |
9 |
Correct |
7 ms |
5716 KB |
Output is correct |
10 |
Correct |
52 ms |
40120 KB |
Output is correct |
11 |
Correct |
74 ms |
40160 KB |
Output is correct |
12 |
Correct |
115 ms |
41012 KB |
Output is correct |
13 |
Correct |
149 ms |
41160 KB |
Output is correct |
14 |
Correct |
122 ms |
40476 KB |
Output is correct |
15 |
Correct |
281 ms |
26296 KB |
Output is correct |
16 |
Correct |
651 ms |
26416 KB |
Output is correct |
17 |
Correct |
87 ms |
39884 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
4 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
4 ms |
5716 KB |
Output is correct |
24 |
Correct |
6 ms |
5716 KB |
Output is correct |
25 |
Correct |
37 ms |
5796 KB |
Output is correct |
26 |
Correct |
23 ms |
5908 KB |
Output is correct |
27 |
Correct |
47 ms |
39844 KB |
Output is correct |
28 |
Correct |
394 ms |
40888 KB |
Output is correct |
29 |
Correct |
348 ms |
41164 KB |
Output is correct |
30 |
Correct |
402 ms |
40636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
39884 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
3 ms |
5032 KB |
Output is correct |
5 |
Correct |
4 ms |
5676 KB |
Output is correct |
6 |
Correct |
4 ms |
5716 KB |
Output is correct |
7 |
Correct |
4 ms |
5680 KB |
Output is correct |
8 |
Correct |
50 ms |
40236 KB |
Output is correct |
9 |
Correct |
57 ms |
40136 KB |
Output is correct |
10 |
Correct |
34 ms |
38436 KB |
Output is correct |
11 |
Correct |
130 ms |
39964 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
5028 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 |
6 ms |
5844 KB |
Output is correct |
18 |
Correct |
11 ms |
5904 KB |
Output is correct |
19 |
Correct |
50 ms |
40276 KB |
Output is correct |
20 |
Correct |
1818 ms |
41364 KB |
Output is correct |
21 |
Correct |
121 ms |
40392 KB |
Output is correct |
22 |
Correct |
1815 ms |
40288 KB |
Output is correct |
23 |
Execution timed out |
3071 ms |
28320 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |