#include<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
const int BUC = 175;
int b[30001], p[30001], n, m;
int dist[(BUC+1)*30000+10];
struct node
{
int m, n;
node(int M, int N):n(N){if(M>BUC)M=0; m=M;}
node(){m=n=0;}
node(int x):n(b[x])
{
if(p[x] <= BUC) m = p[x];
else m = 0;
}
bool operator<(const node &A) const{return false;}
};
int& d(const node &A){return dist[A.n*(BUC+1)+A.m];}
std::vector<int> blist[30001];
void add(int P, int B, int nd, priority_queue<pair<int, node> > &Q)
{
//printf("##%d %d %d\n", P, B, nd);
if(B<0 || B>=n) return;
node X(P, B);
if(d(X) == -1 || d(X) > nd)
{
d(X)=nd;
Q.push(make_pair(-d(X), X));
}
}
int abs(int x){return x>0?x:-x;}
int main()
{
int i;
scanf("%d%d", &n, &m);
for(i=0; i<m; i++) scanf("%d%d", b+i, p+i);
for(i=0; i<m; i++) blist[b[i]].push_back(p[i]);
priority_queue<pair<int, node> > Q;
Q.push(make_pair(0, node(0)));
memset(dist, -1, sizeof(dist));
d(node(0)) = 0;
//printf("-%d %d %d\n", dist[352], dist[353], dist[354]);
while(!Q.empty())
{
node x = Q.top().second;
if(d(x) != -Q.top().first)
{
Q.pop();
continue;
}
Q.pop();
if(x.n==b[1] && x.m == 0) break;
if(x.m == 0) for(auto pp: blist[x.n])
{
if(pp <= BUC) add(pp, x.n, d(x), Q);
else for(i=x.n%pp; i<n; i+=pp) add(0, i, d(x)+abs(i-x.n)/pp, Q);
}
else
{
add(0, x.n, d(x), Q);
add(x.m, x.n+x.m, d(x)+1, Q);
add(x.m, x.n-x.m, d(x)+1, Q);
}
}
printf("%d\n", d(node(0, b[1])));
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
22772 KB |
Output is correct |
2 |
Correct |
5 ms |
22772 KB |
Output is correct |
3 |
Correct |
0 ms |
22772 KB |
Output is correct |
4 |
Correct |
0 ms |
22772 KB |
Output is correct |
5 |
Correct |
2 ms |
22772 KB |
Output is correct |
6 |
Correct |
0 ms |
22772 KB |
Output is correct |
7 |
Correct |
0 ms |
22772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22772 KB |
Output is correct |
2 |
Correct |
0 ms |
22772 KB |
Output is correct |
3 |
Correct |
5 ms |
22772 KB |
Output is correct |
4 |
Correct |
0 ms |
22772 KB |
Output is correct |
5 |
Correct |
2 ms |
22772 KB |
Output is correct |
6 |
Correct |
3 ms |
22772 KB |
Output is correct |
7 |
Correct |
0 ms |
22772 KB |
Output is correct |
8 |
Correct |
0 ms |
22772 KB |
Output is correct |
9 |
Correct |
0 ms |
22772 KB |
Output is correct |
10 |
Correct |
0 ms |
22772 KB |
Output is correct |
11 |
Correct |
0 ms |
22772 KB |
Output is correct |
12 |
Correct |
0 ms |
22772 KB |
Output is correct |
13 |
Correct |
0 ms |
22772 KB |
Output is correct |
14 |
Correct |
6 ms |
22772 KB |
Output is correct |
15 |
Correct |
3 ms |
22772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
22772 KB |
Output is correct |
2 |
Correct |
0 ms |
22772 KB |
Output is correct |
3 |
Correct |
5 ms |
22772 KB |
Output is correct |
4 |
Correct |
0 ms |
22772 KB |
Output is correct |
5 |
Correct |
0 ms |
22772 KB |
Output is correct |
6 |
Correct |
0 ms |
22772 KB |
Output is correct |
7 |
Correct |
2 ms |
22772 KB |
Output is correct |
8 |
Correct |
5 ms |
22772 KB |
Output is correct |
9 |
Correct |
5 ms |
22772 KB |
Output is correct |
10 |
Correct |
3 ms |
22772 KB |
Output is correct |
11 |
Correct |
6 ms |
22772 KB |
Output is correct |
12 |
Correct |
5 ms |
22772 KB |
Output is correct |
13 |
Correct |
6 ms |
22772 KB |
Output is correct |
14 |
Correct |
0 ms |
22772 KB |
Output is correct |
15 |
Correct |
6 ms |
22772 KB |
Output is correct |
16 |
Correct |
0 ms |
22772 KB |
Output is correct |
17 |
Correct |
3 ms |
22772 KB |
Output is correct |
18 |
Correct |
3 ms |
22772 KB |
Output is correct |
19 |
Correct |
0 ms |
22772 KB |
Output is correct |
20 |
Correct |
3 ms |
22772 KB |
Output is correct |
21 |
Correct |
0 ms |
22772 KB |
Output is correct |
22 |
Correct |
5 ms |
22772 KB |
Output is correct |
23 |
Correct |
0 ms |
22772 KB |
Output is correct |
24 |
Correct |
3 ms |
22772 KB |
Output is correct |
25 |
Correct |
0 ms |
22772 KB |
Output is correct |
26 |
Correct |
3 ms |
22772 KB |
Output is correct |
27 |
Correct |
6 ms |
22772 KB |
Output is correct |
28 |
Correct |
4 ms |
22772 KB |
Output is correct |
29 |
Correct |
8 ms |
22772 KB |
Output is correct |
30 |
Correct |
0 ms |
22772 KB |
Output is correct |
31 |
Correct |
9 ms |
22772 KB |
Output is correct |
32 |
Correct |
8 ms |
22772 KB |
Output is correct |
33 |
Correct |
15 ms |
22772 KB |
Output is correct |
34 |
Correct |
6 ms |
22772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22772 KB |
Output is correct |
2 |
Correct |
0 ms |
22772 KB |
Output is correct |
3 |
Correct |
5 ms |
22772 KB |
Output is correct |
4 |
Correct |
5 ms |
22772 KB |
Output is correct |
5 |
Correct |
0 ms |
22772 KB |
Output is correct |
6 |
Correct |
0 ms |
22772 KB |
Output is correct |
7 |
Correct |
0 ms |
22772 KB |
Output is correct |
8 |
Correct |
0 ms |
22772 KB |
Output is correct |
9 |
Correct |
0 ms |
22772 KB |
Output is correct |
10 |
Correct |
3 ms |
22772 KB |
Output is correct |
11 |
Correct |
3 ms |
22772 KB |
Output is correct |
12 |
Correct |
0 ms |
22772 KB |
Output is correct |
13 |
Correct |
0 ms |
22772 KB |
Output is correct |
14 |
Correct |
6 ms |
22772 KB |
Output is correct |
15 |
Correct |
0 ms |
22772 KB |
Output is correct |
16 |
Correct |
0 ms |
22772 KB |
Output is correct |
17 |
Correct |
0 ms |
22772 KB |
Output is correct |
18 |
Correct |
0 ms |
22772 KB |
Output is correct |
19 |
Correct |
5 ms |
22772 KB |
Output is correct |
20 |
Correct |
6 ms |
22772 KB |
Output is correct |
21 |
Correct |
5 ms |
22772 KB |
Output is correct |
22 |
Correct |
5 ms |
22772 KB |
Output is correct |
23 |
Correct |
6 ms |
22772 KB |
Output is correct |
24 |
Correct |
7 ms |
22772 KB |
Output is correct |
25 |
Correct |
0 ms |
22772 KB |
Output is correct |
26 |
Correct |
3 ms |
22772 KB |
Output is correct |
27 |
Correct |
0 ms |
22772 KB |
Output is correct |
28 |
Correct |
2 ms |
22772 KB |
Output is correct |
29 |
Correct |
8 ms |
22772 KB |
Output is correct |
30 |
Correct |
4 ms |
22772 KB |
Output is correct |
31 |
Correct |
9 ms |
22772 KB |
Output is correct |
32 |
Correct |
2 ms |
22772 KB |
Output is correct |
33 |
Correct |
15 ms |
22772 KB |
Output is correct |
34 |
Correct |
8 ms |
22772 KB |
Output is correct |
35 |
Correct |
15 ms |
23196 KB |
Output is correct |
36 |
Correct |
3 ms |
22772 KB |
Output is correct |
37 |
Correct |
8 ms |
23040 KB |
Output is correct |
38 |
Correct |
10 ms |
23036 KB |
Output is correct |
39 |
Correct |
10 ms |
23036 KB |
Output is correct |
40 |
Correct |
0 ms |
23036 KB |
Output is correct |
41 |
Correct |
9 ms |
23224 KB |
Output is correct |
42 |
Correct |
10 ms |
22904 KB |
Output is correct |
43 |
Correct |
0 ms |
22904 KB |
Output is correct |
44 |
Correct |
9 ms |
22904 KB |
Output is correct |
45 |
Correct |
53 ms |
23532 KB |
Output is correct |
46 |
Correct |
20 ms |
23532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
22772 KB |
Output is correct |
2 |
Correct |
0 ms |
22772 KB |
Output is correct |
3 |
Correct |
5 ms |
22772 KB |
Output is correct |
4 |
Correct |
3 ms |
22772 KB |
Output is correct |
5 |
Correct |
0 ms |
22772 KB |
Output is correct |
6 |
Correct |
0 ms |
22772 KB |
Output is correct |
7 |
Correct |
5 ms |
22772 KB |
Output is correct |
8 |
Correct |
0 ms |
22772 KB |
Output is correct |
9 |
Correct |
0 ms |
22772 KB |
Output is correct |
10 |
Correct |
0 ms |
22772 KB |
Output is correct |
11 |
Correct |
0 ms |
22772 KB |
Output is correct |
12 |
Correct |
6 ms |
22772 KB |
Output is correct |
13 |
Correct |
0 ms |
22772 KB |
Output is correct |
14 |
Correct |
0 ms |
22772 KB |
Output is correct |
15 |
Correct |
0 ms |
22772 KB |
Output is correct |
16 |
Correct |
3 ms |
22772 KB |
Output is correct |
17 |
Correct |
6 ms |
22772 KB |
Output is correct |
18 |
Correct |
0 ms |
22772 KB |
Output is correct |
19 |
Correct |
0 ms |
22772 KB |
Output is correct |
20 |
Correct |
0 ms |
22772 KB |
Output is correct |
21 |
Correct |
0 ms |
22772 KB |
Output is correct |
22 |
Correct |
0 ms |
22772 KB |
Output is correct |
23 |
Correct |
6 ms |
22772 KB |
Output is correct |
24 |
Correct |
0 ms |
22772 KB |
Output is correct |
25 |
Correct |
0 ms |
22772 KB |
Output is correct |
26 |
Correct |
0 ms |
22772 KB |
Output is correct |
27 |
Correct |
0 ms |
22772 KB |
Output is correct |
28 |
Correct |
0 ms |
22772 KB |
Output is correct |
29 |
Correct |
8 ms |
22772 KB |
Output is correct |
30 |
Correct |
3 ms |
22772 KB |
Output is correct |
31 |
Correct |
5 ms |
22772 KB |
Output is correct |
32 |
Correct |
8 ms |
22772 KB |
Output is correct |
33 |
Correct |
15 ms |
22772 KB |
Output is correct |
34 |
Correct |
8 ms |
22772 KB |
Output is correct |
35 |
Correct |
10 ms |
23196 KB |
Output is correct |
36 |
Correct |
0 ms |
22772 KB |
Output is correct |
37 |
Correct |
12 ms |
23040 KB |
Output is correct |
38 |
Correct |
0 ms |
23036 KB |
Output is correct |
39 |
Correct |
0 ms |
23036 KB |
Output is correct |
40 |
Correct |
0 ms |
23036 KB |
Output is correct |
41 |
Correct |
15 ms |
23224 KB |
Output is correct |
42 |
Correct |
0 ms |
22904 KB |
Output is correct |
43 |
Correct |
0 ms |
22904 KB |
Output is correct |
44 |
Correct |
0 ms |
22904 KB |
Output is correct |
45 |
Correct |
45 ms |
23532 KB |
Output is correct |
46 |
Correct |
28 ms |
23532 KB |
Output is correct |
47 |
Correct |
12 ms |
23696 KB |
Output is correct |
48 |
Correct |
7 ms |
23168 KB |
Output is correct |
49 |
Correct |
0 ms |
23168 KB |
Output is correct |
50 |
Correct |
4 ms |
23036 KB |
Output is correct |
51 |
Correct |
19 ms |
23944 KB |
Output is correct |
52 |
Correct |
10 ms |
23948 KB |
Output is correct |
53 |
Correct |
11 ms |
23300 KB |
Output is correct |
54 |
Correct |
0 ms |
22772 KB |
Output is correct |
55 |
Correct |
4 ms |
22772 KB |
Output is correct |
56 |
Correct |
10 ms |
23696 KB |
Output is correct |
57 |
Correct |
0 ms |
22772 KB |
Output is correct |
58 |
Correct |
11 ms |
22904 KB |
Output is correct |
59 |
Correct |
19 ms |
22904 KB |
Output is correct |
60 |
Correct |
15 ms |
22908 KB |
Output is correct |
61 |
Correct |
11 ms |
22920 KB |
Output is correct |
62 |
Correct |
44 ms |
23696 KB |
Output is correct |
63 |
Correct |
50 ms |
25320 KB |
Output is correct |
64 |
Correct |
56 ms |
25280 KB |
Output is correct |
65 |
Correct |
236 ms |
25292 KB |
Output is correct |
66 |
Correct |
738 ms |
25256 KB |
Output is correct |
67 |
Correct |
324 ms |
25260 KB |
Output is correct |