Submission #857339

#TimeUsernameProblemLanguageResultExecution timeMemory
857339NK_Toll (BOI17_toll)C++17
100 / 100
173 ms93764 KiB
// Success consists of going from failure to failure without loss of enthusiasm #include <bits/stdc++.h> using namespace std; #define nl '\n' #define pb push_back #define pf push_front #define mp make_pair #define f first #define s second #define sz(x) int(x.size()) template<class T> using V = vector<T>; using pi = pair<int, int>; using vi = V<int>; using vpi = V<pi>; using ll = long long; using pl = pair<ll, ll>; using vpl = V<pl>; using vl = V<ll>; using db = long double; template<class T> using pq = priority_queue<T, V<T>, greater<T>>; const int MOD = 1e9 + 7; const ll INFL = ll(1e17); const int LG = 18; using T = array<array<int, 5>, 5>; const T FUL = { array<int, 5>{MOD, MOD, MOD, MOD, MOD}, array<int, 5>{MOD, MOD, MOD, MOD, MOD}, array<int, 5>{MOD, MOD, MOD, MOD, MOD}, array<int, 5>{MOD, MOD, MOD, MOD, MOD}, array<int, 5>{MOD, MOD, MOD, MOD, MOD} }; int main() { cin.tie(0)->sync_with_stdio(0); int N, M, Q, K; cin >> K >> N >> M >> Q; int LVL = (N + K - 1) / K; V<vpi> nxt(N); for(int i = 0; i < M; i++) { int a, b, t; cin >> a >> b >> t; nxt[a].pb(mp(b, t)); } V<V<T>> up(LVL, V<T>(LG, FUL)); for(int i = 0; i < N; i++) { int blk = i / K, x = i % K; for(auto e : nxt[i]) { int v, w; tie(v, w) = e; up[blk][0][x][v % K] = min(up[blk][0][x][v % K], w); } } for(int blk = LVL - 1; blk >= 0; blk--) { for(int d = 1; d < LG; d++) if ((blk + (1 << (d - 1))) < LVL) { for(int a = 0; a < 5; a++) for(int b = 0; b < 5; b++) for(int c = 0; c < 5; c++) { up[blk][d][a][c] = min(up[blk][d][a][c], up[blk][d-1][a][b] + up[blk + (1 << (d - 1))][d-1][b][c]); } } } for(int i = 0; i < Q; i++) { int u, v; cin >> u >> v; // cout << u << " - " << v << endl; int bu = u / K, bv = v / K; u %= K, v %= K; array<int, 5> ans = {MOD, MOD, MOD, MOD, MOD}; ans[u] = 0; // cout << bu << " = " << bv << endl; for(int b = bu, d = LG - 1; d >= 0; d--) { if ((b + (1 << d)) <= bv) { // cout << b << " " << d << endl; array<int, 5> nans = {MOD, MOD, MOD, MOD, MOD}; for(int x = 0; x < 5; x++) for(int y = 0; y < 5; y++) { nans[y] = min(ans[x] + up[b][d][x][y], nans[y]); } b += (1 << d); ans.swap(nans); } } cout << (ans[v] == MOD ? -1 : ans[v]) << nl; // cout << endl; } exit(0-0); }
#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...