#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), d2 = cal(e);
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 |
1160 KB |
Output is correct |
6 |
Correct |
1 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 |
1160 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1164 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1164 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 |
2 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1112 KB |
Output is correct |
3 |
Correct |
1 ms |
1264 KB |
Output is correct |
4 |
Correct |
1 ms |
1524 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1164 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 |
1068 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 |
1160 KB |
Output is correct |
20 |
Correct |
8 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 |
1112 KB |
Output is correct |
24 |
Correct |
2 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
1116 KB |
Output is correct |
26 |
Correct |
8 ms |
1116 KB |
Output is correct |
27 |
Correct |
7 ms |
1236 KB |
Output is correct |
28 |
Correct |
2 ms |
1500 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 |
1420 KB |
Output is correct |
32 |
Correct |
2 ms |
1372 KB |
Output is correct |
33 |
Correct |
3 ms |
1496 KB |
Output is correct |
34 |
Correct |
3 ms |
1500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1160 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1172 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 |
1368 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 |
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 |
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 |
1120 KB |
Output is correct |
20 |
Correct |
8 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1120 KB |
Output is correct |
22 |
Correct |
1 ms |
1120 KB |
Output is correct |
23 |
Correct |
1 ms |
1124 KB |
Output is correct |
24 |
Correct |
2 ms |
1128 KB |
Output is correct |
25 |
Correct |
2 ms |
1124 KB |
Output is correct |
26 |
Correct |
10 ms |
1364 KB |
Output is correct |
27 |
Correct |
9 ms |
1116 KB |
Output is correct |
28 |
Correct |
2 ms |
1628 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1168 KB |
Output is correct |
31 |
Correct |
2 ms |
1368 KB |
Output is correct |
32 |
Correct |
2 ms |
1372 KB |
Output is correct |
33 |
Correct |
3 ms |
1496 KB |
Output is correct |
34 |
Correct |
3 ms |
1500 KB |
Output is correct |
35 |
Correct |
5 ms |
1628 KB |
Output is correct |
36 |
Correct |
2 ms |
1116 KB |
Output is correct |
37 |
Correct |
6 ms |
1692 KB |
Output is correct |
38 |
Correct |
7 ms |
1744 KB |
Output is correct |
39 |
Correct |
7 ms |
1756 KB |
Output is correct |
40 |
Correct |
8 ms |
1880 KB |
Output is correct |
41 |
Correct |
11 ms |
1828 KB |
Output is correct |
42 |
Correct |
103 ms |
1660 KB |
Output is correct |
43 |
Correct |
146 ms |
1600 KB |
Output is correct |
44 |
Correct |
100 ms |
1628 KB |
Output is correct |
45 |
Correct |
7 ms |
1884 KB |
Output is correct |
46 |
Correct |
9 ms |
1772 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 |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
940 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
2 ms |
1172 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1168 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 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 |
10 ms |
1324 KB |
Output is correct |
21 |
Correct |
1 ms |
1180 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 |
1368 KB |
Output is correct |
26 |
Correct |
8 ms |
1116 KB |
Output is correct |
27 |
Correct |
7 ms |
1124 KB |
Output is correct |
28 |
Correct |
3 ms |
1640 KB |
Output is correct |
29 |
Correct |
2 ms |
1384 KB |
Output is correct |
30 |
Correct |
2 ms |
1296 KB |
Output is correct |
31 |
Correct |
2 ms |
1384 KB |
Output is correct |
32 |
Correct |
2 ms |
1172 KB |
Output is correct |
33 |
Correct |
3 ms |
1512 KB |
Output is correct |
34 |
Correct |
3 ms |
1512 KB |
Output is correct |
35 |
Correct |
6 ms |
1640 KB |
Output is correct |
36 |
Correct |
2 ms |
1244 KB |
Output is correct |
37 |
Correct |
6 ms |
1640 KB |
Output is correct |
38 |
Correct |
7 ms |
1896 KB |
Output is correct |
39 |
Correct |
7 ms |
1820 KB |
Output is correct |
40 |
Correct |
7 ms |
1896 KB |
Output is correct |
41 |
Correct |
9 ms |
1896 KB |
Output is correct |
42 |
Correct |
126 ms |
1676 KB |
Output is correct |
43 |
Correct |
113 ms |
1640 KB |
Output is correct |
44 |
Correct |
114 ms |
1632 KB |
Output is correct |
45 |
Correct |
8 ms |
1808 KB |
Output is correct |
46 |
Correct |
7 ms |
1884 KB |
Output is correct |
47 |
Correct |
21 ms |
3336 KB |
Output is correct |
48 |
Correct |
13 ms |
3284 KB |
Output is correct |
49 |
Correct |
11 ms |
2784 KB |
Output is correct |
50 |
Correct |
3 ms |
1884 KB |
Output is correct |
51 |
Correct |
33 ms |
4284 KB |
Output is correct |
52 |
Correct |
37 ms |
5536 KB |
Output is correct |
53 |
Correct |
16 ms |
4220 KB |
Output is correct |
54 |
Correct |
3 ms |
2036 KB |
Output is correct |
55 |
Correct |
6 ms |
2676 KB |
Output is correct |
56 |
Execution timed out |
1037 ms |
3708 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |