# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
678715 | qwerasdfzxcl | Jakarta Skyscrapers (APIO15_skyscraper) | C++17 | 1099 ms | 87484 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>
typedef long long ll;
using namespace std;
constexpr int INF = 1e9+100;
struct my_bitset{
unsigned long long bit[500];
bool find(int x){return bit[x/64]&(1LL<<(x&63));}
void insert(int x){bit[x/64] |= (1LL<<(x&63));}
};
int dist[30030], visited[30030];
set<int> st[30030];
vector<int> a[30030];
struct Vertex{
int s, p;
Vertex(){}
Vertex(int _s, int _p): s(_s), p(_p) {}
//bool operator < (const Vertex &V) const{return d > V.d;}
};
int main(){
int n, m, s, e;
scanf("%d %d", &n, &m);
for (int i=1;i<=m;i++){
int x, p;
scanf("%d %d", &x, &p);
++x;
a[x].push_back(p);
if (i==1) s = x;
if (i==2) e = x;
}
fill(dist+1, dist+n+1, INF);
fill(visited+1, visited+n+1, 0);
dist[s] = 0;
for (int i=1;i<=n;i++){
sort(a[i].begin(), a[i].end());
a[i].erase(unique(a[i].begin(), a[i].end()), a[i].end());
}
stack<Vertex, vector<Vertex>> pq[2];
pq[0].emplace(s, 0);
int d = 0;
while(true){
if (pq[d&1].empty() && pq[(d+1)&1].empty()) break;
if (pq[d&1].empty()) {d++; continue;}
auto [s, p] = pq[d&1].top(); pq[d&1].pop();
dist[s] = min(dist[s], d);
if (p){
if (st[s].find(p)!=st[s].end()) continue;
st[s].insert(p);
if (s-p > 0) pq[(d+1)&1].emplace(s-p, p);
if (s+p <= n) pq[(d+1)&1].emplace(s+p, p);
}
if (!visited[s]){
visited[s] = 1;
for (auto &p2:a[s]){
st[s].insert(p2);
if (s-p2 > 0) pq[(d+1)&1].emplace(s-p2, p2);
if (s+p2 <= n) pq[(d+1)&1].emplace(s+p2, p2);
}
}
}
if (dist[e]==INF) printf("-1\n");
else printf("%d\n", dist[e]);
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... |