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 oo 1000000000
#define maxm 4000000
#define maxn 50005
#define block 250
using namespace std;
typedef long long ll;
int n, m;
int b[maxn], p[maxn];
vector < pair < int, int > > g[maxm];
int node(int u, int x) {return x * n + u;}
int dist[maxm], vis[maxn];
vector < int > L[maxn];
int dijkstra() {
fill(dist, dist + maxn, oo);
dist[b[0]] = 0;
L[0].push_back(b[0]);
int M = 1000000;
for (int i = 0; i <= M; ++i)
for (int id = 0; id < L[i].size(); ++id) {
int u = L[i][id];
if (u == b[1])
return i;
if (dist[u] < i) continue;
for (auto x : g[u]) {
int v = x.first, w = x.second;
if (dist[v] > dist[u] + w && dist[u] + w <= M) {
dist[v] = dist[u] + w;
L[dist[v]].push_back(v);
}
}
}
return -1;
}
int main(){
#define TASK "ABC"
//freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout);
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < m; ++i)
cin >> b[i] >> p[i];
for (int j = 1; j <= block; ++j)
for (int i = j; i < n; ++i)
g[node(i, j)].push_back({node(i - j, j), 1});
for (int j = 1; j <= block; ++j)
for (int i = 0; i + j < n; ++i)
g[node(i, j)].push_back({node(i + j, j), 1});
for (int i = 0; i < n; ++i)
for (int j = 1; j <= block; ++j)
g[node(i, j)].push_back({i, 0});
for (int i = 0; i < m; ++i)
if (p[i] <= block)
g[b[i]].push_back({node(b[i], p[i]), 0});
for (int i = 0; i < m; ++i)
if (p[i] > block)
for (int j = b[i] % p[i]; j < n; j += p[i])
if (i != j)
g[b[i]].push_back({j, abs(j - b[i]) / p[i]});
//for (int i = 0; i < 10; ++i)
// for (auto j : g[i])
// cerr << i << ' ' << j.first << ' ' << j.second << '\n';
cout << dijkstra();
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int dijkstra()':
skyscraper.cpp:30:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int id = 0; id < L[i].size(); ++id) {
~~~^~~~~~~~~~~~~
# | 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... |