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 pii pair<int, int>
#define mp make_pair
#define F first
#define S second
#define PB push_back
#define N 30005
#define MAGIC 205
#define maxc 1000000007
using namespace std;
int n, m, b[N], p[N];
vector<int> vec[N];
int d[N][MAGIC];
struct state
{
int l, x, w;
state(int _l=0, int _x=0, int _w=0) : l(_l), x(_x), w(_w) {}
bool operator < (const state &rhs) const
{
return l > rhs.l;
}
};
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("INP.TXT", "r", stdin);
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> b[i] >> p[i];
vec[b[i]].PB(p[i]);
}
priority_queue<state>q;
q.push(state(0, b[0], 0));
memset(d, 60, sizeof d);
d[b[0]][0] = 0;
while (!q.empty())
{
auto z = q.top(); q.pop();
int l = z.l;
int x = z.x;
int w = z.w;
if (d[x][w] < l) continue;
if (x == b[1])
{
cout <<l;
return 0;
}
if (w)
{
if (d[x][0] > l) {
d[x][0] = l; q.push(state(l, x, 0));
}
if (x + w < n && d[x + w][w] > l + 1) {
d[x + w][w] = l+1; q.push(state(l+1, x+w, w));
}
if (x - w >= 0 && d[x - w][w] > l + 1) {
d[x - w][w] = l+1; q.push(state(l+1, x-w, w));
}
}
else
{
for (auto ww : vec[x])
{
if (ww >= MAGIC)
{
for (int y = x % ww; y < n; y += ww)
if (d[y][0] > d[x][w] + abs(x - y)/ww)
{
d[y][0] = d[x][w] + abs(x - y)/ww;
q.push(state(d[y][0], y, 0));
}
}
else
{
if (d[x][ww] > l)
{
d[x][ww] = l;
q.push(state(d[x][ww], x, ww));
}
}
}
}
}
cout <<-1;
}
# | 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... |