Submission #657331

#TimeUsernameProblemLanguageResultExecution timeMemory
657331evenvalueSky Walking (IOI19_walk)C++17
10 / 100
4062 ms1048576 KiB
#include "walk.h" #include <bits/stdc++.h> using namespace std; template<typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>; template<typename T> using max_heap = priority_queue<T, vector<T>, less<T>>; using int64 = long long; using ld = long double; constexpr int kInf = 1e9 + 10; constexpr int64 kInf64 = 1e15 + 10; constexpr int kMod = 1e9 + 7; pair<vector<int64>, vector<int>> dijkstra(const vector<vector<pair<int, int>>> &g, const int s) { const int n = (int) g.size(); vector<int64> d(n, kInf64); vector<int> p(n, -1); min_heap<pair<int64, int>> q; d[s] = 0; q.push({0, s}); while (not q.empty()) { const auto [dist, x] = q.top(); q.pop(); if (d[x] < dist) continue; for (const auto &[y, w] : g[x]) { if (d[y] <= d[x] + w) continue; d[y] = d[x] + w; p[y] = x; q.push({d[y], y}); } } return make_pair(d, p); } struct building { int x; int h; }; struct skywalk { int i; int l; int r; int h; bool operator<(const skywalk &other) const { return h < other.h; } }; int64 min_distance(vector<int> X, vector<int> H, vector<int> L, vector<int> R, vector<int> Y, int S, int G) { const int n = (int) X.size(); const int m = (int) L.size(); vector<building> buildings(n); for (int i = 0; i < n; i++) { buildings[i].h = H[i]; buildings[i].x = X[i]; } vector<skywalk> skywalks(m); for (int j = 0; j < m; j++) { skywalks[j].i = j; skywalks[j].l = L[j]; skywalks[j].r = R[j]; skywalks[j].h = Y[j]; } sort(skywalks.begin(), skywalks.end()); vector<vector<pair<int, int>>> g; vector<pair<int, int>> last(n); auto add_node = [&]() -> int { g.emplace_back(); return (int) g.size() - 1; }; auto add_edge = [&](const int x, const int y, const int w) { g[x].emplace_back(y, w); g[y].emplace_back(x, w); }; for (int i = 0; i < n; i++) { last[i] = make_pair(add_node(), 0); } for (skywalk s : skywalks) { int prev = s.l; for (int i = s.l, x = -1; i <= s.r; i++) { if (s.h > buildings[i].h) continue; const int y = add_node(); if (x != -1) add_edge(x, y, buildings[i].x - buildings[prev].x); add_edge(y, last[i].first, s.h - last[i].second); last[i] = make_pair(y, s.h); x = y; prev = i; } } const int64 ans = dijkstra(g, S).first[G]; return (ans == kInf64 ? -1 : ans); }
#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...