# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1097441 | Kerim | Jakarta Skyscrapers (APIO15_skyscraper) | C++17 | 0 ms | 348 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;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 2e3+5;
const ll INF = 1e18;
bool adj[N][N];
ll dis[N];
int main(){
int n, m;
scanf("%d%d", &n, &m);
int s, t;
for(int i = 1; i <= m; ++i){
int x, p;
scanf("%d%d", &x, &p), x++;
if (i == 1)
s = x;
if (i == 2)
t = x;
adj[x][p] = 1;
for(int ad = 1; x-ad*p >= 1; ++ad)
adj[x-ad*p][p] = 1;
for(int ad = 1; x+ad*p <= n; ++ad)
adj[x+ad*p][p] = 1;
}
for(int i = 1; i <= n; ++i)
dis[i] = INF;
priority_queue<pli, vector<pli>, greater<pli>> pq;
pq.push({0, s});
dis[s] = 0;
while(!pq.empty()){
pli P = pq.top();
pq.pop();
ll val = P.ff;
int x = P.ss;
if(dis[x] != val)
continue;
for (int p = 1; p <= n; p++)
if (adj[x][p]){
if (x - p >= 1 and umin(dis[x-p], dis[x]+1))
pq.push({dis[x-p], x-p});
if (x + p >= 1 and umin(dis[x+p], dis[x]+1))
pq.push({dis[x+p], x+p});
}
}
if(dis[t] == INF)
puts("-1");
else
printf("%lld\n", dis[t]);
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... |