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;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// -------------------------------------------------- Main Code --------------------------------------------------
int k, n, m, o;
vector<vector<vii>> dp(50000, vector<vii>(17, vii(5, vi(5, inf))));
void combine(vii &ans, vii &curr, vii &dp) {
for (int x = 0; x < k; x++) {
for (int y = 0; y < k; y++) {
for (int z = 0; z < k; z++) {
ans[x][y] = min(ans[x][y], curr[x][z] + dp[z][y]);
}
}
}
}
void sol() {
cin >> k >> n >> m >> o;
for (int i = 0; i < m; i++) {
int a, b, c; cin >> a >> b >> c;
dp[a/k][0][a%k][b%k] = c;
}
for (int j = 1; j <= 16; j++) {
for (int i = 0; i < n; i++) {
if (i + (1<<j) >= n) break;
combine(dp[i][j], dp[i][j-1], dp[i+(1<<j-1)][j-1]);
}
}
while (o--) {
int a, b; cin >> a >> b;
if (a > b) {cout << -1 << endl; continue;}
vii ans(5, vi(5, inf));
for (int i = 0; i < k; i++) ans[i][i] = 0;
int dis = b/k - a/k, aa = a/k;
for (int i = 0; i <= 16; i++) {
if (dis&(1<<i)) {
vii curr(5, vi(5, inf));
combine(curr, ans, dp[aa][i]);
ans = curr;
aa += (1<<i);
}
}
cout << (ans[a%k][b%k] >= inf ? -1 : ans[a%k][b%k]) << endl;
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
return 0;
}
Compilation message (stderr)
toll.cpp: In function 'void sol()':
toll.cpp:59:53: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
59 | combine(dp[i][j], dp[i][j-1], dp[i+(1<<j-1)][j-1]);
| ~^~
# | 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... |