Submission #328896

#TimeUsernameProblemLanguageResultExecution timeMemory
328896syyToll (BOI17_toll)C++17
100 / 100
145 ms173420 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define FOR(i, a, b) for(ll i = (ll)a; i <= (ll)b; i++) #define DEC(i, a, b) for(ll i = (ll)a; i >= (ll)b; i--) typedef pair<ll, ll> pi; typedef pair<pi, ll> pii; typedef pair<ll, pi> ipi; typedef pair<pi, pi> pipi; #define f first #define s second typedef vector<ll> vi; typedef vector<pi> vpi; typedef vector<pii> vpii; #define pb push_back #define pf push_front #define all(v) v.begin(), v.end() #define size(v) (ll) v.size() #define disc(v) sort(all(v)); v.resize(unique(all(v)) - v.begin()); #define INF (ll) 1e9 + 100 #define LLINF (ll) 1e11 #define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define sandybridge __attribute__((optimize("Ofast"), target("arch=sandybridge"))) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng) inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss ll k, n, m, q, a, b, c, dist[50005][17][5][5], p[50005][17]; int main() { fastio; cin >> k >> n >> m >> q; n += k - (n % k); FOR(i, 0, n/k-1) FOR(t, 0, 16) FOR(j, 0, k-1) FOR(l, 0, k-1) dist[i][t][j][l] = LLINF; memset(p, -1, sizeof p); FOR(i, 1, m) { cin >> a >> b >> c; dist[a/k][0][a%k][b%k] = c; } FOR(i, 0, n/k-2) p[i][0] = i+1; FOR(t, 1, 16) DEC(i, n/k-2, 0) if (p[i][t-1] != -1) p[i][t] = p[p[i][t-1]][t-1]; //~ FOR(i, 0, n/k-1) FOR(t, 0, 2) cout << i << " " << t << " " << p[i][t] << "\n"; DEC(i, n/k-1, 0) FOR(t, 1, 16) if (p[i][t] != -1) { FOR(x, 0, k-1) FOR(z, 0, k-1) FOR(y, 0, k-1) dist[i][t][x][z] = min(dist[i][t][x][z], dist[i][t-1][x][y] + dist[p[i][t-1]][t-1][y][z]); } //~ FOR(i, 0, n/k-1) FOR(t, 0, 1) FOR(j, 0, k-1) FOR(l, 0, k-1) printf("%lld %lld %lld %lld: %lld\n", i, t, j, l, dist[i][t][j][l]); while (q--) { cin >> a >> b; ll x = a/k; vi pos; FOR(i, 0, k-1) pos.pb((i == a%k ? 0 : LLINF)); DEC(t, 16, 0) if (p[x][t] != -1 and p[x][t] <= b/k) { //~ cout << "considering " << x << " to " << p[x][t] << "\n"; vi newpos(k, LLINF); FOR(i, 0, k-1) FOR(y, 0, k-1) newpos[y] = min(newpos[y], dist[x][t][i][y] + pos[i]); //~ cout << "best: " << best.f << " " << best.s << "\n"; pos = newpos; x = p[x][t]; } cout << (pos[b%k] == LLINF ? -1 : pos[b%k]) << "\n"; //~ for (auto [d, it]:pos) if (it == b%k) ans = min(ans, d); //~ cout << (ans == LLINF ? -1 : ans) << "\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...