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>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
using ull = unsigned ll;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000009;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 5e4 + 1;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int k, n, m, o;
ll dp[maxN][18][5][5];
void Init(){
cin >> k >> n >> m >> o;
memset(dp, 0x3f, sizeof(dp));
int x = __lg(n) + 1;
for (int i = 1; i <= m; ++i){
int a, b, w;
cin >> a >> b >> w;
dp[a / k][0][a % k][b % k] = w;
}
for (int i = 1; i <= x; ++i){
for (int j = 0; j < n / k; ++j){
for (int p = 0; p < k; ++p){
for (int q = 0; q < k; ++q){
for (int r = 0; r < k; ++r){
dp[j][i][p][r] = min(dp[j][i][p][r], dp[j][i - 1][p][q] + dp[j + (1 << (i - 1))][i - 1][q][r]);
}
}
}
}
}
while (o--){
int a, b;
cin >> a >> b;
int u = a % k, v = b % k, s = a / k, t = b / k - a / k;
ll ans[5], tem[5];
memset(ans, 0x3f, sizeof(ans));
memset(tem, 0x3f, sizeof(ans));
tem[u] = 0;
while (t){
int diff = t & -t;
int lg = __lg(diff);
for (int p = 0; p < 5; ++p){
for (int q = 0; q < 5; ++q){
ans[p] = min(ans[p], tem[q] + dp[s][lg][q][p]);
}
}
for (int p = 0; p < 5; ++p){
tem[p] = ans[p];
ans[p] = inf;
}
s = s + diff;
t ^= diff;
}
cout << ((tem[v] - inf) ? tem[v] : -1) << "\n";
}
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}
Compilation message (stderr)
toll.cpp: In function 'int main()':
toll.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |