# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1264433 | mingga | Toll (BOI17_toll) | C++20 | 83 ms | 43336 KiB |
// Author: caption_mingle
#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 50005;
int k, n, m, q;
ll f[N][20][5];
vector<pair<int, int>> g[N];
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#define task ""
if(fopen(task ".INP", "r")) {
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
cin >> k >> n >> m >> q;
for(int i = 1; i <= m; i++) {
int a, b, t; cin >> a >> b >> t;
g[a].pb({b, t});
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < 20; j++) {
for(int t = 0; t < k; t++) {
f[i][j][t] = inf;
}
}
}
for(int i = 0; i < n; i++) {
for(auto [v, w] : g[i]) {
f[i][0][v % k] = w;
}
}
for(int j = 1; (1 << j) <= n / k; j++) {
for(int i = 0; i < n; i++) {
for(int x1 = 0; x1 < k; x1++) {
int bl = (i / k) + (1 << (j - 1));
for(int x2 = 0; x2 < k; x2++) {
if(bl * k + x2 >= n) continue;
f[i][j][x1] = min(f[i][j][x1], f[i][j - 1][x2] + f[bl * k + x2][j - 1][x1]);
}
}
}
}
for(int i = 1; i <= q; i++) {
int a, b; cin >> a >> b;
int range = (b / k) - (a / k);
vector<vector<ll>> mn(2, vector<ll>(k, inf));
mn[0][a % k] = 0;
int bl = a / k;
for(int j = 0; (1 << j) <= range; j++) {
if(range >> j & 1) {
for(int x1 = 0; x1 < k; x1++) {
for(int x2 = 0; x2 < k; x2++) {
if(bl * k + x2 >= n) continue;
mn[1][x1] = min(mn[1][x1], mn[0][x2] + f[bl * k + x2][j][x1]);
}
}
bl += (1 << j);
mn[0] = mn[1];
mn[1] = vector<ll>(k, inf);
}
}
if(mn[0][b % k] >= (int)inf) cout << -1;
else cout << mn[0][b % k];
cout << ln;
}
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
Compilation message (stderr)
# | 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... |