#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 3e4 + 10;
vector <int> save[N];
int n;
vector <ll> cal(int s) {
vector <ll> dis;
dis.assign(n, 1e12);
dis[s] = 0;
priority_queue <pair <ll, ll>, vector <pair <ll, ll>>, greater <pair <ll, ll>>> pq;
pq.push({0, s});
while (sz(pq)) {
auto t = pq.top();
pq.pop();
if (dis[t.s] < t.f)
continue;
for (auto x : save[t.s]) {
int cnt = 0;
for (int i = t.s; i >= 0; i -= x) {
if (dis[i] > t.f + cnt) {
dis[i] = t.f + cnt;
pq.push({dis[i], i});
}
cnt++;
}
cnt = 0;
for (int i = t.s; i < n; i += x) {
if (dis[i] > t.f + cnt) {
dis[i] = t.f + cnt;
pq.push({dis[i], i});
}
cnt++;
}
}
}
return dis;
}
ll solve() {
int m;
cin >> n >> m;
int s = 0, e = 0;
for (int i = 0; i < m; i++) {
int p, d;
cin >> p >> d;
save[p].pb(d);
if (i == 0) {
s = p;
}
if (i == 1) {
e = p;
}
}
vector <ll> d1 = cal(s);
ll ans = d1[e];
//for (int i = 0; i < n; i++) {
//ans = min(ans, d1[i] + d2[i]);
//}
if (ans > 1e9)
ans = -1;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << solve() << '\n';
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 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1368 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1112 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1112 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 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 |
1112 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 |
1112 KB |
Output is correct |
11 |
Correct |
1 ms |
1112 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 |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1112 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 |
5 ms |
1116 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 |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
4 ms |
1116 KB |
Output is correct |
27 |
Correct |
4 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 |
1 ms |
1368 KB |
Output is correct |
33 |
Correct |
2 ms |
1500 KB |
Output is correct |
34 |
Correct |
2 ms |
1500 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 |
1312 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 |
1112 KB |
Output is correct |
11 |
Correct |
1 ms |
1112 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 |
1112 KB |
Output is correct |
20 |
Correct |
4 ms |
1116 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 |
1232 KB |
Output is correct |
24 |
Correct |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
4 ms |
1112 KB |
Output is correct |
27 |
Correct |
4 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 |
2 ms |
1368 KB |
Output is correct |
32 |
Correct |
1 ms |
1372 KB |
Output is correct |
33 |
Correct |
2 ms |
1500 KB |
Output is correct |
34 |
Correct |
3 ms |
1500 KB |
Output is correct |
35 |
Correct |
5 ms |
1372 KB |
Output is correct |
36 |
Correct |
2 ms |
1116 KB |
Output is correct |
37 |
Correct |
4 ms |
1372 KB |
Output is correct |
38 |
Correct |
6 ms |
1540 KB |
Output is correct |
39 |
Correct |
6 ms |
1372 KB |
Output is correct |
40 |
Correct |
6 ms |
1628 KB |
Output is correct |
41 |
Correct |
6 ms |
1628 KB |
Output is correct |
42 |
Correct |
51 ms |
1280 KB |
Output is correct |
43 |
Correct |
51 ms |
1368 KB |
Output is correct |
44 |
Correct |
52 ms |
1372 KB |
Output is correct |
45 |
Correct |
6 ms |
1624 KB |
Output is correct |
46 |
Correct |
5 ms |
1624 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 |
1112 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 |
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 |
1116 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 |
1 ms |
1112 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
4 ms |
1116 KB |
Output is correct |
27 |
Correct |
4 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 |
1 ms |
1372 KB |
Output is correct |
33 |
Correct |
2 ms |
1500 KB |
Output is correct |
34 |
Correct |
2 ms |
1500 KB |
Output is correct |
35 |
Correct |
5 ms |
1372 KB |
Output is correct |
36 |
Correct |
2 ms |
1116 KB |
Output is correct |
37 |
Correct |
4 ms |
1516 KB |
Output is correct |
38 |
Correct |
6 ms |
1628 KB |
Output is correct |
39 |
Correct |
6 ms |
1372 KB |
Output is correct |
40 |
Correct |
6 ms |
1628 KB |
Output is correct |
41 |
Correct |
6 ms |
1628 KB |
Output is correct |
42 |
Correct |
63 ms |
1372 KB |
Output is correct |
43 |
Correct |
52 ms |
1372 KB |
Output is correct |
44 |
Correct |
51 ms |
1372 KB |
Output is correct |
45 |
Correct |
5 ms |
1624 KB |
Output is correct |
46 |
Correct |
6 ms |
1628 KB |
Output is correct |
47 |
Correct |
13 ms |
2772 KB |
Output is correct |
48 |
Correct |
4 ms |
1628 KB |
Output is correct |
49 |
Correct |
4 ms |
1628 KB |
Output is correct |
50 |
Correct |
3 ms |
1628 KB |
Output is correct |
51 |
Correct |
20 ms |
4004 KB |
Output is correct |
52 |
Correct |
19 ms |
3028 KB |
Output is correct |
53 |
Correct |
11 ms |
3024 KB |
Output is correct |
54 |
Correct |
2 ms |
1756 KB |
Output is correct |
55 |
Correct |
2 ms |
1760 KB |
Output is correct |
56 |
Correct |
725 ms |
2784 KB |
Output is correct |
57 |
Correct |
39 ms |
6604 KB |
Output is correct |
58 |
Correct |
762 ms |
2004 KB |
Output is correct |
59 |
Correct |
720 ms |
2136 KB |
Output is correct |
60 |
Correct |
726 ms |
2064 KB |
Output is correct |
61 |
Correct |
649 ms |
2140 KB |
Output is correct |
62 |
Correct |
7 ms |
2652 KB |
Output is correct |
63 |
Correct |
30 ms |
4052 KB |
Output is correct |
64 |
Correct |
28 ms |
7116 KB |
Output is correct |
65 |
Correct |
33 ms |
7636 KB |
Output is correct |
66 |
Correct |
50 ms |
6660 KB |
Output is correct |
67 |
Correct |
54 ms |
6092 KB |
Output is correct |