# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
139240 | abacaba | Jakarta Skyscrapers (APIO15_skyscraper) | C++14 | 1081 ms | 82920 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;
const int inf = 2e9;
const int N = 3e4 + 15;
int n, m, d[N];
vector <pair <int, int> > g[N];
pair <int, int> a[N];
unordered_set <int> mod_used[N];
priority_queue <pair <int, int> > q;
map <int, int> is[N];
map <int, bool> used[N];
int main() {
fill(d, d + N, inf);
scanf("%d%d", &n, &m);
for(int i = 0; i < m; ++i) {
scanf("%d%d", &a[i].first, &a[i].second);
++is[a[i].first][a[i].second];
}
for(int i = 0; i < m; ++i) {
int b = a[i].first, p = a[i].second;
int cnt = 0;
if(used[b][p])
continue;
used[b][p] = true;
while(b - p >= 0) {
b -= p;
g[a[i].first].push_back({++cnt, b});
if(is[b][p])
break;
}
b = a[i].first;
cnt = 0;
while(b + p < n) {
b += p;
g[a[i].first].push_back({++cnt, b});
if(is[b][p])
break;
}
}
d[a[0].first] = 0;
q.push({0, a[0].first});
while(!q.empty()) {
int v = q.top().second, dist = -q.top().first;
q.pop();
if(dist > d[v])
continue;
for(pair <int, int> to : g[v]) {
if(d[to.second] > d[v] + to.first) {
d[to.second] = d[v] + to.first;
q.push({-d[to.second], to.second});
}
}
}
if(d[a[1].first] == inf)
puts("-1");
else
printf("%d\n", d[a[1].first]);
return 0;
}
Compilation message (stderr)
# | 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... |