#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(998244353)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX = 3e4 + 10;
int N, M, S, T;
vector<int> adj[MAX];
int f[MAX];
void solve(){
cin >> N >> M;
for(int i=0; i<M; i++){
int p, d;
cin >> p >> d;
adj[p].push_back(d);
if(i == 0){
S = p;
}
if(i == 1){
T = p;
}
}
memset(f, 0x3f, sizeof f);
f[S] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push(make_pair(0, S));
while(sz(pq)){
int u = pq.top().se;
int kc = pq.top().fi;
pq.pop();
if(kc > f[u]) continue;
for(int d: adj[u]){
int cnt = 0;
for(int i=u+d; i<N; i+=d){
cnt++;
if(minimize(f[i], f[u] + cnt)){
pq.push(make_pair(f[i], i));
}
}
cnt = 0;
for(int i=u-d; i>=0; i-=d){
cnt++;
if(minimize(f[i], f[u] + cnt)){
pq.push(make_pair(f[i], i));
}
}
}
}
cout << (f[T] >= INF ? -1 : f[T]) << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("numtable.inp","r",stdin);
// freopen("numtable.out","w",stdout);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
1136 KB |
Output is correct |
5 |
Correct |
1 ms |
1168 KB |
Output is correct |
6 |
Correct |
1 ms |
1276 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1112 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1112 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1112 KB |
Output is correct |
15 |
Correct |
1 ms |
1380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1168 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1176 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1112 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
3 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1112 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
1372 KB |
Output is correct |
26 |
Correct |
3 ms |
1112 KB |
Output is correct |
27 |
Correct |
3 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1372 KB |
Output is correct |
31 |
Correct |
1 ms |
1372 KB |
Output is correct |
32 |
Correct |
2 ms |
1372 KB |
Output is correct |
33 |
Correct |
2 ms |
1432 KB |
Output is correct |
34 |
Correct |
2 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1120 KB |
Output is correct |
19 |
Correct |
1 ms |
1112 KB |
Output is correct |
20 |
Correct |
4 ms |
1388 KB |
Output is correct |
21 |
Correct |
1 ms |
1112 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
3 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1432 KB |
Output is correct |
30 |
Correct |
1 ms |
1372 KB |
Output is correct |
31 |
Correct |
1 ms |
1368 KB |
Output is correct |
32 |
Correct |
1 ms |
1224 KB |
Output is correct |
33 |
Correct |
2 ms |
1372 KB |
Output is correct |
34 |
Correct |
3 ms |
1372 KB |
Output is correct |
35 |
Correct |
4 ms |
1540 KB |
Output is correct |
36 |
Correct |
1 ms |
1372 KB |
Output is correct |
37 |
Correct |
4 ms |
1744 KB |
Output is correct |
38 |
Correct |
5 ms |
1628 KB |
Output is correct |
39 |
Correct |
5 ms |
1628 KB |
Output is correct |
40 |
Correct |
5 ms |
1884 KB |
Output is correct |
41 |
Correct |
5 ms |
1884 KB |
Output is correct |
42 |
Correct |
41 ms |
1692 KB |
Output is correct |
43 |
Correct |
45 ms |
1728 KB |
Output is correct |
44 |
Correct |
42 ms |
1628 KB |
Output is correct |
45 |
Correct |
5 ms |
1884 KB |
Output is correct |
46 |
Correct |
4 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1168 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
4 ms |
1376 KB |
Output is correct |
21 |
Correct |
1 ms |
1120 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
1372 KB |
Output is correct |
26 |
Correct |
3 ms |
1120 KB |
Output is correct |
27 |
Correct |
3 ms |
1184 KB |
Output is correct |
28 |
Correct |
1 ms |
1120 KB |
Output is correct |
29 |
Correct |
2 ms |
1432 KB |
Output is correct |
30 |
Correct |
1 ms |
1376 KB |
Output is correct |
31 |
Correct |
1 ms |
1372 KB |
Output is correct |
32 |
Correct |
1 ms |
1368 KB |
Output is correct |
33 |
Correct |
2 ms |
1372 KB |
Output is correct |
34 |
Correct |
2 ms |
1440 KB |
Output is correct |
35 |
Correct |
4 ms |
1612 KB |
Output is correct |
36 |
Correct |
2 ms |
1372 KB |
Output is correct |
37 |
Correct |
4 ms |
1740 KB |
Output is correct |
38 |
Correct |
5 ms |
1632 KB |
Output is correct |
39 |
Correct |
5 ms |
1896 KB |
Output is correct |
40 |
Correct |
5 ms |
1892 KB |
Output is correct |
41 |
Correct |
6 ms |
1896 KB |
Output is correct |
42 |
Correct |
41 ms |
1640 KB |
Output is correct |
43 |
Correct |
41 ms |
1628 KB |
Output is correct |
44 |
Correct |
41 ms |
1624 KB |
Output is correct |
45 |
Correct |
5 ms |
1884 KB |
Output is correct |
46 |
Correct |
5 ms |
1884 KB |
Output is correct |
47 |
Correct |
11 ms |
2264 KB |
Output is correct |
48 |
Correct |
4 ms |
1884 KB |
Output is correct |
49 |
Correct |
5 ms |
1956 KB |
Output is correct |
50 |
Correct |
3 ms |
1628 KB |
Output is correct |
51 |
Correct |
18 ms |
3284 KB |
Output is correct |
52 |
Correct |
18 ms |
2772 KB |
Output is correct |
53 |
Correct |
11 ms |
2776 KB |
Output is correct |
54 |
Correct |
2 ms |
1372 KB |
Output is correct |
55 |
Correct |
2 ms |
1372 KB |
Output is correct |
56 |
Correct |
642 ms |
2756 KB |
Output is correct |
57 |
Correct |
32 ms |
3536 KB |
Output is correct |
58 |
Correct |
643 ms |
1628 KB |
Output is correct |
59 |
Correct |
571 ms |
1884 KB |
Output is correct |
60 |
Correct |
655 ms |
1628 KB |
Output is correct |
61 |
Correct |
538 ms |
1884 KB |
Output is correct |
62 |
Correct |
7 ms |
2392 KB |
Output is correct |
63 |
Correct |
21 ms |
2892 KB |
Output is correct |
64 |
Correct |
23 ms |
3788 KB |
Output is correct |
65 |
Correct |
28 ms |
3800 KB |
Output is correct |
66 |
Correct |
39 ms |
3968 KB |
Output is correct |
67 |
Correct |
43 ms |
3792 KB |
Output is correct |