# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
146014 | mhy908 | Jakarta Skyscrapers (APIO15_skyscraper) | C++14 | 115 ms | 4044 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>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define inf 987654321
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
int n, m;
int b[30010], p[30010];
int dijk[30010];
priority_queue<pii> pq;
vector<int> jump[30010];
void dijkstra(int st)
{
pq.push({0, st});
dijk[st]=0;
while(!pq.empty()){
pii y=pq.top();
int here=y.S, th=-y.F;
pq.pop();
if(dijk[here]<th)continue;
for(int i=0; i<jump[here].size(); i++){
for(int j=here-jump[here][i]; j>=0; j-=jump[here][i]){
if(dijk[j]>dijk[here]+(here-j)/jump[here][i]){
dijk[j]=dijk[here]+(here-j)/jump[here][i];
pq.push({-dijk[j], j});
}
vector<int>::iterator it=lower_bound(jump[j].begin(), jump[j].end(), jump[here][i]);
if(it!=jump[j].end()){
if(*it==jump[here][i])break;
}
}
for(int j=here+jump[here][i]; j<n; j+=jump[here][i]){
if(dijk[j]>dijk[here]+(j-here)/jump[here][i]){
dijk[j]=dijk[here]+(j-here)/jump[here][i];
pq.push({-dijk[j], j});
}
vector<int>::iterator it=lower_bound(jump[j].begin(), jump[j].end(), jump[here][i]);
if(it!=jump[j].end()){
if(*it==jump[here][i])break;
}
}
}
}
}
int main()
{
scanf("%d %d", &n, &m);
for(int i=1; i<=m; i++){
scanf("%d %d", &b[i], &p[i]);
jump[b[i]].pb(p[i]);
}
for(int i=0; i<n; i++){
sort(jump[i].begin(), jump[i].end());
jump[i].erase(unique(jump[i].begin(), jump[i].end()), jump[i].end());
}
fill(dijk, dijk+30005, inf);
dijkstra(b[1]);
printf("%d", dijk[b[2]]==inf?-1:dijk[b[2]]);
}
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... |