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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fr first
#define sc second
#define endl '\n'
#define pb push_back
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pw(x) (1ll << x)
#pragma GCC optimize ("-O3")
#pragma GCC optimize ("unroll-loops")
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef long double ld;
template <typename T> inline bool umax (T &a, const T &b) { return (a < b ? a = b, 1 : 0); }
template <typename T> inline bool umin (T &a, const T &b) { return (a > b ? a = b, 1 : 0); }
template <typename T>
using oset = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 5e4 + 100;
const int K = 5;
const int LG = 17;
int up[N][K][K][LG];
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
for (int i = 0; i < N; i++) for (int j = 0; j < K; j++) for (int k = 0; k < K; k++) for (int q = 0; q < LG; q++) up[i][j][k][q] = 1e9;
int k, n, m, q; cin >> k >> n >> m >> q;
for (int i = 0; i < m; i++) {
int a, b, c; cin >> a >> b >> c;
umin(up[a / k][a % k][b % k][0], c);
}
for (int d = 1; d < LG; d++) {
for (int i = 0; i < N; i++) {
if (i + pw(d) >= N) break;
for (int x = 0; x < k; x++) {
vector <int> dst (k, 1e9);
for (int y = 0; y < k; y++) {
for (int z = 0; z < k; z++) {
umin(dst[z], up[i][x][y][d - 1] + up[i + pw(d - 1)][y][z][d - 1]);
}
}
for (int y = 0; y < k; y++) up[i][x][y][d] = dst[y];
}
}
}
while (q--) {
int a, b; cin >> a >> b;
vector <int> dst (k, 1e9);
dst[a % k] = 0;
int ba = a / k, bb = b / k;
for (int j = LG - 1; j >= 0; j--) {
if (ba + pw(j) > bb) continue;
vector <int> dstt (k, 1e9);
for (int x = 0; x < k; x++) {
for (int y = 0; y < k; y++) {
umin(dstt[y], dst[x] + up[ba][x][y][j]);
}
}
dst = dstt;
ba += pw(j);
}
cout << (dst[b % k] == 1e9 ? -1 : dst[b % k]) << endl;
}
return 0;
}
Compilation message (stderr)
toll.cpp: In function 'int main()':
toll.cpp:53:71: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
53 | umin(dst[z], up[i][x][y][d - 1] + up[i + pw(d - 1)][y][z][d - 1]);
| ~~^~~
toll.cpp:11:23: note: in definition of macro 'pw'
11 | #define pw(x) (1ll << x)
| ^
# | 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... |