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 <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#define fi first
#define se second
using namespace std;
const int N = 30005, D = 180, MX = 5E5 + 5, INF = 1E9 + 7;
int n, m, d, b, p, st, en, dis[N][D];
vector<int> dif[N];
vector<pair<int, int>> ve[MX];
void mini(pair<int, int> u, int v)
{
if (dis[u.fi][u.se] > v)
{
dis[u.fi][u.se] = v;
ve[v].push_back(u);
}
}
void dijkstra()
{
for (int i = 0; i < n; i++)
for (int j = 0; j < d; j++)
dis[i][j] = INF;
dis[st][0] = 0; ve[0].push_back({st, 0});
for (int i = 0; i < MX; i++)
while (!ve[i].empty())
{
pair<int, int> u = ve[i].back(); ve[i].pop_back();
if (u.se == 0)
{
for (int &v : dif[u.fi])
{
if (v < d)
mini({u.fi, v}, i);
else
for (int j = u.fi % v; j < n; j += v)
mini({j, 0}, i + abs(u.fi - j) / v);
}
}
else
{
mini({u.fi, 0}, i);
if (u.fi + u.se < n)
mini({u.fi + u.se, u.se}, i + 1);
if (u.fi - u.se >= 0)
mini({u.fi - u.se, u.se}, i + 1);
}
}
printf("%d\n", dis[en][0] == INF ? -1 : dis[en][0]);
}
int main()
{
scanf("%d%d", &n, &m);
d = sqrt(n - 1) + 1;
for (int i = 0; i < m; i++)
{
scanf("%d%d", &b, &p);
if (i == 0)
st = b;
else if (i == 1)
en = b;
dif[b].push_back(p);
}
for (int i = 0; i < n; i++)
{
sort(dif[i].begin(), dif[i].end());
dif[i].erase(unique(dif[i].begin(), dif[i].end()), dif[i].end());
}
dijkstra();
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:60:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
skyscraper.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &b, &p);
~~~~~^~~~~~~~~~~~~~~~
# | 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... |