# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
298644 | errorgorn | Jakarta Skyscrapers (APIO15_skyscraper) | C++14 | 102 ms | 7036 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 <cstdio>
#include <queue>
#include <utility>
#include <cstring>
using namespace std;
typedef pair<int,int> ii;
const int SQRTN=300;
const int MAXN=30005;
int n,k;
int w[MAXN];
bool pow[SQRTN][MAXN];
vector<int> power[MAXN];
priority_queue<ii,vector<ii>,greater<ii> > pq;
int main(){
scanf("%d%d",&n,&k);
int a,b;
int s,t;
for (int x=0;x<k;x++){
scanf("%d%d",&a,&b);
if (x==0) s=a;
else if (x==1) t=a;
if (b<SQRTN){
if (pow[b][a]) continue;
pow[b][a]=true;
power[a].push_back(b);
}
else{
power[a].push_back(b);
}
}
memset(w,127,sizeof(w));
w[s]=0;
pq.push(ii(w[s],s));
int node,weight;
while (!pq.empty()){
node=pq.top().second,weight=pq.top().first,pq.pop();
//printf("%d %d\n",node,weight);
if (w[node]!=weight) continue;
if (node==t){
printf("%d\n",weight);
return 0;
}
for (auto &p:power[node]){
for (int x=node-p;x>=0;x-=p){
if (w[x]>weight+(node-x)/p){
w[x]=weight+(node-x)/p;
pq.push(ii(w[x],x));
}
if (p<SQRTN && pow[p][x]) break;
}
for (int x=node+p;x<MAXN;x+=p){
if (w[x]>weight+(x-node)/p){
w[x]=weight+(x-node)/p;
pq.push(ii(w[x],x));
}
if (p<SQRTN && pow[p][x]) break;
}
}
}
printf("-1\n");
}
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... |