#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mk make_pair
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll inf = 1e18 + 50;
const int N = 30050, SQ = 210;
ll n, m, b[N], p[N], dis[N];
bool mark[SQ][N];
vector<pll> adj[N];
set<pll> st;
void solve() {
cin >> n >> m;
for (int x = 0; x < n; x++) {
dis[x] = inf;
}
for (int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
if (p[i] < SQ) mark[p[i]][b[i]] = 1;
else for (int x = b[i]%p[i]; x < n; x += p[i]) {
adj[b[i]].push_back({x, abs(b[i]-x)/p[i]});
}
}
for (int d = 1; d < SQ; d++) {
for (int x = 0; x < n; x++) {
if (!mark[d][x]) continue;
for (int y = x + d; y < n; y += d) {
adj[x].push_back({y, abs(x-y)/d});
if (mark[d][y]) break;
}
for (int y = x - d; y >= 0; y -= d) {
adj[x].push_back({y, abs(x-y)/d});
if (mark[d][y]) break;
}
}
}
dis[b[0]] = 0;
st.insert({dis[b[0]], b[0]});
while (st.size()) {
ll u = st.begin()->S;
st.erase(st.begin());;
for (pll x: adj[u]) {
ll v = x.F, w = x.S;
if (dis[u] + w < dis[v]) {
st.erase({dis[v], v});
dis[v] = dis[u] + w;
st.insert({dis[v], v});
}
}
}
cout << (dis[b[1]] >= inf? -1: dis[b[1]]) << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
1044 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
2 ms |
1464 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1364 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
2 ms |
1364 KB |
Output is correct |
15 |
Correct |
2 ms |
1364 KB |
Output is correct |
16 |
Correct |
2 ms |
1940 KB |
Output is correct |
17 |
Correct |
3 ms |
2260 KB |
Output is correct |
18 |
Correct |
3 ms |
1572 KB |
Output is correct |
19 |
Correct |
2 ms |
1364 KB |
Output is correct |
20 |
Correct |
2 ms |
1108 KB |
Output is correct |
21 |
Correct |
2 ms |
1492 KB |
Output is correct |
22 |
Correct |
2 ms |
1364 KB |
Output is correct |
23 |
Correct |
3 ms |
1492 KB |
Output is correct |
24 |
Correct |
4 ms |
2004 KB |
Output is correct |
25 |
Correct |
3 ms |
1364 KB |
Output is correct |
26 |
Correct |
3 ms |
1748 KB |
Output is correct |
27 |
Correct |
2 ms |
1364 KB |
Output is correct |
28 |
Correct |
3 ms |
2020 KB |
Output is correct |
29 |
Correct |
7 ms |
2544 KB |
Output is correct |
30 |
Correct |
4 ms |
1640 KB |
Output is correct |
31 |
Correct |
4 ms |
1876 KB |
Output is correct |
32 |
Correct |
3 ms |
1620 KB |
Output is correct |
33 |
Correct |
9 ms |
3668 KB |
Output is correct |
34 |
Correct |
12 ms |
3668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1492 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1876 KB |
Output is correct |
17 |
Correct |
2 ms |
2260 KB |
Output is correct |
18 |
Correct |
2 ms |
1588 KB |
Output is correct |
19 |
Correct |
2 ms |
1364 KB |
Output is correct |
20 |
Correct |
2 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
1492 KB |
Output is correct |
22 |
Correct |
2 ms |
1364 KB |
Output is correct |
23 |
Correct |
3 ms |
1492 KB |
Output is correct |
24 |
Correct |
4 ms |
2004 KB |
Output is correct |
25 |
Correct |
4 ms |
1364 KB |
Output is correct |
26 |
Correct |
3 ms |
1748 KB |
Output is correct |
27 |
Correct |
4 ms |
1364 KB |
Output is correct |
28 |
Correct |
3 ms |
2004 KB |
Output is correct |
29 |
Correct |
6 ms |
2556 KB |
Output is correct |
30 |
Correct |
4 ms |
1620 KB |
Output is correct |
31 |
Correct |
4 ms |
1876 KB |
Output is correct |
32 |
Correct |
4 ms |
1620 KB |
Output is correct |
33 |
Correct |
9 ms |
3668 KB |
Output is correct |
34 |
Correct |
9 ms |
3640 KB |
Output is correct |
35 |
Correct |
10 ms |
5204 KB |
Output is correct |
36 |
Correct |
4 ms |
2388 KB |
Output is correct |
37 |
Correct |
11 ms |
7252 KB |
Output is correct |
38 |
Correct |
12 ms |
7060 KB |
Output is correct |
39 |
Correct |
12 ms |
7124 KB |
Output is correct |
40 |
Correct |
12 ms |
6996 KB |
Output is correct |
41 |
Correct |
16 ms |
7124 KB |
Output is correct |
42 |
Correct |
6 ms |
2132 KB |
Output is correct |
43 |
Correct |
6 ms |
1876 KB |
Output is correct |
44 |
Correct |
5 ms |
1588 KB |
Output is correct |
45 |
Correct |
18 ms |
11604 KB |
Output is correct |
46 |
Correct |
21 ms |
11468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1492 KB |
Output is correct |
11 |
Correct |
2 ms |
1492 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1876 KB |
Output is correct |
17 |
Correct |
2 ms |
2260 KB |
Output is correct |
18 |
Correct |
2 ms |
1492 KB |
Output is correct |
19 |
Correct |
2 ms |
1364 KB |
Output is correct |
20 |
Correct |
2 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
1492 KB |
Output is correct |
22 |
Correct |
2 ms |
1364 KB |
Output is correct |
23 |
Correct |
2 ms |
1492 KB |
Output is correct |
24 |
Correct |
4 ms |
1968 KB |
Output is correct |
25 |
Correct |
3 ms |
1364 KB |
Output is correct |
26 |
Correct |
3 ms |
1748 KB |
Output is correct |
27 |
Correct |
2 ms |
1364 KB |
Output is correct |
28 |
Correct |
4 ms |
2004 KB |
Output is correct |
29 |
Correct |
7 ms |
2540 KB |
Output is correct |
30 |
Correct |
5 ms |
1620 KB |
Output is correct |
31 |
Correct |
4 ms |
1876 KB |
Output is correct |
32 |
Correct |
4 ms |
1684 KB |
Output is correct |
33 |
Correct |
9 ms |
3696 KB |
Output is correct |
34 |
Correct |
9 ms |
3668 KB |
Output is correct |
35 |
Correct |
9 ms |
5204 KB |
Output is correct |
36 |
Correct |
3 ms |
2388 KB |
Output is correct |
37 |
Correct |
11 ms |
7252 KB |
Output is correct |
38 |
Correct |
12 ms |
6996 KB |
Output is correct |
39 |
Correct |
12 ms |
7124 KB |
Output is correct |
40 |
Correct |
12 ms |
7028 KB |
Output is correct |
41 |
Correct |
12 ms |
7084 KB |
Output is correct |
42 |
Correct |
6 ms |
2132 KB |
Output is correct |
43 |
Correct |
5 ms |
1876 KB |
Output is correct |
44 |
Correct |
5 ms |
1620 KB |
Output is correct |
45 |
Correct |
18 ms |
11608 KB |
Output is correct |
46 |
Correct |
18 ms |
11468 KB |
Output is correct |
47 |
Correct |
57 ms |
22064 KB |
Output is correct |
48 |
Correct |
24 ms |
7044 KB |
Output is correct |
49 |
Correct |
23 ms |
5208 KB |
Output is correct |
50 |
Correct |
21 ms |
4944 KB |
Output is correct |
51 |
Correct |
73 ms |
11064 KB |
Output is correct |
52 |
Correct |
68 ms |
11576 KB |
Output is correct |
53 |
Correct |
40 ms |
5540 KB |
Output is correct |
54 |
Correct |
21 ms |
2632 KB |
Output is correct |
55 |
Correct |
23 ms |
3532 KB |
Output is correct |
56 |
Correct |
25 ms |
3196 KB |
Output is correct |
57 |
Correct |
74 ms |
18384 KB |
Output is correct |
58 |
Correct |
29 ms |
4216 KB |
Output is correct |
59 |
Correct |
29 ms |
4956 KB |
Output is correct |
60 |
Correct |
31 ms |
6784 KB |
Output is correct |
61 |
Correct |
29 ms |
4816 KB |
Output is correct |
62 |
Correct |
38 ms |
12432 KB |
Output is correct |
63 |
Correct |
119 ms |
49204 KB |
Output is correct |
64 |
Correct |
132 ms |
56648 KB |
Output is correct |
65 |
Correct |
174 ms |
83636 KB |
Output is correct |
66 |
Correct |
271 ms |
136836 KB |
Output is correct |
67 |
Correct |
272 ms |
136448 KB |
Output is correct |