#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P2;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
const int B = 200;
//const int B = 1;
int N, M;
vector<int> G[30000];
int D[30000][B+1];
signed main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> N >> M;
rep(i, N) rep(j, B+1) D[i][j] = INF;
int sx = 0, tx = 0;
rep(i, M) {
int b, p;
cin >> b >> p;
if (i == 0) sx = b;
else if (i == 1) tx = b;
G[b].pb(p);
}
rep(i, N) sort(all(G[i])), uniq(G[i]);
priority_queue<P2, vector<P2>, greater<P2> > q;
D[sx][0] = 0;
q.push(P2(0, P(sx, 0)));
while (!q.empty()) {
int x = q.top()._2._1,
p = q.top()._2._2,
r = q.top()._1; q.pop();
if (D[x][p] < r) continue;
if (p == 0) {
for (int np : G[x]) {
if (np <= B) {
if (D[x][np] > r) {
D[x][np] = r;
q.push(P2(r, P(x, np)));
}
}
else {
for (int t=x%np; t<N; t+=np) {
int cost = abs(x-t)/np;
if (D[t][0] > r+cost) {
D[t][0] = r+cost;
q.push(P2(r+cost, P(t, 0)));
}
}
}
}
}
else {
if (D[x][0] > r) {
D[x][0] = r;
q.push(P2(r, P(x, 0)));
}
if (x-p >= 0) {
if (D[x-p][p] > r+1) {
D[x-p][p] = r+1;
q.push(P2(r+1, P(x-p, p)));
}
}
if (x+p < N) {
if (D[x+p][p] > r+1) {
D[x+p][p] = r+1;
q.push(P2(r+1, P(x+p, p)));
}
}
}
}
int m = D[tx][0];
if (m == INF) m = -1;
cout << m << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
26436 KB |
Output is correct |
2 |
Correct |
0 ms |
26436 KB |
Output is correct |
3 |
Correct |
0 ms |
26436 KB |
Output is correct |
4 |
Correct |
0 ms |
26436 KB |
Output is correct |
5 |
Correct |
0 ms |
26436 KB |
Output is correct |
6 |
Correct |
0 ms |
26436 KB |
Output is correct |
7 |
Correct |
0 ms |
26436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
26436 KB |
Output is correct |
2 |
Correct |
0 ms |
26436 KB |
Output is correct |
3 |
Correct |
0 ms |
26436 KB |
Output is correct |
4 |
Correct |
0 ms |
26436 KB |
Output is correct |
5 |
Correct |
0 ms |
26436 KB |
Output is correct |
6 |
Correct |
0 ms |
26436 KB |
Output is correct |
7 |
Correct |
0 ms |
26436 KB |
Output is correct |
8 |
Correct |
0 ms |
26436 KB |
Output is correct |
9 |
Correct |
0 ms |
26436 KB |
Output is correct |
10 |
Correct |
0 ms |
26436 KB |
Output is correct |
11 |
Correct |
0 ms |
26436 KB |
Output is correct |
12 |
Correct |
0 ms |
26436 KB |
Output is correct |
13 |
Correct |
0 ms |
26436 KB |
Output is correct |
14 |
Correct |
0 ms |
26436 KB |
Output is correct |
15 |
Correct |
0 ms |
26436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
26436 KB |
Output is correct |
2 |
Correct |
0 ms |
26436 KB |
Output is correct |
3 |
Correct |
0 ms |
26436 KB |
Output is correct |
4 |
Correct |
0 ms |
26436 KB |
Output is correct |
5 |
Correct |
0 ms |
26436 KB |
Output is correct |
6 |
Correct |
0 ms |
26436 KB |
Output is correct |
7 |
Correct |
0 ms |
26436 KB |
Output is correct |
8 |
Correct |
0 ms |
26436 KB |
Output is correct |
9 |
Correct |
0 ms |
26436 KB |
Output is correct |
10 |
Correct |
0 ms |
26436 KB |
Output is correct |
11 |
Correct |
0 ms |
26436 KB |
Output is correct |
12 |
Correct |
0 ms |
26436 KB |
Output is correct |
13 |
Correct |
0 ms |
26436 KB |
Output is correct |
14 |
Correct |
0 ms |
26436 KB |
Output is correct |
15 |
Correct |
0 ms |
26436 KB |
Output is correct |
16 |
Correct |
0 ms |
26436 KB |
Output is correct |
17 |
Correct |
3 ms |
26436 KB |
Output is correct |
18 |
Correct |
0 ms |
26436 KB |
Output is correct |
19 |
Correct |
0 ms |
26436 KB |
Output is correct |
20 |
Correct |
0 ms |
26436 KB |
Output is correct |
21 |
Correct |
0 ms |
26436 KB |
Output is correct |
22 |
Correct |
0 ms |
26436 KB |
Output is correct |
23 |
Correct |
3 ms |
26436 KB |
Output is correct |
24 |
Correct |
0 ms |
26436 KB |
Output is correct |
25 |
Correct |
0 ms |
26436 KB |
Output is correct |
26 |
Correct |
0 ms |
26436 KB |
Output is correct |
27 |
Correct |
0 ms |
26436 KB |
Output is correct |
28 |
Correct |
0 ms |
26436 KB |
Output is correct |
29 |
Correct |
9 ms |
26436 KB |
Output is correct |
30 |
Correct |
3 ms |
26436 KB |
Output is correct |
31 |
Correct |
3 ms |
26436 KB |
Output is correct |
32 |
Correct |
3 ms |
26436 KB |
Output is correct |
33 |
Correct |
16 ms |
26436 KB |
Output is correct |
34 |
Correct |
26 ms |
26436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
26436 KB |
Output is correct |
2 |
Correct |
0 ms |
26436 KB |
Output is correct |
3 |
Correct |
0 ms |
26436 KB |
Output is correct |
4 |
Correct |
0 ms |
26436 KB |
Output is correct |
5 |
Correct |
0 ms |
26436 KB |
Output is correct |
6 |
Correct |
0 ms |
26436 KB |
Output is correct |
7 |
Correct |
0 ms |
26436 KB |
Output is correct |
8 |
Correct |
0 ms |
26436 KB |
Output is correct |
9 |
Correct |
0 ms |
26436 KB |
Output is correct |
10 |
Correct |
0 ms |
26436 KB |
Output is correct |
11 |
Correct |
0 ms |
26436 KB |
Output is correct |
12 |
Correct |
0 ms |
26436 KB |
Output is correct |
13 |
Correct |
0 ms |
26436 KB |
Output is correct |
14 |
Correct |
0 ms |
26436 KB |
Output is correct |
15 |
Correct |
0 ms |
26436 KB |
Output is correct |
16 |
Correct |
0 ms |
26436 KB |
Output is correct |
17 |
Correct |
3 ms |
26436 KB |
Output is correct |
18 |
Correct |
0 ms |
26436 KB |
Output is correct |
19 |
Correct |
0 ms |
26436 KB |
Output is correct |
20 |
Correct |
0 ms |
26436 KB |
Output is correct |
21 |
Correct |
0 ms |
26436 KB |
Output is correct |
22 |
Correct |
0 ms |
26436 KB |
Output is correct |
23 |
Correct |
0 ms |
26436 KB |
Output is correct |
24 |
Correct |
0 ms |
26436 KB |
Output is correct |
25 |
Correct |
0 ms |
26436 KB |
Output is correct |
26 |
Correct |
0 ms |
26436 KB |
Output is correct |
27 |
Correct |
0 ms |
26436 KB |
Output is correct |
28 |
Correct |
3 ms |
26436 KB |
Output is correct |
29 |
Correct |
9 ms |
26436 KB |
Output is correct |
30 |
Correct |
3 ms |
26436 KB |
Output is correct |
31 |
Correct |
3 ms |
26436 KB |
Output is correct |
32 |
Correct |
3 ms |
26436 KB |
Output is correct |
33 |
Correct |
16 ms |
26436 KB |
Output is correct |
34 |
Correct |
16 ms |
26436 KB |
Output is correct |
35 |
Correct |
16 ms |
26888 KB |
Output is correct |
36 |
Correct |
3 ms |
26436 KB |
Output is correct |
37 |
Correct |
23 ms |
26852 KB |
Output is correct |
38 |
Correct |
36 ms |
26888 KB |
Output is correct |
39 |
Correct |
19 ms |
26888 KB |
Output is correct |
40 |
Correct |
26 ms |
26888 KB |
Output is correct |
41 |
Correct |
26 ms |
26888 KB |
Output is correct |
42 |
Correct |
3 ms |
26600 KB |
Output is correct |
43 |
Correct |
6 ms |
26600 KB |
Output is correct |
44 |
Correct |
6 ms |
26596 KB |
Output is correct |
45 |
Correct |
79 ms |
27232 KB |
Output is correct |
46 |
Correct |
73 ms |
27228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
26436 KB |
Output is correct |
2 |
Correct |
0 ms |
26436 KB |
Output is correct |
3 |
Correct |
0 ms |
26436 KB |
Output is correct |
4 |
Correct |
0 ms |
26436 KB |
Output is correct |
5 |
Correct |
0 ms |
26436 KB |
Output is correct |
6 |
Correct |
0 ms |
26436 KB |
Output is correct |
7 |
Correct |
0 ms |
26436 KB |
Output is correct |
8 |
Correct |
0 ms |
26436 KB |
Output is correct |
9 |
Correct |
0 ms |
26436 KB |
Output is correct |
10 |
Correct |
0 ms |
26436 KB |
Output is correct |
11 |
Correct |
0 ms |
26436 KB |
Output is correct |
12 |
Correct |
0 ms |
26436 KB |
Output is correct |
13 |
Correct |
0 ms |
26436 KB |
Output is correct |
14 |
Correct |
0 ms |
26436 KB |
Output is correct |
15 |
Correct |
0 ms |
26436 KB |
Output is correct |
16 |
Correct |
0 ms |
26436 KB |
Output is correct |
17 |
Correct |
0 ms |
26436 KB |
Output is correct |
18 |
Correct |
0 ms |
26436 KB |
Output is correct |
19 |
Correct |
0 ms |
26436 KB |
Output is correct |
20 |
Correct |
0 ms |
26436 KB |
Output is correct |
21 |
Correct |
0 ms |
26436 KB |
Output is correct |
22 |
Correct |
0 ms |
26436 KB |
Output is correct |
23 |
Correct |
0 ms |
26436 KB |
Output is correct |
24 |
Correct |
3 ms |
26436 KB |
Output is correct |
25 |
Correct |
0 ms |
26436 KB |
Output is correct |
26 |
Correct |
0 ms |
26436 KB |
Output is correct |
27 |
Correct |
0 ms |
26436 KB |
Output is correct |
28 |
Correct |
0 ms |
26436 KB |
Output is correct |
29 |
Correct |
6 ms |
26436 KB |
Output is correct |
30 |
Correct |
3 ms |
26436 KB |
Output is correct |
31 |
Correct |
3 ms |
26436 KB |
Output is correct |
32 |
Correct |
3 ms |
26436 KB |
Output is correct |
33 |
Correct |
16 ms |
26436 KB |
Output is correct |
34 |
Correct |
19 ms |
26436 KB |
Output is correct |
35 |
Correct |
9 ms |
26888 KB |
Output is correct |
36 |
Correct |
3 ms |
26436 KB |
Output is correct |
37 |
Correct |
29 ms |
26852 KB |
Output is correct |
38 |
Correct |
26 ms |
26888 KB |
Output is correct |
39 |
Correct |
23 ms |
26888 KB |
Output is correct |
40 |
Correct |
23 ms |
26888 KB |
Output is correct |
41 |
Correct |
26 ms |
26888 KB |
Output is correct |
42 |
Correct |
3 ms |
26600 KB |
Output is correct |
43 |
Correct |
3 ms |
26600 KB |
Output is correct |
44 |
Correct |
6 ms |
26596 KB |
Output is correct |
45 |
Correct |
69 ms |
27232 KB |
Output is correct |
46 |
Correct |
79 ms |
27228 KB |
Output is correct |
47 |
Correct |
133 ms |
27396 KB |
Output is correct |
48 |
Correct |
3 ms |
26832 KB |
Output is correct |
49 |
Correct |
16 ms |
26832 KB |
Output is correct |
50 |
Correct |
13 ms |
26700 KB |
Output is correct |
51 |
Correct |
86 ms |
27644 KB |
Output is correct |
52 |
Correct |
73 ms |
27648 KB |
Output is correct |
53 |
Correct |
26 ms |
26964 KB |
Output is correct |
54 |
Correct |
6 ms |
26436 KB |
Output is correct |
55 |
Correct |
19 ms |
26436 KB |
Output is correct |
56 |
Correct |
16 ms |
27360 KB |
Output is correct |
57 |
Correct |
106 ms |
26436 KB |
Output is correct |
58 |
Correct |
23 ms |
26596 KB |
Output is correct |
59 |
Correct |
23 ms |
26596 KB |
Output is correct |
60 |
Correct |
19 ms |
26604 KB |
Output is correct |
61 |
Correct |
16 ms |
26612 KB |
Output is correct |
62 |
Correct |
66 ms |
27360 KB |
Output is correct |
63 |
Correct |
83 ms |
29008 KB |
Output is correct |
64 |
Correct |
83 ms |
28984 KB |
Output is correct |
65 |
Correct |
489 ms |
29000 KB |
Output is correct |
66 |
Execution timed out |
1000 ms |
28956 KB |
Execution timed out |
67 |
Halted |
0 ms |
0 KB |
- |