# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
846923 | manhlinh1501 | Jakarta Skyscrapers (APIO15_skyscraper) | C++17 | 0 ms | 0 KiB |
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 <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int N = 3e4 + 5;
const int oo = 1e9;
const int SQRT = 175;
#define eb emplace_back
struct node {
int w, u, s;
node() {
w = u = s = 0;
}
node(int w, int u, int s) : w(w), u(u), s(s) {}
friend bool operator > (node a, node b) {
return a.w > b.w;
}
};
struct data {
int to, w, cur;
data() {
to = w = cur = 0;
}
data(int to, int w, int cur) : to(to), w(w), cur(cur) {}
};
int n, m;
int dist[N];
pii a[N];
vector<data> adj[N];
priority_queue<node, vector<node>, greater<node>> Q;
void update(int u, int v, int w, int cur) {
if(dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
Q.emplace(dist[v], v, cur);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
for(int j = 1; j * v <= n or j * v <= u; j++)
adj[u].eb(j * v, j, v);
a[i] = {u, v};
}
for(int i = 0; i < n; i++)
dist[i] = oo;
dist[a[0].first] = 0;
Q.emplace(0, a[0].first, a[0].second);
while(!Q.empty()) {
node top = Q.top();
Q.pop();
int u = top.u;
if(top.w != dist[u])
continue;
for(auto [v, w, cur] : adj[u]) {
if(u + v <= n)
update(u, u + v, w, cur);
if(u - v >= 0)
update(u, u - v, w, cur);
}
}
cout << (dist[a[1].first] == oo ? -1 : dist[a[1].first]);
}
/*
5 3
0 2
1 1
4 1
*/