Submission #1039224

#TimeUsernameProblemLanguageResultExecution timeMemory
1039224n3rm1nToll (BOI17_toll)C++17
0 / 100
27 ms9436 KiB
#include<bits/stdc++.h> #define endl '\n' using namespace std; const long long MAXN = 5e4 + 10, MAXM = 1e4 + 10; void speed() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } long long k, n, m, q; long long is[MAXN]; long long pref[MAXN], cnt[MAXN]; vector < pair < long long, long long > > g[MAXN]; void read() { cin >> k >> n >> m >> q; for (long long i = 1; i <= m; ++ i) { long long x, y, c; cin >> x >> y >> c; is[x] = c; g[x].push_back(make_pair(y, c)); } for (int i = 0; i < n; ++ i) { if(i)pref[i] = pref[i-1]; pref[i] += is[i]; if(i)cnt[i] = cnt[i-1]; if(is[i])cnt[i] ++; } } long long isstart[MAXN]; vector < pair < long long, long long > > que[MAXN]; long long st, dist[MAXN], inf = 1e17; /// fix long long visited[MAXN]; void dijkstra() { for (long long i = 0; i < n; ++ i) { dist[i] = inf; visited[i] = 0; } dist[st] = 0; priority_queue < pair < long long, long long > > q; q.push({0, st}); long long wver, wdist; while(q.size()) { wdist = -q.top().first; wver = q.top().second; q.pop(); if(visited[wver])continue; visited[wver] = 1; for (auto nb: g[wver]) { if(dist[nb.first] > dist[wver] + nb.second) { dist[nb.first] = dist[wver] + nb.second; q.push(make_pair(-dist[nb.first], nb.first)); } } } } long long ans[MAXN]; void precompute() { for (long long i = 1; i <= q; ++ i) { long long from, to; cin >> from >> to; if(k == 1) { int diff = to - from; if(from > to) { cout << -1 << endl; continue; } int cnttt = cnt[to-1] - cnt[from]; int summ = pref[to-1]; if(from)summ-= pref[from-1]; if(cnttt == diff)cout << summ << endl; else cout << -1 << endl; } que[from].push_back({to, i}); isstart[from] = 1; } if(k == 1)exit(0); memset(ans, -1, sizeof(ans)); for (long long i = 0; i < n; ++ i) { if(!isstart[i])continue; // cout << i << endl; st = i; dijkstra(); for (auto to: que[i]) { // cout << "to " << to.first << " " << dist[to.first] << endl; if(dist[to.first] != inf)ans[to.second] = dist[to.first]; } } for (long long i = 1; i <= q; ++ i) cout << ans[i] << endl; } int main() { speed(); read(); precompute(); return 0; }

Compilation message (stderr)

toll.cpp: In function 'void dijkstra()':
toll.cpp:50:21: warning: variable 'wdist' set but not used [-Wunused-but-set-variable]
   50 |     long long wver, wdist;
      |                     ^~~~~
#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...