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 <iostream>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <stack>
#include <queue>
#include <assert.h>
#include <limits>
#include <cstdio>
using namespace std;
//#define RDEBUG 1
#ifdef RDEBUG
#define D(x) x
#else
#define D(x)
#endif
#define inf 0x7fffffff
#define MOD 1000000007
int add(int a, int b) {
a += b;
if(a >= MOD) {
a -= MOD;
}
return a;
}
int sub(int a, int b) {
a -= b;
if(a < 0) {
a += MOD;
}
return a;
}
int mul(int a, int b) {
return (a * b)%MOD;
}
void add_self(int& a, int b) {
a = add(a, b);
}
void sub_self(int& a, int b) {
a = sub(a, b);
}
void mul_self(int& a, int b) {
a = mul(a, b);
}
const int MAXN = 30010;
int sp[MAXN][175];
int N, M;
pair<int, int> doge[MAXN];
vector<pair<pair<int, int>, int>> adj[MAXN][175];
void dij(int start) {
for (int i = 0; i<N; i++) {
for (int j = 0; j<=sqrt(N); j++) {
sp[i][j] = 1e13;
}
}
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
pq.push({0, {doge[start].first, 0}});
sp[doge[start].first][0] = 0;
while (pq.size() != 0) {
int d = pq.top().first, u = pq.top().second.first, p = pq.top().second.second;
pq.pop();
if (sp[u][p] != d) {
continue;
}
if (p != 0) {
if (u-p >= 0) {
adj[u][p].push_back({{u-p, p}, 1});
}
if (u+p < N) {
adj[u][p].push_back({{u+p, p}, 1});
}
adj[u][p].push_back({{u, 0}, 0});
}
for (auto edge : adj[u][p]) {
int v = edge.first.first, np = edge.first.second, w = edge.second;
if (d+w < sp[v][np]) {
sp[v][np] = d+w;
pq.push({d+w, {v, np}});
}
}
if (p != 0) {
if (u-p >= 0) {
adj[u][p].pop_back();
}
if (u+p < N) {
adj[u][p].pop_back();
}
adj[u][p].push_back({{u, 0}, 0});
}
}
}
int main() {
ios_base :: sync_with_stdio(false);
cin >> N >> M;
for (int i = 0; i<M; i++) {
int a, b;
cin >> a >> b;
doge[i] = {a, b};
if (b<=sqrt(N)) {
adj[a][0].push_back({{a, b}, 0});
} else {
for (int j = 1; j<=N; j++) {
int hold = 0;
if (a-b*j >= 0) {
hold++;
adj[a][0].push_back({{a-b*j, 0}, j});
}
if (a+b*j < N) {
hold++;
adj[a][0].push_back({{a+b*j, 0}, j});
}
if (!hold) {
break;
}
}
}
}
dij(0);
cout << (sp[doge[1].first][0] == 1e13 ? -1 : sp[doge[1].first][0]) << "\n";
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'void dij(int)':
skyscraper.cpp:70:24: warning: overflow in implicit constant conversion [-Woverflow]
sp[i][j] = 1e13;
^~~~
# | 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... |