#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define MOO(i, a, b) for(int i=a; i<b; i++)
#define M00(i, a) for(int i=0; i<a; i++)
#define MOOd(i,a,b) for(int i = (b)-1; i >= a; i--)
#define M00d(i,a) for(int i = (a)-1; i>=0; i--)
#define FAST ios::sync_with_stdio(0); cin.tie(0);
#define finish(x) return cout << x << '\n', 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << "\n";
#define _ << " _ " <<
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
const int inf = 1e8;
const int MAX_N = 30030;
int d[MAX_N];
bool v[MAX_N];
int n, m;
set<int> doges[MAX_N];
int main() { FAST
cin >> n >> m;
int st = -1, en = -1;
M00(i, m) {
int b, p; cin >> b >> p;
if(i == 0) st = b;
else if(i == 1) en = b;
doges[b].insert(p);
}
priority_queue<pi, vector<pi>, greater<pi>> pq;
M00(i, n) d[i] = inf;
d[st] = 0;
M00(i, n) v[i] = 0;
pq.push(mp(0, st));
while(!pq.empty()) {
pi t = pq.top(); pq.pop();
if(v[t.s]) continue;
v[t.s] = 1;
for(int j: doges[t.s]) {
int i = t.s;
for(int x = i+j; x < n; x += j) {
if(d[x] > d[t.s] + (x-i)/j) {
d[x] = d[t.s] + (x-i)/j;
pq.push(mp(d[t.s] + (x-i)/j, x));
}
if(doges[x].count(j)) break;
}
for(int x = i-j; x >= 0; x -= j) {
if(d[x] > d[t.s] + (i-x)/j) {
d[x] = d[t.s] + (i-x)/j;
pq.push(mp(d[t.s] + (i-x)/j, x));
}
if(doges[x].count(j)) break;
}
}
}
int res = d[en];
if(res == 1e8) {
finish(-1);
}
cout << res << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1792 KB |
Output is correct |
2 |
Correct |
6 ms |
1792 KB |
Output is correct |
3 |
Correct |
5 ms |
1792 KB |
Output is correct |
4 |
Correct |
6 ms |
1792 KB |
Output is correct |
5 |
Correct |
5 ms |
1792 KB |
Output is correct |
6 |
Correct |
5 ms |
1792 KB |
Output is correct |
7 |
Correct |
6 ms |
1792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1792 KB |
Output is correct |
2 |
Correct |
5 ms |
1792 KB |
Output is correct |
3 |
Correct |
5 ms |
1792 KB |
Output is correct |
4 |
Correct |
5 ms |
1792 KB |
Output is correct |
5 |
Correct |
5 ms |
1792 KB |
Output is correct |
6 |
Correct |
5 ms |
1792 KB |
Output is correct |
7 |
Correct |
5 ms |
1792 KB |
Output is correct |
8 |
Correct |
5 ms |
1792 KB |
Output is correct |
9 |
Correct |
5 ms |
1792 KB |
Output is correct |
10 |
Correct |
6 ms |
1792 KB |
Output is correct |
11 |
Correct |
6 ms |
1792 KB |
Output is correct |
12 |
Correct |
5 ms |
1792 KB |
Output is correct |
13 |
Correct |
5 ms |
1792 KB |
Output is correct |
14 |
Correct |
6 ms |
1920 KB |
Output is correct |
15 |
Correct |
6 ms |
1792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1824 KB |
Output is correct |
2 |
Correct |
5 ms |
1792 KB |
Output is correct |
3 |
Correct |
5 ms |
1792 KB |
Output is correct |
4 |
Correct |
5 ms |
1792 KB |
Output is correct |
5 |
Correct |
6 ms |
1792 KB |
Output is correct |
6 |
Correct |
5 ms |
1792 KB |
Output is correct |
7 |
Correct |
5 ms |
1792 KB |
Output is correct |
8 |
Correct |
5 ms |
1792 KB |
Output is correct |
9 |
Correct |
6 ms |
1792 KB |
Output is correct |
10 |
Correct |
5 ms |
1792 KB |
Output is correct |
11 |
Correct |
6 ms |
1920 KB |
Output is correct |
12 |
Correct |
5 ms |
1792 KB |
Output is correct |
13 |
Correct |
6 ms |
1792 KB |
Output is correct |
14 |
Correct |
6 ms |
1792 KB |
Output is correct |
15 |
Correct |
6 ms |
1792 KB |
Output is correct |
16 |
Correct |
6 ms |
1920 KB |
Output is correct |
17 |
Correct |
7 ms |
1792 KB |
Output is correct |
18 |
Correct |
6 ms |
1792 KB |
Output is correct |
19 |
Correct |
5 ms |
1792 KB |
Output is correct |
20 |
Correct |
7 ms |
1792 KB |
Output is correct |
21 |
Correct |
5 ms |
1792 KB |
Output is correct |
22 |
Correct |
6 ms |
1792 KB |
Output is correct |
23 |
Correct |
6 ms |
1792 KB |
Output is correct |
24 |
Correct |
7 ms |
1920 KB |
Output is correct |
25 |
Correct |
6 ms |
1920 KB |
Output is correct |
26 |
Correct |
6 ms |
1792 KB |
Output is correct |
27 |
Correct |
6 ms |
1792 KB |
Output is correct |
28 |
Correct |
6 ms |
1792 KB |
Output is correct |
29 |
Correct |
8 ms |
2048 KB |
Output is correct |
30 |
Correct |
6 ms |
1920 KB |
Output is correct |
31 |
Correct |
7 ms |
1920 KB |
Output is correct |
32 |
Correct |
6 ms |
1920 KB |
Output is correct |
33 |
Correct |
10 ms |
2300 KB |
Output is correct |
34 |
Correct |
10 ms |
2300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1792 KB |
Output is correct |
2 |
Correct |
6 ms |
1920 KB |
Output is correct |
3 |
Correct |
6 ms |
1792 KB |
Output is correct |
4 |
Correct |
5 ms |
1792 KB |
Output is correct |
5 |
Correct |
5 ms |
1792 KB |
Output is correct |
6 |
Correct |
5 ms |
1792 KB |
Output is correct |
7 |
Correct |
5 ms |
1792 KB |
Output is correct |
8 |
Correct |
5 ms |
1792 KB |
Output is correct |
9 |
Correct |
5 ms |
1792 KB |
Output is correct |
10 |
Correct |
5 ms |
1792 KB |
Output is correct |
11 |
Correct |
6 ms |
1792 KB |
Output is correct |
12 |
Correct |
6 ms |
1792 KB |
Output is correct |
13 |
Correct |
6 ms |
1792 KB |
Output is correct |
14 |
Correct |
6 ms |
1792 KB |
Output is correct |
15 |
Correct |
6 ms |
1792 KB |
Output is correct |
16 |
Correct |
6 ms |
1792 KB |
Output is correct |
17 |
Correct |
7 ms |
1920 KB |
Output is correct |
18 |
Correct |
6 ms |
1792 KB |
Output is correct |
19 |
Correct |
6 ms |
1792 KB |
Output is correct |
20 |
Correct |
6 ms |
1920 KB |
Output is correct |
21 |
Correct |
6 ms |
1792 KB |
Output is correct |
22 |
Correct |
5 ms |
1792 KB |
Output is correct |
23 |
Correct |
6 ms |
1792 KB |
Output is correct |
24 |
Correct |
6 ms |
1920 KB |
Output is correct |
25 |
Correct |
7 ms |
1920 KB |
Output is correct |
26 |
Correct |
7 ms |
1792 KB |
Output is correct |
27 |
Correct |
6 ms |
1792 KB |
Output is correct |
28 |
Correct |
7 ms |
1792 KB |
Output is correct |
29 |
Correct |
8 ms |
2048 KB |
Output is correct |
30 |
Correct |
7 ms |
1920 KB |
Output is correct |
31 |
Correct |
6 ms |
1920 KB |
Output is correct |
32 |
Correct |
6 ms |
1920 KB |
Output is correct |
33 |
Correct |
10 ms |
2300 KB |
Output is correct |
34 |
Correct |
10 ms |
2300 KB |
Output is correct |
35 |
Correct |
19 ms |
3072 KB |
Output is correct |
36 |
Correct |
7 ms |
2048 KB |
Output is correct |
37 |
Correct |
20 ms |
2816 KB |
Output is correct |
38 |
Correct |
24 ms |
3200 KB |
Output is correct |
39 |
Correct |
24 ms |
3200 KB |
Output is correct |
40 |
Correct |
23 ms |
3328 KB |
Output is correct |
41 |
Correct |
26 ms |
3328 KB |
Output is correct |
42 |
Correct |
10 ms |
1792 KB |
Output is correct |
43 |
Correct |
10 ms |
1792 KB |
Output is correct |
44 |
Correct |
10 ms |
1920 KB |
Output is correct |
45 |
Correct |
23 ms |
3584 KB |
Output is correct |
46 |
Correct |
25 ms |
3584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1792 KB |
Output is correct |
2 |
Correct |
6 ms |
1792 KB |
Output is correct |
3 |
Correct |
6 ms |
1792 KB |
Output is correct |
4 |
Correct |
5 ms |
1792 KB |
Output is correct |
5 |
Correct |
6 ms |
1792 KB |
Output is correct |
6 |
Correct |
6 ms |
1792 KB |
Output is correct |
7 |
Correct |
6 ms |
1792 KB |
Output is correct |
8 |
Correct |
5 ms |
1792 KB |
Output is correct |
9 |
Correct |
6 ms |
1792 KB |
Output is correct |
10 |
Correct |
5 ms |
1792 KB |
Output is correct |
11 |
Correct |
6 ms |
1792 KB |
Output is correct |
12 |
Correct |
6 ms |
1792 KB |
Output is correct |
13 |
Correct |
6 ms |
1792 KB |
Output is correct |
14 |
Correct |
6 ms |
1792 KB |
Output is correct |
15 |
Correct |
6 ms |
1792 KB |
Output is correct |
16 |
Correct |
5 ms |
1792 KB |
Output is correct |
17 |
Correct |
6 ms |
1792 KB |
Output is correct |
18 |
Correct |
6 ms |
1792 KB |
Output is correct |
19 |
Correct |
6 ms |
1792 KB |
Output is correct |
20 |
Correct |
6 ms |
1792 KB |
Output is correct |
21 |
Correct |
6 ms |
1792 KB |
Output is correct |
22 |
Correct |
6 ms |
1792 KB |
Output is correct |
23 |
Correct |
6 ms |
1792 KB |
Output is correct |
24 |
Correct |
6 ms |
1920 KB |
Output is correct |
25 |
Correct |
7 ms |
1920 KB |
Output is correct |
26 |
Correct |
6 ms |
1792 KB |
Output is correct |
27 |
Correct |
6 ms |
1792 KB |
Output is correct |
28 |
Correct |
6 ms |
1792 KB |
Output is correct |
29 |
Correct |
8 ms |
2048 KB |
Output is correct |
30 |
Correct |
6 ms |
1920 KB |
Output is correct |
31 |
Correct |
7 ms |
1920 KB |
Output is correct |
32 |
Correct |
6 ms |
1920 KB |
Output is correct |
33 |
Correct |
10 ms |
2300 KB |
Output is correct |
34 |
Correct |
10 ms |
2300 KB |
Output is correct |
35 |
Correct |
19 ms |
3072 KB |
Output is correct |
36 |
Correct |
7 ms |
2048 KB |
Output is correct |
37 |
Correct |
20 ms |
2816 KB |
Output is correct |
38 |
Correct |
26 ms |
3456 KB |
Output is correct |
39 |
Correct |
24 ms |
3328 KB |
Output is correct |
40 |
Correct |
25 ms |
3328 KB |
Output is correct |
41 |
Correct |
23 ms |
3200 KB |
Output is correct |
42 |
Correct |
10 ms |
1792 KB |
Output is correct |
43 |
Correct |
11 ms |
1792 KB |
Output is correct |
44 |
Correct |
10 ms |
1920 KB |
Output is correct |
45 |
Correct |
23 ms |
3584 KB |
Output is correct |
46 |
Correct |
23 ms |
3584 KB |
Output is correct |
47 |
Correct |
37 ms |
3580 KB |
Output is correct |
48 |
Correct |
13 ms |
2944 KB |
Output is correct |
49 |
Correct |
12 ms |
2816 KB |
Output is correct |
50 |
Correct |
10 ms |
2560 KB |
Output is correct |
51 |
Correct |
40 ms |
3964 KB |
Output is correct |
52 |
Correct |
38 ms |
3964 KB |
Output is correct |
53 |
Correct |
23 ms |
3964 KB |
Output is correct |
54 |
Correct |
7 ms |
2176 KB |
Output is correct |
55 |
Correct |
7 ms |
2176 KB |
Output is correct |
56 |
Correct |
14 ms |
3328 KB |
Output is correct |
57 |
Correct |
30 ms |
2688 KB |
Output is correct |
58 |
Correct |
13 ms |
2176 KB |
Output is correct |
59 |
Correct |
14 ms |
2176 KB |
Output is correct |
60 |
Correct |
14 ms |
2176 KB |
Output is correct |
61 |
Correct |
15 ms |
2304 KB |
Output is correct |
62 |
Correct |
18 ms |
3328 KB |
Output is correct |
63 |
Correct |
59 ms |
5492 KB |
Output is correct |
64 |
Correct |
64 ms |
5492 KB |
Output is correct |
65 |
Correct |
81 ms |
5492 KB |
Output is correct |
66 |
Correct |
147 ms |
7536 KB |
Output is correct |
67 |
Correct |
151 ms |
7784 KB |
Output is correct |