#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
25080 KB |
Output is correct |
2 |
Correct |
20 ms |
25188 KB |
Output is correct |
3 |
Correct |
21 ms |
25240 KB |
Output is correct |
4 |
Correct |
19 ms |
25240 KB |
Output is correct |
5 |
Correct |
19 ms |
25248 KB |
Output is correct |
6 |
Correct |
21 ms |
25468 KB |
Output is correct |
7 |
Correct |
19 ms |
25468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
25468 KB |
Output is correct |
2 |
Correct |
19 ms |
25468 KB |
Output is correct |
3 |
Correct |
19 ms |
25468 KB |
Output is correct |
4 |
Correct |
20 ms |
25468 KB |
Output is correct |
5 |
Correct |
21 ms |
25468 KB |
Output is correct |
6 |
Correct |
19 ms |
25468 KB |
Output is correct |
7 |
Correct |
23 ms |
25468 KB |
Output is correct |
8 |
Correct |
19 ms |
25468 KB |
Output is correct |
9 |
Incorrect |
19 ms |
25468 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
25468 KB |
Output is correct |
2 |
Correct |
20 ms |
25468 KB |
Output is correct |
3 |
Correct |
19 ms |
25468 KB |
Output is correct |
4 |
Correct |
20 ms |
25468 KB |
Output is correct |
5 |
Correct |
22 ms |
25468 KB |
Output is correct |
6 |
Correct |
23 ms |
25468 KB |
Output is correct |
7 |
Correct |
21 ms |
25468 KB |
Output is correct |
8 |
Correct |
19 ms |
25468 KB |
Output is correct |
9 |
Incorrect |
19 ms |
25468 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
25468 KB |
Output is correct |
2 |
Correct |
19 ms |
25468 KB |
Output is correct |
3 |
Correct |
19 ms |
25468 KB |
Output is correct |
4 |
Correct |
20 ms |
25468 KB |
Output is correct |
5 |
Correct |
23 ms |
25468 KB |
Output is correct |
6 |
Correct |
21 ms |
25468 KB |
Output is correct |
7 |
Correct |
21 ms |
25468 KB |
Output is correct |
8 |
Correct |
20 ms |
25468 KB |
Output is correct |
9 |
Incorrect |
19 ms |
25468 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
25468 KB |
Output is correct |
2 |
Correct |
19 ms |
25468 KB |
Output is correct |
3 |
Correct |
19 ms |
25468 KB |
Output is correct |
4 |
Correct |
19 ms |
25468 KB |
Output is correct |
5 |
Correct |
20 ms |
25468 KB |
Output is correct |
6 |
Correct |
21 ms |
25468 KB |
Output is correct |
7 |
Correct |
56 ms |
25468 KB |
Output is correct |
8 |
Correct |
19 ms |
25468 KB |
Output is correct |
9 |
Incorrect |
23 ms |
25468 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |