#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define russian setlocale(LC_ALL,"Russian_Russia.20866");
#define file freopen("onlyone.in", "r", stdin), freopen("onlyone.out", "w", stdout);
#define ll long long
#define ull unsigned long long
#define ld long double
#define pll pair<ll, ll>
#define pii pair<int, int>
#define all(s) s.begin(), s.end()
#define pb push_back
#define ins insert
#define mp make_pair
#define sz(x) x.size()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define mem(x) memset(x, 0, sizeof(x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 30010;
const ll M = 600000;
const ll block = 316;
const ll mod = 998244353;
const ll P = 263;
const ld pi = acos(-1);
const ll inf = 1e9;
ll add(ll a, ll b) {
if(a + b < 0) return a + b + mod;
if(a + b >= mod) return a + b - mod;
return a + b;
}
ll sub(ll a, ll b) {
return (a - b + mod) % mod;
}
ll mul(ll a, ll b) {
return a * 1LL * b % mod;
}
ll binpow(ll a, ll n) {
ll res = 1LL;
while(n) {
if(n & 1) res = mul(res, a); // if(n % 2 == 1)
a = mul(a, a);
n >>= 1; //n / 2
}
return res;
}
ll inv(ll x) {//число х в степени -1
return binpow(x, mod - 2);
}
ll n, m;
ll b[N], p[N], d[N];
vector<ll> g[N];
set<pll> s;
void solve() {
cin >> n >> m;
for(ll i = 0; i < m; i++) {
cin >> b[i] >> p[i];//b-дом p-прыжок
g[b[i]].pb(p[i]);
}
fill(d, d + n, LLONG_MAX);
d[b[0]] = 0LL;
s.insert({d[b[0]], b[0]});
while(!s.empty()) {
auto [c, v] = *s.begin();
s.erase(s.begin());
for(auto to: g[v]) {
ll delta = v + to, dist = 1LL;
while(delta < n) {
if(d[delta] > d[v] + dist) {
s.erase({d[delta], delta});
d[delta] = d[v] + dist;
s.insert({d[delta], delta});
}
delta += to;
dist++;
}
delta = v - to, dist = 1LL;
while(delta >= 0) {
if(d[delta] > d[v] + dist) {
s.erase({d[delta], delta});
d[delta] = d[v] + dist;
s.insert({d[delta], delta});
}
delta -= to;
dist++;
}
}
}
cout << (d[b[1]] == LLONG_MAX ? -1 : d[b[1]]) << '\n';
}
signed main() {
speed;
//file;
int test = 1;
//cin >> test;
while(test--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1168 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1176 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 |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
1172 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 |
1176 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 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 |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 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 |
1112 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 |
1192 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 |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1180 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
4 ms |
1116 KB |
Output is correct |
27 |
Correct |
4 ms |
1320 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
3 ms |
1112 KB |
Output is correct |
30 |
Correct |
2 ms |
1116 KB |
Output is correct |
31 |
Correct |
2 ms |
1116 KB |
Output is correct |
32 |
Correct |
1 ms |
1116 KB |
Output is correct |
33 |
Correct |
3 ms |
1372 KB |
Output is correct |
34 |
Correct |
3 ms |
1324 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 |
1116 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 |
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 |
2 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 |
1112 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 |
1424 KB |
Output is correct |
21 |
Correct |
1 ms |
1176 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 |
4 ms |
1184 KB |
Output is correct |
27 |
Correct |
3 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
3 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 ms |
1116 KB |
Output is correct |
31 |
Correct |
2 ms |
1116 KB |
Output is correct |
32 |
Correct |
2 ms |
1116 KB |
Output is correct |
33 |
Correct |
3 ms |
1372 KB |
Output is correct |
34 |
Correct |
4 ms |
1372 KB |
Output is correct |
35 |
Correct |
5 ms |
2140 KB |
Output is correct |
36 |
Correct |
2 ms |
1376 KB |
Output is correct |
37 |
Correct |
5 ms |
1884 KB |
Output is correct |
38 |
Correct |
7 ms |
2396 KB |
Output is correct |
39 |
Correct |
7 ms |
2296 KB |
Output is correct |
40 |
Correct |
7 ms |
2396 KB |
Output is correct |
41 |
Correct |
7 ms |
2396 KB |
Output is correct |
42 |
Correct |
42 ms |
2148 KB |
Output is correct |
43 |
Correct |
41 ms |
2012 KB |
Output is correct |
44 |
Correct |
43 ms |
2052 KB |
Output is correct |
45 |
Correct |
7 ms |
2140 KB |
Output is correct |
46 |
Correct |
6 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 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 |
0 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 |
1120 KB |
Output is correct |
9 |
Correct |
0 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
2 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 |
1184 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 |
1180 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 |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
3 ms |
1332 KB |
Output is correct |
27 |
Correct |
5 ms |
1180 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
3 ms |
1116 KB |
Output is correct |
30 |
Correct |
2 ms |
1116 KB |
Output is correct |
31 |
Correct |
2 ms |
1116 KB |
Output is correct |
32 |
Correct |
2 ms |
1116 KB |
Output is correct |
33 |
Correct |
3 ms |
1372 KB |
Output is correct |
34 |
Correct |
3 ms |
1368 KB |
Output is correct |
35 |
Correct |
6 ms |
2140 KB |
Output is correct |
36 |
Correct |
2 ms |
1372 KB |
Output is correct |
37 |
Correct |
6 ms |
1876 KB |
Output is correct |
38 |
Correct |
7 ms |
2396 KB |
Output is correct |
39 |
Correct |
6 ms |
2396 KB |
Output is correct |
40 |
Correct |
6 ms |
2204 KB |
Output is correct |
41 |
Correct |
9 ms |
2388 KB |
Output is correct |
42 |
Correct |
42 ms |
2012 KB |
Output is correct |
43 |
Correct |
42 ms |
2008 KB |
Output is correct |
44 |
Correct |
42 ms |
2052 KB |
Output is correct |
45 |
Correct |
6 ms |
2136 KB |
Output is correct |
46 |
Correct |
6 ms |
2140 KB |
Output is correct |
47 |
Correct |
20 ms |
2976 KB |
Output is correct |
48 |
Correct |
4 ms |
2396 KB |
Output is correct |
49 |
Correct |
4 ms |
2396 KB |
Output is correct |
50 |
Correct |
3 ms |
2140 KB |
Output is correct |
51 |
Correct |
39 ms |
4700 KB |
Output is correct |
52 |
Correct |
35 ms |
4184 KB |
Output is correct |
53 |
Correct |
14 ms |
4440 KB |
Output is correct |
54 |
Correct |
3 ms |
2140 KB |
Output is correct |
55 |
Correct |
4 ms |
2196 KB |
Output is correct |
56 |
Correct |
619 ms |
4884 KB |
Output is correct |
57 |
Correct |
75 ms |
3160 KB |
Output is correct |
58 |
Correct |
484 ms |
3036 KB |
Output is correct |
59 |
Correct |
600 ms |
3052 KB |
Output is correct |
60 |
Correct |
506 ms |
3040 KB |
Output is correct |
61 |
Correct |
524 ms |
3288 KB |
Output is correct |
62 |
Correct |
7 ms |
2904 KB |
Output is correct |
63 |
Correct |
53 ms |
4344 KB |
Output is correct |
64 |
Correct |
54 ms |
4184 KB |
Output is correct |
65 |
Correct |
65 ms |
4188 KB |
Output is correct |
66 |
Correct |
85 ms |
4184 KB |
Output is correct |
67 |
Correct |
86 ms |
4188 KB |
Output is correct |