#include <bits/stdc++.h>
using namespace std;
#define task ""
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i)
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};
const int INF = 1e9 + 7;
const int MAX = 50000;
int k, n, m, q;
int ans[MAX];
int L[MAX], R[MAX];
int A[MAX], B[MAX];
vector<int> blocks[MAX];
vector<pair<int, int>> adj[2][MAX];
void divi(int l, int r, vector<int> &queries) {
if (queries.empty()) return;
if (l == r) {
for (int &id: queries) {
ans[id] = A[id] == B[id] ? 0 : INF;
}
return;
}
int mid = l + r >> 1;
for (int i = l; i <= mid - 1; ++i) for (int &u: blocks[i]) L[u] = INF;
for (int i = mid + 1; i <= r; ++i) for (int &u: blocks[i]) R[u] = INF;
for (int &u: blocks[mid]) L[u] = 0;
for (int i = mid; i > l; --i) {
for (int &u: blocks[i]) {
for (pair<int, int> &x: adj[0][u]) {
int v = x.first, w = x.second;
minimize(L[v], L[u] + w);
}
}
}
for (int &u: blocks[mid]) {
for (pair<int, int> &x: adj[1][u]) {
int v = x.first, w = x.second;
minimize(R[v], w);
}
}
for (int i = mid + 1; i < r; ++i) {
for (int &u: blocks[i]) {
for (pair<int, int> &x: adj[1][u]) {
int v = x.first, w = x.second;
minimize(R[v], R[u] + w);
}
}
}
vector<int> todo[2];
for (int &id: queries) {
if (A[id] / k <= mid && mid < B[id] / k) {
ans[id] = min(ans[id], L[A[id]] + R[B[id]]);
} else {
todo[A[id] / k > mid].push_back(id);
}
}
divi(l, mid, todo[0]); divi(mid + 1, r, todo[1]);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> k >> n >> m >> q;
while (m--) {
int a, b, t; cin >> a >> b >> t;
adj[1][a].push_back({b, t});
adj[0][b].push_back({a, t});
}
for (int i = 0; i < n; ++i) {
blocks[i/k].push_back(i);
}
vector<int> queries;
for (int i = 0; i < q; ++i) {
ans[i] = INF;
cin >> A[i] >> B[i];
queries.push_back(i);
}
divi(0, (n + k - 1) / k - 1, queries);
for (int i = 0; i < q; ++i) {
if (ans[i] == INF) ans[i] = -1;
cout << ans[i] << '\n';
}
cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
return 0;
}
Compilation message
toll.cpp: In function 'void divi(int, int, std::vector<int>&)':
toll.cpp:34:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
34 | int mid = l + r >> 1;
| ~~^~~
toll.cpp: In function 'int main()':
toll.cpp:75:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:76:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
9200 KB |
Output is correct |
2 |
Correct |
2 ms |
3852 KB |
Output is correct |
3 |
Correct |
2 ms |
3796 KB |
Output is correct |
4 |
Correct |
2 ms |
3796 KB |
Output is correct |
5 |
Correct |
4 ms |
3996 KB |
Output is correct |
6 |
Correct |
3 ms |
3924 KB |
Output is correct |
7 |
Correct |
3 ms |
3924 KB |
Output is correct |
8 |
Correct |
26 ms |
10084 KB |
Output is correct |
9 |
Correct |
21 ms |
9812 KB |
Output is correct |
10 |
Correct |
7 ms |
6100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
32 ms |
8268 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3796 KB |
Output is correct |
2 |
Incorrect |
3 ms |
3796 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3796 KB |
Output is correct |
2 |
Incorrect |
3 ms |
3796 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
9200 KB |
Output is correct |
2 |
Correct |
2 ms |
3852 KB |
Output is correct |
3 |
Correct |
2 ms |
3796 KB |
Output is correct |
4 |
Correct |
2 ms |
3796 KB |
Output is correct |
5 |
Correct |
4 ms |
3996 KB |
Output is correct |
6 |
Correct |
3 ms |
3924 KB |
Output is correct |
7 |
Correct |
3 ms |
3924 KB |
Output is correct |
8 |
Correct |
26 ms |
10084 KB |
Output is correct |
9 |
Correct |
21 ms |
9812 KB |
Output is correct |
10 |
Correct |
7 ms |
6100 KB |
Output is correct |
11 |
Incorrect |
32 ms |
8268 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |