Submission #948336

#TimeUsernameProblemLanguageResultExecution timeMemory
948336steveonalexToll (BOI17_toll)C++14
100 / 100
91 ms16724 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ll mask){return __builtin_popcountll(mask);} int ctz(ll mask){return __builtin_ctzll(mask);} int logOf(ll mask){return 63 - __builtin_clzll(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T container, char separator = ' ', char finish = '\n'){ for(auto item: container) cout << item << separator; cout << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } const long N = 5e4 + 69, INF = 1e9 + 69; long k, n, m, o; vector<pair<long, long>> graph[N], rgraph[N]; long ans[N]; pair<long, long> queries[N]; long dp[N]; void dfs(long l, long r, vector<long> cur){ if (l >= r) return; long mid = (l + r) >> 1; vector<long> L, R, pending; for(long i: cur){ if (queries[i].second / k < mid) L.push_back(i); else if (queries[i].first / k > mid) R.push_back(i); else pending.push_back(i); } if (L.size()) dfs(l, mid - 1, L); if (R.size()) dfs(mid + 1, r, R); if (pending.empty()) return; for(long i = mid * k; i < (mid + 1) * k; ++i) { for(long j = mid * k; j < (mid + 1) * k; ++j) dp[j] = INF; dp[i] = 0; for(long i = mid * k - 1; i >= l * k; --i) { dp[i] = INF; for(auto j: graph[i]) minimize(dp[i], dp[j.first] + j.second); } for(long i = (mid+1) * k; i < (r+1) * k; ++i) { dp[i] = INF; for(auto j: rgraph[i]) minimize(dp[i], dp[j.first] + j.second); } for(long i: pending){ minimize(ans[i], dp[queries[i].first] + dp[queries[i].second]); } } } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> k >> n >> m >> o; while(m--){ long u, v, w; cin >> u >> v >> w; graph[u].push_back({v, w}); rgraph[v].push_back({u, w}); } vector<long> cur(o); for(long i = 0; i<o; ++i) { cin >> queries[i].first >> queries[i].second; cur[i] = i; ans[i] = INF; } dfs(0, (n + k - 1) / k, cur); for(long i = 0; i<o; ++i) if (ans[i] == INF) cout << -1 << "\n"; else cout << ans[i] << "\n"; return 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...