#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
using namespace std;
typedef pair<int, int> pi;
typedef long long ll;
int n, k, m, o;
vector<vector<pi>> adj_list;
vector<vector<pi>> rev_list;
vector<int> ans;
vector<vector<ll>> lefty;
vector<vector<ll>> righty;
vector<pi> initial;
void divide(int lx, int rx, vector<int>& queries)
{
if (!queries.size())
return;
if (rx - lx <= k)
return;
int sz = rx - lx;
int mid = lx;
while (mid - lx < (rx - lx) / 2)
mid += k;
for (int i = mid - k; i < mid; i++) {
for (int j = 0; j < k; j++)
lefty[i][j] = INT_MAX;
lefty[i][i % k] = 0;
}
for (int i = mid - k - 1; i >= lx; i--) {
for (int j = 0; j < k; j++)
lefty[i][j] = INT_MAX;
for (auto& neigh : adj_list[i])
for (int j = 0; j < k; j++)
lefty[i][j] = min(lefty[i][j], lefty[neigh.first][j] + neigh.second);
}
for (int i = mid; i < mid + k && i < rx; i++) {
for (int j = 0; j < k; j++)
righty[i][j] = INT_MAX;
righty[i][i % k] = 0;
}
for (int i = mid + k; i < rx; i++) {
for (int j = 0; j < k; j++)
righty[i][j] = INT_MAX;
for (auto& neigh : rev_list[i])
for (int j = 0; j < k; j++)
righty[i][j] = min(righty[i][j], righty[neigh.first][j] + neigh.second);
}
vector<int> q1, q2;
for (auto& q : queries) {
if (initial[q].first < mid) {
if (initial[q].second < mid)
q1.push_back(q);
else {
long long dist = INT_MAX;
int v1 = initial[q].first, v2 = initial[q].second;
for (int i = 0; i < k; i++) {
int node = mid - k + i;
for (auto neigh : adj_list[node])
dist = min(dist, lefty[v1][i] + neigh.second + righty[v2][neigh.first % k]);
}
ans[q] = (dist == INT_MAX ? -1 : dist);
}
}
else
q2.push_back(q);
}
divide(lx, mid, q1);
divide(mid, rx, q2);
}
int main(void)
{
scanf("%d %d %d %d", &k, &n, &m, &o);
adj_list.resize(n);
rev_list.resize(n);
ans.assign(o, -1);
initial.resize(o);
lefty.resize(n, vector<ll>(k));
righty.resize(n, vector<ll>(k));
for (int i = 0; i < m; i++) {
int a, b, t;
scanf("%d %d %d", &a, &b, &t);
adj_list[a].pb(mp(b ,t));
rev_list[b].pb(mp(a, t));
}
vector<int> everything(o);
for (int q = 0; q < o; q++) {
int l, r;
everything[q] = q;
scanf("%d %d", &l, &r);
initial[q] = mp(l, r);
}
divide(0, n, everything);
for (int i = 0; i < o; i++)
printf("%d\n", ans[i]);
return 0;
}
Compilation message
toll.cpp: In function 'void divide(int, int, std::vector<int>&)':
toll.cpp:26:9: warning: unused variable 'sz' [-Wunused-variable]
26 | int sz = rx - lx;
| ^~
toll.cpp: In function 'int main()':
toll.cpp:84:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | scanf("%d %d %d %d", &k, &n, &m, &o);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:94:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | scanf("%d %d %d", &a, &b, &t);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:104:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | scanf("%d %d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
12380 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
436 KB |
Output is correct |
8 |
Correct |
39 ms |
12364 KB |
Output is correct |
9 |
Correct |
40 ms |
12108 KB |
Output is correct |
10 |
Correct |
12 ms |
8404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
12664 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
3 ms |
768 KB |
Output is correct |
8 |
Correct |
5 ms |
852 KB |
Output is correct |
9 |
Correct |
39 ms |
12336 KB |
Output is correct |
10 |
Correct |
79 ms |
14564 KB |
Output is correct |
11 |
Correct |
56 ms |
12744 KB |
Output is correct |
12 |
Correct |
46 ms |
12480 KB |
Output is correct |
13 |
Correct |
86 ms |
12636 KB |
Output is correct |
14 |
Correct |
54 ms |
9548 KB |
Output is correct |
15 |
Correct |
44 ms |
9768 KB |
Output is correct |
16 |
Correct |
42 ms |
9780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
30 ms |
11952 KB |
Output is correct |
11 |
Correct |
54 ms |
12760 KB |
Output is correct |
12 |
Correct |
78 ms |
15144 KB |
Output is correct |
13 |
Correct |
93 ms |
15816 KB |
Output is correct |
14 |
Correct |
71 ms |
14092 KB |
Output is correct |
15 |
Correct |
40 ms |
9680 KB |
Output is correct |
16 |
Correct |
39 ms |
9640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
30 ms |
11952 KB |
Output is correct |
11 |
Correct |
54 ms |
12760 KB |
Output is correct |
12 |
Correct |
78 ms |
15144 KB |
Output is correct |
13 |
Correct |
93 ms |
15816 KB |
Output is correct |
14 |
Correct |
71 ms |
14092 KB |
Output is correct |
15 |
Correct |
40 ms |
9680 KB |
Output is correct |
16 |
Correct |
39 ms |
9640 KB |
Output is correct |
17 |
Correct |
61 ms |
12848 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
296 KB |
Output is correct |
23 |
Correct |
2 ms |
580 KB |
Output is correct |
24 |
Correct |
2 ms |
584 KB |
Output is correct |
25 |
Correct |
3 ms |
784 KB |
Output is correct |
26 |
Correct |
3 ms |
692 KB |
Output is correct |
27 |
Correct |
37 ms |
12108 KB |
Output is correct |
28 |
Correct |
81 ms |
15308 KB |
Output is correct |
29 |
Correct |
92 ms |
15992 KB |
Output is correct |
30 |
Correct |
64 ms |
14088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
12380 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
436 KB |
Output is correct |
8 |
Correct |
39 ms |
12364 KB |
Output is correct |
9 |
Correct |
40 ms |
12108 KB |
Output is correct |
10 |
Correct |
12 ms |
8404 KB |
Output is correct |
11 |
Correct |
70 ms |
12664 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
3 ms |
768 KB |
Output is correct |
18 |
Correct |
5 ms |
852 KB |
Output is correct |
19 |
Correct |
39 ms |
12336 KB |
Output is correct |
20 |
Correct |
79 ms |
14564 KB |
Output is correct |
21 |
Correct |
56 ms |
12744 KB |
Output is correct |
22 |
Correct |
46 ms |
12480 KB |
Output is correct |
23 |
Correct |
86 ms |
12636 KB |
Output is correct |
24 |
Correct |
54 ms |
9548 KB |
Output is correct |
25 |
Correct |
44 ms |
9768 KB |
Output is correct |
26 |
Correct |
42 ms |
9780 KB |
Output is correct |
27 |
Correct |
1 ms |
300 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
296 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
300 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
3 ms |
724 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
30 ms |
11952 KB |
Output is correct |
37 |
Correct |
54 ms |
12760 KB |
Output is correct |
38 |
Correct |
78 ms |
15144 KB |
Output is correct |
39 |
Correct |
93 ms |
15816 KB |
Output is correct |
40 |
Correct |
71 ms |
14092 KB |
Output is correct |
41 |
Correct |
40 ms |
9680 KB |
Output is correct |
42 |
Correct |
39 ms |
9640 KB |
Output is correct |
43 |
Correct |
61 ms |
12848 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
304 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
296 KB |
Output is correct |
49 |
Correct |
2 ms |
580 KB |
Output is correct |
50 |
Correct |
2 ms |
584 KB |
Output is correct |
51 |
Correct |
3 ms |
784 KB |
Output is correct |
52 |
Correct |
3 ms |
692 KB |
Output is correct |
53 |
Correct |
37 ms |
12108 KB |
Output is correct |
54 |
Correct |
81 ms |
15308 KB |
Output is correct |
55 |
Correct |
92 ms |
15992 KB |
Output is correct |
56 |
Correct |
64 ms |
14088 KB |
Output is correct |
57 |
Correct |
133 ms |
18572 KB |
Output is correct |
58 |
Correct |
40 ms |
12332 KB |
Output is correct |
59 |
Correct |
76 ms |
13228 KB |
Output is correct |