#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];
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<pair<int, pii> >q;
q.push(mp(0, mp(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.F;
int x = z.S.F;
int w = z.S.S;
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(mp(-l, mp(x, 0)));
}
if (x + w < n && d[x + w][w] > l + 1) {
d[x + w][w] = l+1; q.push(mp(-l-1, mp(x+w, w)));
}
if (x - w >= 0 && d[x - w][w] > l + 1) {
d[x - w][w] = l+1; q.push(mp(-l-1, mp(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(mp(-d[y][0], mp(y, 0)));
}
}
else
{
if (d[x][ww] > d[x][0])
{
d[x][ww] = d[x][0];
q.push(mp(-d[x][ww], mp(x, ww)));
}
}
}
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
25080 KB |
Output is correct |
2 |
Correct |
20 ms |
25192 KB |
Output is correct |
3 |
Correct |
22 ms |
25432 KB |
Output is correct |
4 |
Incorrect |
19 ms |
25432 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
25432 KB |
Output is correct |
2 |
Correct |
20 ms |
25432 KB |
Output is correct |
3 |
Correct |
21 ms |
25472 KB |
Output is correct |
4 |
Incorrect |
19 ms |
25472 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
25472 KB |
Output is correct |
2 |
Correct |
20 ms |
25472 KB |
Output is correct |
3 |
Correct |
20 ms |
25476 KB |
Output is correct |
4 |
Incorrect |
20 ms |
25480 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
25564 KB |
Output is correct |
2 |
Correct |
20 ms |
25564 KB |
Output is correct |
3 |
Correct |
20 ms |
25564 KB |
Output is correct |
4 |
Incorrect |
20 ms |
25624 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
25624 KB |
Output is correct |
2 |
Correct |
20 ms |
25624 KB |
Output is correct |
3 |
Correct |
20 ms |
25624 KB |
Output is correct |
4 |
Incorrect |
20 ms |
25624 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |