#include <bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define pii pair<int, int>
typedef long long ll;
using namespace std;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const int OFF = (1 << 20);
int k, n, ed, q, a, b, c;
vector<pii> v[50005];
vector<pii> rev[50005];
vector<int> br[50005];
map<int, int> m[50005];
map<int, int> bio[50005];
void rek(int l, int r) {
if (l >= r) return;
int mid = (l+r)/2;
for (int i = mid-1; i >= l; i--) {
for (int j = 0; j < br[mid].size(); j++) {
int poc = br[mid][j];
for (int w = 0; w < br[i+1].size(); w++) {
int cvor = br[i+1][w];
if (m[cvor][poc] == 0 && cvor != poc) continue;
for (int slj = 0; slj < rev[cvor].size(); slj++) {
int sus = rev[cvor][slj].X;
int dist = m[cvor][poc]+rev[cvor][slj].Y;
if (m[sus][poc] == 0 || m[sus][poc] > dist) m[sus][poc] = dist;
}
}
}
}
for (int i = mid+2; i <= r; i++) {
for (int j = 0; j < br[mid+1].size(); j++) {
int poc = br[mid+1][j];
for (int w = 0; w < br[i-1].size(); w++) {
int cvor = br[i-1][w];
if (m[poc][cvor] == 0 && cvor != poc) continue;
for (int slj = 0; slj < v[cvor].size(); slj++) {
int sus = v[cvor][slj].X;
int dist = m[poc][cvor]+v[cvor][slj].Y;
if (m[poc][sus] == 0 || m[poc][sus] > dist) m[poc][sus] = dist;
}
}
}
}
rek(l, mid);
rek(mid+1, r);
}
int trazimid(int l, int r, int lo, int hi) {
if (hi < l || r < lo) return -1;
int mid = (lo+hi)/2;
if (l <= mid && mid <= r) return mid;
return max(trazimid(l, r, lo, mid), trazimid(l, r, mid+1, hi));
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> k >> n >> ed >> q;
for (int i = 0; i < ed; i++) {
cin >> a >> b >> c;
v[a].push_back({b, c});
rev[b].push_back({a, c});
}
for (int i = 0; i < n; i++) br[i/k].push_back(i);
rek(0, (n-1)/k);
for (int i = 0; i < q; i++) {
cin >> a >> b;
int sred = trazimid(a/k, b/k, 0, (n-1)/k);
if (sred == b/k) {
if (m[a][b] == 0) cout << "-1\n";
else cout << m[a][b] << "\n";
}
else {
int mini = 1e9;
for (int j = 0; j < br[sred].size(); j++) {
int cvor1 = br[sred][j];
for (int w = 0; w < v[cvor1].size(); w++) {
int cvor2 = v[cvor1][w].X;
int tez = v[cvor1][w].Y;
if (m[a][cvor1] == 0 && a != cvor1) continue;
if (m[cvor2][b] == 0 && b != cvor2) continue;
mini = min(mini, m[a][cvor1]+tez+m[cvor2][b]);
}
}
if (mini == 1e9) cout << "-1\n";
else cout << mini << "\n";
}
}
return 0;
}
Compilation message
toll.cpp: In function 'void rek(int, int)':
toll.cpp:26:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for (int j = 0; j < br[mid].size(); j++) {
| ~~^~~~~~~~~~~~~~~~
toll.cpp:28:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for (int w = 0; w < br[i+1].size(); w++) {
| ~~^~~~~~~~~~~~~~~~
toll.cpp:32:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int slj = 0; slj < rev[cvor].size(); slj++) {
| ~~~~^~~~~~~~~~~~~~~~~~
toll.cpp:41:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int j = 0; j < br[mid+1].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~
toll.cpp:43:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int w = 0; w < br[i-1].size(); w++) {
| ~~^~~~~~~~~~~~~~~~
toll.cpp:47:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int slj = 0; slj < v[cvor].size(); slj++) {
| ~~~~^~~~~~~~~~~~~~~~
toll.cpp: In function 'int main()':
toll.cpp:93:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for (int j = 0; j < br[sred].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~
toll.cpp:95:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for (int w = 0; w < v[cvor1].size(); w++) {
| ~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
46296 KB |
Output is correct |
2 |
Correct |
5 ms |
8536 KB |
Output is correct |
3 |
Correct |
5 ms |
8536 KB |
Output is correct |
4 |
Correct |
5 ms |
8480 KB |
Output is correct |
5 |
Correct |
6 ms |
8916 KB |
Output is correct |
6 |
Correct |
7 ms |
8916 KB |
Output is correct |
7 |
Correct |
7 ms |
8984 KB |
Output is correct |
8 |
Correct |
135 ms |
46304 KB |
Output is correct |
9 |
Correct |
111 ms |
46008 KB |
Output is correct |
10 |
Correct |
79 ms |
42344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
351 ms |
73668 KB |
Output is correct |
2 |
Correct |
5 ms |
8532 KB |
Output is correct |
3 |
Correct |
4 ms |
8548 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Correct |
5 ms |
8532 KB |
Output is correct |
6 |
Correct |
4 ms |
8532 KB |
Output is correct |
7 |
Correct |
8 ms |
9044 KB |
Output is correct |
8 |
Correct |
15 ms |
9428 KB |
Output is correct |
9 |
Correct |
130 ms |
46268 KB |
Output is correct |
10 |
Correct |
623 ms |
100620 KB |
Output is correct |
11 |
Correct |
378 ms |
73808 KB |
Output is correct |
12 |
Correct |
299 ms |
97988 KB |
Output is correct |
13 |
Correct |
738 ms |
91396 KB |
Output is correct |
14 |
Correct |
366 ms |
61420 KB |
Output is correct |
15 |
Correct |
488 ms |
87664 KB |
Output is correct |
16 |
Correct |
479 ms |
87512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8536 KB |
Output is correct |
2 |
Correct |
6 ms |
8540 KB |
Output is correct |
3 |
Correct |
4 ms |
8540 KB |
Output is correct |
4 |
Correct |
5 ms |
8532 KB |
Output is correct |
5 |
Correct |
6 ms |
8536 KB |
Output is correct |
6 |
Correct |
6 ms |
8916 KB |
Output is correct |
7 |
Correct |
8 ms |
9192 KB |
Output is correct |
8 |
Correct |
15 ms |
10044 KB |
Output is correct |
9 |
Correct |
13 ms |
9812 KB |
Output is correct |
10 |
Correct |
117 ms |
46156 KB |
Output is correct |
11 |
Correct |
344 ms |
73556 KB |
Output is correct |
12 |
Correct |
608 ms |
100496 KB |
Output is correct |
13 |
Correct |
626 ms |
101324 KB |
Output is correct |
14 |
Correct |
531 ms |
99404 KB |
Output is correct |
15 |
Correct |
473 ms |
87548 KB |
Output is correct |
16 |
Correct |
483 ms |
87516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8536 KB |
Output is correct |
2 |
Correct |
6 ms |
8540 KB |
Output is correct |
3 |
Correct |
4 ms |
8540 KB |
Output is correct |
4 |
Correct |
5 ms |
8532 KB |
Output is correct |
5 |
Correct |
6 ms |
8536 KB |
Output is correct |
6 |
Correct |
6 ms |
8916 KB |
Output is correct |
7 |
Correct |
8 ms |
9192 KB |
Output is correct |
8 |
Correct |
15 ms |
10044 KB |
Output is correct |
9 |
Correct |
13 ms |
9812 KB |
Output is correct |
10 |
Correct |
117 ms |
46156 KB |
Output is correct |
11 |
Correct |
344 ms |
73556 KB |
Output is correct |
12 |
Correct |
608 ms |
100496 KB |
Output is correct |
13 |
Correct |
626 ms |
101324 KB |
Output is correct |
14 |
Correct |
531 ms |
99404 KB |
Output is correct |
15 |
Correct |
473 ms |
87548 KB |
Output is correct |
16 |
Correct |
483 ms |
87516 KB |
Output is correct |
17 |
Correct |
349 ms |
73496 KB |
Output is correct |
18 |
Correct |
5 ms |
8424 KB |
Output is correct |
19 |
Correct |
5 ms |
8532 KB |
Output is correct |
20 |
Correct |
5 ms |
8532 KB |
Output is correct |
21 |
Correct |
5 ms |
8660 KB |
Output is correct |
22 |
Correct |
5 ms |
8532 KB |
Output is correct |
23 |
Correct |
7 ms |
9044 KB |
Output is correct |
24 |
Correct |
12 ms |
9340 KB |
Output is correct |
25 |
Correct |
23 ms |
10000 KB |
Output is correct |
26 |
Correct |
18 ms |
9788 KB |
Output is correct |
27 |
Correct |
121 ms |
46084 KB |
Output is correct |
28 |
Correct |
650 ms |
100500 KB |
Output is correct |
29 |
Correct |
630 ms |
101292 KB |
Output is correct |
30 |
Correct |
519 ms |
99484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
46296 KB |
Output is correct |
2 |
Correct |
5 ms |
8536 KB |
Output is correct |
3 |
Correct |
5 ms |
8536 KB |
Output is correct |
4 |
Correct |
5 ms |
8480 KB |
Output is correct |
5 |
Correct |
6 ms |
8916 KB |
Output is correct |
6 |
Correct |
7 ms |
8916 KB |
Output is correct |
7 |
Correct |
7 ms |
8984 KB |
Output is correct |
8 |
Correct |
135 ms |
46304 KB |
Output is correct |
9 |
Correct |
111 ms |
46008 KB |
Output is correct |
10 |
Correct |
79 ms |
42344 KB |
Output is correct |
11 |
Correct |
351 ms |
73668 KB |
Output is correct |
12 |
Correct |
5 ms |
8532 KB |
Output is correct |
13 |
Correct |
4 ms |
8548 KB |
Output is correct |
14 |
Correct |
4 ms |
8532 KB |
Output is correct |
15 |
Correct |
5 ms |
8532 KB |
Output is correct |
16 |
Correct |
4 ms |
8532 KB |
Output is correct |
17 |
Correct |
8 ms |
9044 KB |
Output is correct |
18 |
Correct |
15 ms |
9428 KB |
Output is correct |
19 |
Correct |
130 ms |
46268 KB |
Output is correct |
20 |
Correct |
623 ms |
100620 KB |
Output is correct |
21 |
Correct |
378 ms |
73808 KB |
Output is correct |
22 |
Correct |
299 ms |
97988 KB |
Output is correct |
23 |
Correct |
738 ms |
91396 KB |
Output is correct |
24 |
Correct |
366 ms |
61420 KB |
Output is correct |
25 |
Correct |
488 ms |
87664 KB |
Output is correct |
26 |
Correct |
479 ms |
87512 KB |
Output is correct |
27 |
Correct |
5 ms |
8536 KB |
Output is correct |
28 |
Correct |
6 ms |
8540 KB |
Output is correct |
29 |
Correct |
4 ms |
8540 KB |
Output is correct |
30 |
Correct |
5 ms |
8532 KB |
Output is correct |
31 |
Correct |
6 ms |
8536 KB |
Output is correct |
32 |
Correct |
6 ms |
8916 KB |
Output is correct |
33 |
Correct |
8 ms |
9192 KB |
Output is correct |
34 |
Correct |
15 ms |
10044 KB |
Output is correct |
35 |
Correct |
13 ms |
9812 KB |
Output is correct |
36 |
Correct |
117 ms |
46156 KB |
Output is correct |
37 |
Correct |
344 ms |
73556 KB |
Output is correct |
38 |
Correct |
608 ms |
100496 KB |
Output is correct |
39 |
Correct |
626 ms |
101324 KB |
Output is correct |
40 |
Correct |
531 ms |
99404 KB |
Output is correct |
41 |
Correct |
473 ms |
87548 KB |
Output is correct |
42 |
Correct |
483 ms |
87516 KB |
Output is correct |
43 |
Correct |
349 ms |
73496 KB |
Output is correct |
44 |
Correct |
5 ms |
8424 KB |
Output is correct |
45 |
Correct |
5 ms |
8532 KB |
Output is correct |
46 |
Correct |
5 ms |
8532 KB |
Output is correct |
47 |
Correct |
5 ms |
8660 KB |
Output is correct |
48 |
Correct |
5 ms |
8532 KB |
Output is correct |
49 |
Correct |
7 ms |
9044 KB |
Output is correct |
50 |
Correct |
12 ms |
9340 KB |
Output is correct |
51 |
Correct |
23 ms |
10000 KB |
Output is correct |
52 |
Correct |
18 ms |
9788 KB |
Output is correct |
53 |
Correct |
121 ms |
46084 KB |
Output is correct |
54 |
Correct |
650 ms |
100500 KB |
Output is correct |
55 |
Correct |
630 ms |
101292 KB |
Output is correct |
56 |
Correct |
519 ms |
99484 KB |
Output is correct |
57 |
Correct |
961 ms |
127172 KB |
Output is correct |
58 |
Correct |
122 ms |
46308 KB |
Output is correct |
59 |
Correct |
382 ms |
73876 KB |
Output is correct |