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 "C"
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
int k, n, m, o;
int dp[50000][16][5][5], ans[5][5], tmp[5][5];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> k >> n >> m >> o;
memset(dp, 0x3f, sizeof(dp));
while (m--) {
int a, b, t;
cin >> a >> b >> t;
dp[a / k][0][a % k][b % k] = t;
}
for (int j = 1; j <= 15; j++) for (int i = 0; i + (1 << j) < (n + k - 1) / k; i++) {
for (int x = 0; x < k; x++) for (int y = 0; y < k; y++) for (int z = 0; z < k; z++) {
dp[i][j][x][y] = min(dp[i][j][x][y], dp[i][j - 1][x][z] + dp[i + (1 << j - 1)][j - 1][z][y]);
}
}
while (o--) {
int a, b; cin >> a >> b;
int u = a / k, v = b / k;
memset(ans, 0x3f, sizeof(ans));
for (int i = 0; i < k; i++) ans[i][i] = 0;
for (int j = 15; j >= 0; j--) {
if (u + (1 << j) <= v) {
memset(tmp, 0x3f, sizeof(tmp));
for (int x = 0; x < k; x++) {
for (int y = 0; y < k; y++) {
for (int z = 0; z < k; z++) {
tmp[x][y] = min(tmp[x][y], ans[x][z] + dp[u][j][z][y]);
}
}
}
memcpy(ans, tmp, sizeof(ans));
u += 1 << j;
}
}
cout << (ans[a % k][b % k] == 0x3f3f3f3f ? -1 : ans[a % k][b % k]) << '\n';
}
}
Compilation message (stderr)
toll.cpp: In function 'int main()':
toll.cpp:37:79: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
37 | dp[i][j][x][y] = min(dp[i][j][x][y], dp[i][j - 1][x][z] + dp[i + (1 << j - 1)][j - 1][z][y]);
| ~~^~~
toll.cpp:24:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:25:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | 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... |