# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
27347 | TAMREF | Jakarta Skyscrapers (APIO15_skyscraper) | C++11 | 1000 ms | 4524 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 va first
#define vb second
#define mp make_pair
#define all(x) x.begin(),x.end()
using namespace std;
typedef pair<int,int> pii;
const int mx=30005;
int dist[mx];
pii doge[mx];
vector<int> C[mx];
int N,M;
void input(){
scanf("%d%d",&N,&M);
for(int i=0;i<M;i++){
scanf("%d%d",&doge[i].va,&doge[i].vb);
C[doge[i].va].push_back(doge[i].vb);
}
for(int i=0;i<N;i++)
sort(all(C[i]),greater<int>());
fill(dist,dist+N,(int)1e9);
}
void dijk(int s){
priority_queue<pii,vector<pii>,greater<pii>> q;
q.push(mp(dist[s]=0,s));
pii t;
int vis[mx];
while(!q.empty()){
t=q.top(); q.pop();
if(t.va>dist[t.vb]) continue;
for(int i=0;i<N;i++) vis[i]=0;
for(int p : C[t.vb]){
for(int x=t.vb+p;x<N;x+=p){
if(!C[x].size()||vis[x]) continue;
vis[x]=1;
if(binary_search(all(C[x]),p)){
if(dist[x]>t.va+(x-t.vb)/p){
q.push(mp(dist[x]=t.va+(x-t.vb)/p,x));
break;
}
}
if(dist[x]>=t.va+(x-t.vb)/p)
q.push(mp(dist[x]=t.va+(x-t.vb)/p,x));
}
for(int x=t.vb-p;x>=0;x-=p){
if(!C[x].size()||vis[x]) continue;
vis[x]=1;
if(binary_search(all(C[x]),p)){
if(dist[x]>=t.va+(t.vb-x)/p){
q.push(mp(dist[x]=t.va+(t.vb-x)/p,x));
break;
}
}
if(dist[x]>=t.va+(t.vb-x)/p)
q.push(mp(dist[x]=t.va+(t.vb-x)/p,x));
}
}
}
}
int main(){
input();
dijk(doge[0].va);
printf("%d\n",dist[doge[1].va]==(int)1e9?-1:dist[doge[1].va]);
}
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... |