This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using ll = long long;
using ii = pair<int, int>;
struct Dijkstra {
vector<vector<ii> > L;
int n;
Dijkstra(int N) : n(N), L(N) {}
void add_edge(int u, int v, int w) {
L[u].push_back({v, w});
L[v].push_back({u, w});
}
ll dist(int s, int t) {
const ll INF = 1e18;
vector<ll> D(n, INF);
D[s] = 0;
priority_queue<pair<ll, int> > PQ;
PQ.push({0, s});
while(!PQ.empty()) {
int u = PQ.top().second;
int d = - PQ.top().first;
PQ.pop();
if(D[u] != d) continue;
for(auto [it, w] : L[u]) {
if(D[it] > D[u] + w) {
D[it] = D[u] + w;
PQ.push({-D[it], it});
}
}
}
if(D[t] == INF) D[t] = -1;
return D[t];
}
};
long long min_distance(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g) {
int n = x.size();
int tmr = 0;
vector<vector<pair<int, int> > > Noduri(n); /// perechi(id, h)
vector<tuple<int, int, int> > E;
for(int i = 0; i < n; ++i) {
Noduri[i].push_back({tmr++, 0});
}
for(int i = 0; i < l.size(); ++i) {
int ult = -1;
for(int j = l[i]; j <= r[i]; ++j) {
if(h[j] < y[i]) continue;
else {
Noduri[j].push_back({tmr++, y[i]});
if(ult != -1) {
E.emplace_back(tmr - 1, Noduri[ult].back().first, x[j] - x[ult]);
}
ult = j;
}
}
}
Dijkstra Sol(tmr);
for(auto [u, v, w] : E)
Sol.add_edge(u, v, w);
for(int i = 0; i < n; ++i) {
sort(Noduri[i].begin(), Noduri[i].end(), [&](auto a, auto b) {return a.second < b.second;});
for(int j = 0; j + 1 < Noduri[i].size(); ++j) {
Sol.add_edge(Noduri[i][j].first, Noduri[i][j + 1].first,
Noduri[i][j + 1].second - Noduri[i][j].second);
}
}
return Sol.dist(Noduri[s][0].first, Noduri[g][0].first);
}
Compilation message (stderr)
walk.cpp: In constructor 'Dijkstra::Dijkstra(int)':
walk.cpp:12:9: warning: 'Dijkstra::n' will be initialized after [-Wreorder]
12 | int n;
| ^
walk.cpp:11:25: warning: 'std::vector<std::vector<std::pair<int, int> > > Dijkstra::L' [-Wreorder]
11 | vector<vector<ii> > L;
| ^
walk.cpp:14:5: warning: when initialized here [-Wreorder]
14 | Dijkstra(int N) : n(N), L(N) {}
| ^~~~~~~~
walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:52:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i = 0; i < l.size(); ++i) {
| ~~^~~~~~~~~~
walk.cpp:73:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int j = 0; j + 1 < Noduri[i].size(); ++j) {
| ~~~~~~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |