This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 A[MAX], B[MAX];
vector<int> blocks[MAX];
int L[MAX][5], R[MAX][5];
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]) for (int t = 0; t < 5; ++t) L[u][t] = INF;
for (int i = mid + 1; i <= r; ++i) for (int &u: blocks[i]) for (int t = 0; t < 5; ++t) R[u][t] = INF;
for (int &u: blocks[mid]) for (int t = 0; t < 5; ++t) L[u][t] = 0;
for (int i = mid; i > l; --i) {
for (int &u: blocks[i]) {
for (int t = 0; t < 5; ++t) {
for (pair<int, int> &x: adj[0][u]) {
int v = x.first, w = x.second;
minimize(L[v][t], L[u][t] + w);
}
}
}
}
for (int &u: blocks[mid]) {
for (int t = 0; t < 5; ++t) {
for (pair<int, int> &x: adj[1][u]) {
int v = x.first, w = x.second;
minimize(R[v][t], w);
}
}
}
for (int i = mid + 1; i < r; ++i) {
for (int &u: blocks[i]) {
for (int t = 0; t < 5; ++t) {
for (pair<int, int> &x: adj[1][u]) {
int v = x.first, w = x.second;
minimize(R[v][t], R[u][t] + w);
}
}
}
}
vector<int> todo[2];
for (int &id: queries) {
if (A[id] / k <= mid && mid < B[id] / k) {
for (int t = 0; t < 5; ++t) {
ans[id] = min(ans[id], L[A[id]][t] + R[B[id]][t]);
}
} 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 (stderr)
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:83:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:84:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |