#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
const int ar = 5e4 + 5;
const ll mod = 1e9 + 7;
int n, m;
int sqr;
int b[ar];
int p[ar];
int dp[ar][226];
vector<int> ad[ar];
struct seg
{
int dis, i, p;
};
bool operator>(seg a, seg b)
{
return a.dis > b.dis;
}
queue<seg> pq;
int ans = 1e9;
void dijkstra()
{
memset(dp, 0x3f, sizeof dp);
pq.push({0, b[0], 0});
dp[b[0]][0] = 0;
while(pq.size())
{
seg top = pq.front();
pq.pop();
int dis = top.dis;
int i = top.i;
int pk = top.p;
if (dp[i][pk] < dis) continue;
if (i == b[1]) ans = min(ans, dis);
if (pk == 0)
{
for (auto x : ad[i])
{
if (p[x] <= sqr)
{
if (dp[i][p[x]] > dis)
{
dp[i][p[x]] = dis;
pq.push({dis, i, p[x]});
}
}
else
{
int cnt = 0;
for (int j = i; j < n; j += p[x])
{
if (dp[j][0] > dis + cnt)
{
dp[j][0] = dis + cnt;
pq.push({dp[j][0], j, 0});
}
cnt++;
}
cnt = 0;
for (int j = i; j >= 0; j -= p[x])
{
if (dp[j][0] > dis + cnt)
{
dp[j][0] = dis + cnt;
pq.push({dp[j][0], j, 0});
}
cnt++;
}
}
}
}
else
{
if (i + pk < n && dp[i + pk][pk] > dis + 1)
{
dp[i + pk][pk] = dis + 1;
pq.push({dp[i + pk][pk], i + pk, pk});
}
if (i - pk >= 0 && dp[i - pk][pk] > dis + 1)
{
dp[i - pk][pk] = dis + 1;
pq.push({dp[i - pk][pk], i - pk, pk});
}
if (dp[i][0] > dis)
{
dp[i][0] = dis;
pq.push({dp[i][0], i, 0});
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> b[i] >> p[i];
ad[b[i]].push_back(i);
}
sqr = 225;
dijkstra();
if (ans == 1e9) ans = -1;
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
45648 KB |
Output is correct |
2 |
Correct |
6 ms |
45648 KB |
Output is correct |
3 |
Correct |
6 ms |
45816 KB |
Output is correct |
4 |
Correct |
6 ms |
45868 KB |
Output is correct |
5 |
Correct |
7 ms |
45648 KB |
Output is correct |
6 |
Correct |
6 ms |
45648 KB |
Output is correct |
7 |
Correct |
6 ms |
45648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
45648 KB |
Output is correct |
2 |
Correct |
6 ms |
45648 KB |
Output is correct |
3 |
Correct |
6 ms |
45648 KB |
Output is correct |
4 |
Correct |
6 ms |
45648 KB |
Output is correct |
5 |
Correct |
6 ms |
45648 KB |
Output is correct |
6 |
Correct |
6 ms |
45736 KB |
Output is correct |
7 |
Correct |
6 ms |
45648 KB |
Output is correct |
8 |
Correct |
6 ms |
45712 KB |
Output is correct |
9 |
Correct |
6 ms |
45648 KB |
Output is correct |
10 |
Correct |
6 ms |
45648 KB |
Output is correct |
11 |
Correct |
6 ms |
45904 KB |
Output is correct |
12 |
Correct |
6 ms |
45904 KB |
Output is correct |
13 |
Correct |
6 ms |
45904 KB |
Output is correct |
14 |
Correct |
6 ms |
45916 KB |
Output is correct |
15 |
Correct |
7 ms |
45904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
45648 KB |
Output is correct |
2 |
Correct |
6 ms |
45648 KB |
Output is correct |
3 |
Correct |
9 ms |
45832 KB |
Output is correct |
4 |
Correct |
6 ms |
45648 KB |
Output is correct |
5 |
Correct |
6 ms |
45648 KB |
Output is correct |
6 |
Correct |
6 ms |
45648 KB |
Output is correct |
7 |
Correct |
6 ms |
45648 KB |
Output is correct |
8 |
Correct |
7 ms |
45648 KB |
Output is correct |
9 |
Correct |
7 ms |
45648 KB |
Output is correct |
10 |
Correct |
6 ms |
45648 KB |
Output is correct |
11 |
Correct |
7 ms |
45904 KB |
Output is correct |
12 |
Correct |
7 ms |
45904 KB |
Output is correct |
13 |
Correct |
7 ms |
45904 KB |
Output is correct |
14 |
Correct |
8 ms |
46072 KB |
Output is correct |
15 |
Correct |
8 ms |
45916 KB |
Output is correct |
16 |
Correct |
7 ms |
45900 KB |
Output is correct |
17 |
Correct |
9 ms |
45904 KB |
Output is correct |
18 |
Correct |
9 ms |
45904 KB |
Output is correct |
19 |
Correct |
7 ms |
45648 KB |
Output is correct |
20 |
Correct |
7 ms |
45904 KB |
Output is correct |
21 |
Correct |
7 ms |
45648 KB |
Output is correct |
22 |
Correct |
7 ms |
45904 KB |
Output is correct |
23 |
Correct |
10 ms |
45992 KB |
Output is correct |
24 |
Correct |
7 ms |
45904 KB |
Output is correct |
25 |
Correct |
7 ms |
45904 KB |
Output is correct |
26 |
Correct |
8 ms |
45904 KB |
Output is correct |
27 |
Correct |
11 ms |
45904 KB |
Output is correct |
28 |
Correct |
8 ms |
45872 KB |
Output is correct |
29 |
Correct |
8 ms |
45904 KB |
Output is correct |
30 |
Correct |
8 ms |
45648 KB |
Output is correct |
31 |
Correct |
11 ms |
45648 KB |
Output is correct |
32 |
Correct |
8 ms |
45648 KB |
Output is correct |
33 |
Correct |
15 ms |
45916 KB |
Output is correct |
34 |
Correct |
12 ms |
45904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
45648 KB |
Output is correct |
2 |
Correct |
7 ms |
45648 KB |
Output is correct |
3 |
Correct |
11 ms |
45648 KB |
Output is correct |
4 |
Correct |
8 ms |
45648 KB |
Output is correct |
5 |
Correct |
6 ms |
45648 KB |
Output is correct |
6 |
Correct |
6 ms |
45648 KB |
Output is correct |
7 |
Correct |
9 ms |
45648 KB |
Output is correct |
8 |
Correct |
10 ms |
45648 KB |
Output is correct |
9 |
Correct |
6 ms |
45648 KB |
Output is correct |
10 |
Correct |
9 ms |
45648 KB |
Output is correct |
11 |
Correct |
7 ms |
45904 KB |
Output is correct |
12 |
Correct |
7 ms |
45908 KB |
Output is correct |
13 |
Correct |
7 ms |
45904 KB |
Output is correct |
14 |
Correct |
7 ms |
45904 KB |
Output is correct |
15 |
Correct |
7 ms |
45904 KB |
Output is correct |
16 |
Correct |
7 ms |
45648 KB |
Output is correct |
17 |
Correct |
8 ms |
45904 KB |
Output is correct |
18 |
Correct |
7 ms |
45904 KB |
Output is correct |
19 |
Correct |
7 ms |
45648 KB |
Output is correct |
20 |
Correct |
7 ms |
45904 KB |
Output is correct |
21 |
Correct |
7 ms |
45648 KB |
Output is correct |
22 |
Correct |
7 ms |
45648 KB |
Output is correct |
23 |
Correct |
7 ms |
45904 KB |
Output is correct |
24 |
Correct |
8 ms |
45904 KB |
Output is correct |
25 |
Correct |
7 ms |
45904 KB |
Output is correct |
26 |
Correct |
7 ms |
45904 KB |
Output is correct |
27 |
Correct |
7 ms |
45904 KB |
Output is correct |
28 |
Correct |
9 ms |
45904 KB |
Output is correct |
29 |
Correct |
8 ms |
45904 KB |
Output is correct |
30 |
Correct |
7 ms |
45648 KB |
Output is correct |
31 |
Correct |
8 ms |
45648 KB |
Output is correct |
32 |
Correct |
8 ms |
45648 KB |
Output is correct |
33 |
Correct |
9 ms |
45904 KB |
Output is correct |
34 |
Correct |
12 ms |
45820 KB |
Output is correct |
35 |
Correct |
15 ms |
46160 KB |
Output is correct |
36 |
Correct |
7 ms |
45904 KB |
Output is correct |
37 |
Correct |
14 ms |
46152 KB |
Output is correct |
38 |
Correct |
15 ms |
46416 KB |
Output is correct |
39 |
Correct |
15 ms |
46584 KB |
Output is correct |
40 |
Correct |
17 ms |
46416 KB |
Output is correct |
41 |
Correct |
15 ms |
46416 KB |
Output is correct |
42 |
Correct |
12 ms |
46160 KB |
Output is correct |
43 |
Correct |
10 ms |
46292 KB |
Output is correct |
44 |
Correct |
10 ms |
46160 KB |
Output is correct |
45 |
Correct |
18 ms |
46584 KB |
Output is correct |
46 |
Correct |
17 ms |
46416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
45648 KB |
Output is correct |
2 |
Correct |
6 ms |
45648 KB |
Output is correct |
3 |
Correct |
6 ms |
45748 KB |
Output is correct |
4 |
Correct |
6 ms |
45712 KB |
Output is correct |
5 |
Correct |
6 ms |
45648 KB |
Output is correct |
6 |
Correct |
6 ms |
45648 KB |
Output is correct |
7 |
Correct |
6 ms |
45816 KB |
Output is correct |
8 |
Correct |
6 ms |
45772 KB |
Output is correct |
9 |
Correct |
6 ms |
45648 KB |
Output is correct |
10 |
Correct |
6 ms |
45648 KB |
Output is correct |
11 |
Correct |
6 ms |
45916 KB |
Output is correct |
12 |
Correct |
6 ms |
45904 KB |
Output is correct |
13 |
Correct |
6 ms |
45904 KB |
Output is correct |
14 |
Correct |
6 ms |
45820 KB |
Output is correct |
15 |
Correct |
6 ms |
45904 KB |
Output is correct |
16 |
Correct |
6 ms |
45648 KB |
Output is correct |
17 |
Correct |
7 ms |
46076 KB |
Output is correct |
18 |
Correct |
6 ms |
45904 KB |
Output is correct |
19 |
Correct |
6 ms |
45904 KB |
Output is correct |
20 |
Correct |
6 ms |
45904 KB |
Output is correct |
21 |
Correct |
6 ms |
45648 KB |
Output is correct |
22 |
Correct |
7 ms |
45816 KB |
Output is correct |
23 |
Correct |
6 ms |
45904 KB |
Output is correct |
24 |
Correct |
7 ms |
45904 KB |
Output is correct |
25 |
Correct |
7 ms |
45904 KB |
Output is correct |
26 |
Correct |
7 ms |
45904 KB |
Output is correct |
27 |
Correct |
6 ms |
45792 KB |
Output is correct |
28 |
Correct |
7 ms |
45904 KB |
Output is correct |
29 |
Correct |
7 ms |
45648 KB |
Output is correct |
30 |
Correct |
8 ms |
45648 KB |
Output is correct |
31 |
Correct |
8 ms |
45764 KB |
Output is correct |
32 |
Correct |
7 ms |
45648 KB |
Output is correct |
33 |
Correct |
8 ms |
45904 KB |
Output is correct |
34 |
Correct |
8 ms |
45700 KB |
Output is correct |
35 |
Correct |
13 ms |
46360 KB |
Output is correct |
36 |
Correct |
7 ms |
45904 KB |
Output is correct |
37 |
Correct |
12 ms |
46304 KB |
Output is correct |
38 |
Correct |
14 ms |
46416 KB |
Output is correct |
39 |
Correct |
14 ms |
46416 KB |
Output is correct |
40 |
Correct |
14 ms |
46416 KB |
Output is correct |
41 |
Correct |
14 ms |
46384 KB |
Output is correct |
42 |
Correct |
12 ms |
46160 KB |
Output is correct |
43 |
Correct |
11 ms |
46216 KB |
Output is correct |
44 |
Correct |
13 ms |
46160 KB |
Output is correct |
45 |
Correct |
21 ms |
46416 KB |
Output is correct |
46 |
Correct |
23 ms |
46436 KB |
Output is correct |
47 |
Correct |
36 ms |
46840 KB |
Output is correct |
48 |
Correct |
12 ms |
46428 KB |
Output is correct |
49 |
Correct |
11 ms |
46416 KB |
Output is correct |
50 |
Correct |
9 ms |
46160 KB |
Output is correct |
51 |
Correct |
36 ms |
46928 KB |
Output is correct |
52 |
Correct |
34 ms |
46944 KB |
Output is correct |
53 |
Correct |
15 ms |
46672 KB |
Output is correct |
54 |
Correct |
6 ms |
45648 KB |
Output is correct |
55 |
Correct |
8 ms |
45648 KB |
Output is correct |
56 |
Correct |
13 ms |
46928 KB |
Output is correct |
57 |
Correct |
23 ms |
45904 KB |
Output is correct |
58 |
Correct |
10 ms |
46264 KB |
Output is correct |
59 |
Correct |
12 ms |
46368 KB |
Output is correct |
60 |
Correct |
14 ms |
46160 KB |
Output is correct |
61 |
Correct |
12 ms |
46160 KB |
Output is correct |
62 |
Correct |
21 ms |
46940 KB |
Output is correct |
63 |
Correct |
21 ms |
46928 KB |
Output is correct |
64 |
Correct |
24 ms |
47096 KB |
Output is correct |
65 |
Correct |
71 ms |
46964 KB |
Output is correct |
66 |
Correct |
134 ms |
47160 KB |
Output is correct |
67 |
Correct |
139 ms |
47060 KB |
Output is correct |