#include <bits/stdc++.h>
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size() )
const int MAX = 30010;
const int SQ = 176 ;
using namespace std ;
char c ;
void read(int &num)
{
num = 0 ;
for(c=getchar() ; ( c > 47 && c < 58 ) ; c=getchar() )
num = num*10 + c - 48 ;
}
int n , m ;
int b[MAX] , p[MAX] , dist[MAX] ;
vector<int> content[MAX] ;
vector<int> noName[SQ+5][SQ+5] ;
void dijkstra()
{
for(int i = 0 ; i < n ; i++ ) dist[i] = MAX*MAX ;
dist[ b[0] ] = 0 ;
priority_queue< pii , vector<pii> , greater<pii> > fila ;
fila.push(make_pair(0, b[0] ) ) ;
while(!fila.empty() )
{
int d = fila.top().first ;
int curr = fila.top().second ;
fila.pop() ;
if( d != dist[curr] ) continue ;
for(auto e : content[curr] )
{
if( p[e] >= SQ )
{
for(int i = b[e] - p[e], w = 1 ; i >= 0 ; i-= p[e] , w++ )
if( d + w < dist[i] )
{
dist[i] = d + w ;
fila.push(make_pair(d+w, i) ) ;
}
for(int i = b[e] + p[e] , w = 1 ; i < n ; i+= p[e] , w++ )
if( d + w < dist[i] )
{
dist[i] = d + w ;
fila.push(make_pair(d+w, i) ) ;
}
}
else
{
int firstDim = b[e] % p[e] ;
int secondDim = p[e] ;
int x = lower_bound(all(noName[firstDim][secondDim]), b[e]) - noName[firstDim][secondDim].begin() ;
int limLow = (x == 0) ? -1 : noName[firstDim][secondDim][x-1] ;
int limUp = (x == sz(noName[firstDim][secondDim])-1 ) ? -1 : noName[firstDim][secondDim][x+1] ;
for(int i = curr - p[e] , w = 1 ; i >= 0 ; i -= p[e] , w++ )
{
if(d + w < dist[i] )
{
dist[i] = d + w ;
fila.push(make_pair(d+w, i) ) ;
}
if( i == limLow ) break ;
}
for(int i = b[e] + p[e] , w = 1 ; i < n ; i += p[e] , w++ )
{
if(d + w < dist[i] )
{
dist[i] = d + w ;
fila.push(make_pair(d+w, i) ) ;
}
if( i == limUp ) break ;
}
}
}
}
printf("%d\n", dist[ b[1] ] == MAX*MAX ? -1 : dist[b[1]] ) ;
}
int main()
{
read(n) ; read(m) ;
for(int i = 0 ; i < m ; i++ )
{
read(b[i] ) ; read(p[i] ) ;
content[ b[i] ].push_back(i) ;
if( p[i] < SQ ) noName[ b[i] % p[i] ][ p[i] ].push_back( b[i] ) ;
}
for(int i = 0 ; i < SQ ; i++ )
for(int j = 0 ; j < SQ; j++ )
{
sort(all(noName[i][j]) );
noName[i][j].erase( unique(all(noName[i][j])) , noName[i][j].end() ) ;
}
dijkstra() ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
2 ms |
1792 KB |
Output is correct |
3 |
Correct |
1 ms |
1784 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
2 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
2 ms |
1772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
8 |
Correct |
1 ms |
1772 KB |
Output is correct |
9 |
Correct |
2 ms |
1772 KB |
Output is correct |
10 |
Correct |
1 ms |
1772 KB |
Output is correct |
11 |
Correct |
2 ms |
1900 KB |
Output is correct |
12 |
Correct |
2 ms |
1772 KB |
Output is correct |
13 |
Correct |
2 ms |
1900 KB |
Output is correct |
14 |
Correct |
2 ms |
1920 KB |
Output is correct |
15 |
Correct |
2 ms |
1900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
8 |
Correct |
1 ms |
1772 KB |
Output is correct |
9 |
Correct |
2 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1772 KB |
Output is correct |
11 |
Correct |
2 ms |
1900 KB |
Output is correct |
12 |
Correct |
2 ms |
1772 KB |
Output is correct |
13 |
Correct |
2 ms |
1900 KB |
Output is correct |
14 |
Correct |
2 ms |
1900 KB |
Output is correct |
15 |
Correct |
2 ms |
1900 KB |
Output is correct |
16 |
Correct |
2 ms |
1772 KB |
Output is correct |
17 |
Correct |
2 ms |
1900 KB |
Output is correct |
18 |
Correct |
2 ms |
1900 KB |
Output is correct |
19 |
Correct |
2 ms |
1772 KB |
Output is correct |
20 |
Correct |
2 ms |
1900 KB |
Output is correct |
21 |
Correct |
2 ms |
1772 KB |
Output is correct |
22 |
Correct |
2 ms |
1772 KB |
Output is correct |
23 |
Correct |
2 ms |
1900 KB |
Output is correct |
24 |
Correct |
2 ms |
1900 KB |
Output is correct |
25 |
Correct |
2 ms |
1900 KB |
Output is correct |
26 |
Correct |
6 ms |
1900 KB |
Output is correct |
27 |
Correct |
5 ms |
1900 KB |
Output is correct |
28 |
Correct |
2 ms |
1900 KB |
Output is correct |
29 |
Correct |
3 ms |
2028 KB |
Output is correct |
30 |
Correct |
2 ms |
1900 KB |
Output is correct |
31 |
Correct |
2 ms |
1900 KB |
Output is correct |
32 |
Correct |
2 ms |
1900 KB |
Output is correct |
33 |
Correct |
4 ms |
2156 KB |
Output is correct |
34 |
Correct |
4 ms |
2156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
2 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
8 |
Correct |
1 ms |
1772 KB |
Output is correct |
9 |
Correct |
1 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1772 KB |
Output is correct |
11 |
Correct |
2 ms |
1900 KB |
Output is correct |
12 |
Correct |
2 ms |
1772 KB |
Output is correct |
13 |
Correct |
2 ms |
1900 KB |
Output is correct |
14 |
Correct |
2 ms |
1900 KB |
Output is correct |
15 |
Correct |
2 ms |
1900 KB |
Output is correct |
16 |
Correct |
2 ms |
1772 KB |
Output is correct |
17 |
Correct |
2 ms |
1900 KB |
Output is correct |
18 |
Correct |
2 ms |
1900 KB |
Output is correct |
19 |
Correct |
2 ms |
1792 KB |
Output is correct |
20 |
Correct |
3 ms |
1900 KB |
Output is correct |
21 |
Correct |
2 ms |
1772 KB |
Output is correct |
22 |
Correct |
2 ms |
1772 KB |
Output is correct |
23 |
Correct |
2 ms |
1900 KB |
Output is correct |
24 |
Correct |
2 ms |
1900 KB |
Output is correct |
25 |
Correct |
2 ms |
1900 KB |
Output is correct |
26 |
Correct |
6 ms |
1920 KB |
Output is correct |
27 |
Correct |
5 ms |
1900 KB |
Output is correct |
28 |
Correct |
2 ms |
1900 KB |
Output is correct |
29 |
Correct |
3 ms |
2028 KB |
Output is correct |
30 |
Correct |
2 ms |
1900 KB |
Output is correct |
31 |
Correct |
2 ms |
1900 KB |
Output is correct |
32 |
Correct |
2 ms |
1900 KB |
Output is correct |
33 |
Correct |
4 ms |
2156 KB |
Output is correct |
34 |
Correct |
4 ms |
2156 KB |
Output is correct |
35 |
Correct |
6 ms |
2284 KB |
Output is correct |
36 |
Correct |
3 ms |
1900 KB |
Output is correct |
37 |
Correct |
6 ms |
2284 KB |
Output is correct |
38 |
Correct |
7 ms |
2412 KB |
Output is correct |
39 |
Correct |
7 ms |
2412 KB |
Output is correct |
40 |
Correct |
7 ms |
2412 KB |
Output is correct |
41 |
Correct |
7 ms |
2412 KB |
Output is correct |
42 |
Correct |
60 ms |
2412 KB |
Output is correct |
43 |
Correct |
52 ms |
2412 KB |
Output is correct |
44 |
Correct |
6 ms |
2412 KB |
Output is correct |
45 |
Correct |
9 ms |
2924 KB |
Output is correct |
46 |
Correct |
8 ms |
2924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
8 |
Correct |
1 ms |
1772 KB |
Output is correct |
9 |
Correct |
1 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1772 KB |
Output is correct |
11 |
Correct |
2 ms |
1900 KB |
Output is correct |
12 |
Correct |
2 ms |
1772 KB |
Output is correct |
13 |
Correct |
2 ms |
1920 KB |
Output is correct |
14 |
Correct |
2 ms |
1900 KB |
Output is correct |
15 |
Correct |
3 ms |
1900 KB |
Output is correct |
16 |
Correct |
2 ms |
1920 KB |
Output is correct |
17 |
Correct |
2 ms |
1900 KB |
Output is correct |
18 |
Correct |
2 ms |
1900 KB |
Output is correct |
19 |
Correct |
2 ms |
1772 KB |
Output is correct |
20 |
Correct |
2 ms |
1900 KB |
Output is correct |
21 |
Correct |
2 ms |
1772 KB |
Output is correct |
22 |
Correct |
2 ms |
1772 KB |
Output is correct |
23 |
Correct |
2 ms |
1900 KB |
Output is correct |
24 |
Correct |
2 ms |
1900 KB |
Output is correct |
25 |
Correct |
2 ms |
1920 KB |
Output is correct |
26 |
Correct |
7 ms |
1900 KB |
Output is correct |
27 |
Correct |
4 ms |
1900 KB |
Output is correct |
28 |
Correct |
2 ms |
1900 KB |
Output is correct |
29 |
Correct |
3 ms |
2028 KB |
Output is correct |
30 |
Correct |
2 ms |
1900 KB |
Output is correct |
31 |
Correct |
2 ms |
1900 KB |
Output is correct |
32 |
Correct |
2 ms |
1900 KB |
Output is correct |
33 |
Correct |
4 ms |
2156 KB |
Output is correct |
34 |
Correct |
4 ms |
2156 KB |
Output is correct |
35 |
Correct |
6 ms |
2284 KB |
Output is correct |
36 |
Correct |
3 ms |
1900 KB |
Output is correct |
37 |
Correct |
6 ms |
2284 KB |
Output is correct |
38 |
Correct |
9 ms |
2412 KB |
Output is correct |
39 |
Correct |
7 ms |
2412 KB |
Output is correct |
40 |
Correct |
9 ms |
2412 KB |
Output is correct |
41 |
Correct |
7 ms |
2412 KB |
Output is correct |
42 |
Correct |
66 ms |
2540 KB |
Output is correct |
43 |
Correct |
57 ms |
2420 KB |
Output is correct |
44 |
Correct |
7 ms |
2412 KB |
Output is correct |
45 |
Correct |
9 ms |
2924 KB |
Output is correct |
46 |
Correct |
9 ms |
3052 KB |
Output is correct |
47 |
Correct |
15 ms |
3048 KB |
Output is correct |
48 |
Correct |
5 ms |
2540 KB |
Output is correct |
49 |
Correct |
5 ms |
2572 KB |
Output is correct |
50 |
Correct |
5 ms |
2412 KB |
Output is correct |
51 |
Correct |
25 ms |
3440 KB |
Output is correct |
52 |
Correct |
26 ms |
3440 KB |
Output is correct |
53 |
Correct |
14 ms |
3436 KB |
Output is correct |
54 |
Correct |
3 ms |
2156 KB |
Output is correct |
55 |
Correct |
3 ms |
2156 KB |
Output is correct |
56 |
Correct |
12 ms |
3180 KB |
Output is correct |
57 |
Correct |
24 ms |
2672 KB |
Output is correct |
58 |
Correct |
776 ms |
2668 KB |
Output is correct |
59 |
Correct |
713 ms |
2724 KB |
Output is correct |
60 |
Correct |
775 ms |
3052 KB |
Output is correct |
61 |
Correct |
638 ms |
3004 KB |
Output is correct |
62 |
Correct |
9 ms |
3436 KB |
Output is correct |
63 |
Correct |
32 ms |
3820 KB |
Output is correct |
64 |
Correct |
31 ms |
3692 KB |
Output is correct |
65 |
Correct |
41 ms |
4968 KB |
Output is correct |
66 |
Correct |
59 ms |
5224 KB |
Output is correct |
67 |
Correct |
69 ms |
5224 KB |
Output is correct |