#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, kInf);
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;
}
}
for (int x = 0; x < g.size(); x++) {
for (const auto &[y, w] : g[x]) {
// cout << x << ' ' << y << ' ' << w << '\n';
}
}
return dijkstra(g, S).first[G];
}
Compilation message
walk.cpp: In function 'int64 min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:109:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for (int x = 0; x < g.size(); x++) {
| ~~^~~~~~~~~~
walk.cpp:110:22: warning: unused structured binding declaration [-Wunused-variable]
110 | for (const auto &[y, w] : g[x]) {
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
264 ms |
73148 KB |
Output is correct |
4 |
Incorrect |
269 ms |
84384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
13984 KB |
Output is correct |
2 |
Incorrect |
1393 ms |
486652 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
13984 KB |
Output is correct |
2 |
Incorrect |
1393 ms |
486652 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |