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;
#ifdef MIKU
const string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
#define int long long
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
typedef pair<int, int> pii;
typedef array<array<int, 5>, 5> MK;
const int MXN = 50005, INF = 3.9e18;
int n, m, k, q;
MK bgd[MXN], emp;
MK operator+(MK a, MK b) {
MK ans = emp;
FOR(i, 0, k) FOR(j, 0, k) {
FOR(c, 0, k) ans[i][j] = min(ans[i][j], a[i][c] + b[c][j]);
}
return ans;
}
#define mid ((l + r) >> 1)
struct SMG {
MK val[MXN * 4];
vector<MK> v;
void build(int id, int l, int r) {
if (l + 1 == r) {
val[id] = bgd[l];
return;
}
build(id * 2 + 1, l, mid);
build(id * 2 + 2, mid, r);
val[id] = val[id * 2 + 1] + val[id * 2 + 2];
}
MK query(int id, int l, int r, int _l, int _r) {
if (_l <= l && r <= _r) return val[id];
if (_l < mid && mid < _r) return query(id * 2 + 1, l, mid, _l, _r) + query(id * 2 + 2, mid, r, _l, _r);
if (_l < mid) return query(id * 2 + 1, l, mid, _l, _r);
return query(id * 2 + 2, mid, r, _l, _r);
}
} smg;
#undef mid
void init() {
FOR(i, 0, 5) FOR(j, 0, 5) emp[i][j] = INF;
}
void PUSH_EDGE(MK &m, int x, int y, int z) {
m[x][y] = min(m[x][y], z);
}
void miku() {
int a, b, c;
cin >> k >> n >> m >> q;
int N = (n - 1) / k + 1;
fill(bgd, bgd + N, emp);
while (m--) {
cin >> a >> b >> c;
PUSH_EDGE(bgd[a / k], a % k, b % k, c);
}
smg.build(0, 0, N);
// MK ans = bgd[0] + bgd[1];
// FOR(i, 0, k) {
// FOR(j, 0, k) cout << (ans[i][j] == INF ? -1 : ans[i][j]) << ' ';
// cout << '\n';
// }
while (q--) {
cin >> a >> b;
if (a / k == b / k) {
cout << -1 << '\n';
continue;
}
debug(a / k, b / k);
MK ans = smg.query(0, 0, N, a / k, b / k);
// FOR(i, 0, k) {
// FOR(j, 0, k) cout << (ans[i][j] == INF ? -1 : ans[i][j]) << ' ';
// cout << '\n';
// }
cout << (ans[a % k][b % k] == INF ? -1 : ans[a % k][b % k]) << '\n';
}
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(cin.failbit);
init();
miku();
return 0;
}
Compilation message (stderr)
toll.cpp: In function 'void miku()':
toll.cpp:11:20: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) 39
| ^~
toll.cpp:87:9: note: in expansion of macro 'debug'
87 | debug(a / k, b / k);
| ^~~~~
# | 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... |