Submission #1061980

#TimeUsernameProblemLanguageResultExecution timeMemory
1061980VMaksimoski008Toll (BOI17_toll)C++17
7 / 100
40 ms6916 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const int maxn = 5e4 + 5; int K, N, M, Q, B[maxn]; vector<ll> ans, dist(maxn); vector<pii> g1[maxn], g2[maxn]; void D(int s, int l, int r) { for(int i=0; i<N; i++) dist[i] = 1e18; dist[s] = 0; priority_queue<pll, vector<pll>, greater<pll> > pq; pq.push({ 0, s }); while(!pq.empty()) { auto [d, u] = pq.top(); pq.pop(); if(d != dist[u]) continue; for(auto &[v, w] : g1[u]) { if(B[v] < l || B[v] > r) continue; if(dist[v] > dist[u] + w) { dist[v] = d + w; pq.push({ d + w, v }); } } } pq.push({ 0, s }); while(!pq.empty()) { auto [d, u] = pq.top(); pq.pop(); if(d != dist[u]) continue; for(auto &[v, w] : g2[u]) { if(B[v] < l || B[v] > r) continue; if(dist[v] > dist[u] + w) { dist[v] = d + w; pq.push({ d + w, v }); } } } } void f(int l, int r, vector<array<int, 3> > &qus) { if(l > r || qus.empty()) return ; int tm = (l + r) / 2; vector<array<int, 3> > L, R; for(int i=tm*K; i<min(N, tm * K + K); i++) { D(i, l, r); for(auto &[a, b, id] : qus) ans[id] = min(ans[id], dist[a] + dist[b]); } for(auto i=0; i<qus.size(); i++) { if(B[qus[i][1]] < tm) L.push_back(qus[i]); if(B[qus[i][0]] > tm) R.push_back(qus[i]); } f(l, tm-1, L); f(tm+1, r, R); } signed main() { ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); cin >> K >> N >> M >> Q; for(int i=0; i<N; i++) B[i] = i / K; ans.resize(Q, 1e18); vector<array<int, 3> > qus; for(int i=0; i<M; i++) { int a, b, t; cin >> a >> b >> t; g1[a].push_back({ b, t }); g2[b].push_back({ a, t }); } for(int i=0; i<Q; i++) { int a, b; cin >> a >> b; if(B[a] == B[b]) ans[i] = -1; else qus.push_back({ a, b, i }); } f(0, B[N-1], qus); for(auto x : ans) cout << (x >= 1e18 ? -1 : x) << '\n'; return 0; }

Compilation message (stderr)

toll.cpp: In function 'void f(int, int, std::vector<std::array<int, 3> >&)':
toll.cpp:58:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |     for(auto i=0; i<qus.size(); i++) {
      |                   ~^~~~~~~~~~~
#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...