#include <bits/stdc++.h>
#define int long long
#define see(x) cout<<#x<<"="<<x<<endl;
#define endl "\n"
using namespace std;
const int N = 30005;
const int INF = 1e18;
int n, m, b[N], p[N], d[N];
vector <pair <int, int>> g[N];
priority_queue <pair <int, int>> q;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
/*
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
*/
cin >> n >> m;
for (int i = 0; i < m; i ++) {
cin >> b[i] >> p[i];
}
for (int i = 0; i < m; i ++) {
for (int j = 0; j < m; j ++) {
if (i == j) continue;
if (abs(b[i] - b[j]) % p[i] == 0) g[i].push_back({j, abs(b[i] - b[j]) / p[i]});
}
}
for (int i = 0; i < m; i ++) {
d[i] = INF;
}
d[0] = 0;
q.push({0, 0});
while (!q.empty()) {
int v = q.top().second;
q.pop();
for (auto i: g[v]) {
if (d[i.first] > d[v] + i.second) {
d[i.first] = d[v] + i.second;
q.push({-d[i.first], i.first});
}
}
}
if (d[1] == INF) d[1] = -1;
cout << d[1] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1028 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
3 ms |
1236 KB |
Output is correct |
11 |
Correct |
28 ms |
5364 KB |
Output is correct |
12 |
Correct |
83 ms |
64872 KB |
Output is correct |
13 |
Correct |
87 ms |
65220 KB |
Output is correct |
14 |
Correct |
31 ms |
4000 KB |
Output is correct |
15 |
Correct |
29 ms |
3908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 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 |
1032 KB |
Output is correct |
7 |
Correct |
1 ms |
1032 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
3 ms |
1156 KB |
Output is correct |
11 |
Correct |
31 ms |
5256 KB |
Output is correct |
12 |
Correct |
88 ms |
64852 KB |
Output is correct |
13 |
Correct |
82 ms |
65176 KB |
Output is correct |
14 |
Correct |
28 ms |
3936 KB |
Output is correct |
15 |
Correct |
28 ms |
4000 KB |
Output is correct |
16 |
Correct |
5 ms |
1620 KB |
Output is correct |
17 |
Correct |
20 ms |
2268 KB |
Output is correct |
18 |
Correct |
6 ms |
1144 KB |
Output is correct |
19 |
Correct |
3 ms |
980 KB |
Output is correct |
20 |
Correct |
76 ms |
65300 KB |
Output is correct |
21 |
Correct |
8 ms |
1164 KB |
Output is correct |
22 |
Correct |
4 ms |
1108 KB |
Output is correct |
23 |
Correct |
6 ms |
1144 KB |
Output is correct |
24 |
Correct |
22 ms |
1428 KB |
Output is correct |
25 |
Correct |
24 ms |
1340 KB |
Output is correct |
26 |
Correct |
79 ms |
59260 KB |
Output is correct |
27 |
Correct |
79 ms |
61036 KB |
Output is correct |
28 |
Correct |
25 ms |
1516 KB |
Output is correct |
29 |
Correct |
6 ms |
1640 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
3 ms |
1236 KB |
Output is correct |
32 |
Correct |
3 ms |
1108 KB |
Output is correct |
33 |
Correct |
27 ms |
3996 KB |
Output is correct |
34 |
Correct |
25 ms |
3964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1032 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1032 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
1036 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
1036 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
2 ms |
1236 KB |
Output is correct |
11 |
Correct |
30 ms |
5300 KB |
Output is correct |
12 |
Correct |
79 ms |
64840 KB |
Output is correct |
13 |
Correct |
80 ms |
65240 KB |
Output is correct |
14 |
Correct |
26 ms |
3916 KB |
Output is correct |
15 |
Correct |
28 ms |
4036 KB |
Output is correct |
16 |
Correct |
6 ms |
1608 KB |
Output is correct |
17 |
Correct |
21 ms |
2372 KB |
Output is correct |
18 |
Correct |
7 ms |
1116 KB |
Output is correct |
19 |
Correct |
3 ms |
1044 KB |
Output is correct |
20 |
Correct |
81 ms |
65180 KB |
Output is correct |
21 |
Correct |
9 ms |
1236 KB |
Output is correct |
22 |
Correct |
4 ms |
1108 KB |
Output is correct |
23 |
Correct |
6 ms |
1108 KB |
Output is correct |
24 |
Correct |
20 ms |
1420 KB |
Output is correct |
25 |
Correct |
24 ms |
1384 KB |
Output is correct |
26 |
Correct |
80 ms |
59236 KB |
Output is correct |
27 |
Correct |
76 ms |
61068 KB |
Output is correct |
28 |
Correct |
24 ms |
1492 KB |
Output is correct |
29 |
Correct |
5 ms |
1628 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
3 ms |
1160 KB |
Output is correct |
32 |
Correct |
3 ms |
1108 KB |
Output is correct |
33 |
Correct |
29 ms |
3992 KB |
Output is correct |
34 |
Correct |
28 ms |
3948 KB |
Output is correct |
35 |
Execution timed out |
1083 ms |
19536 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
980 KB |
Output is correct |
2 |
Correct |
2 ms |
1028 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1028 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 |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
2 ms |
1236 KB |
Output is correct |
11 |
Correct |
26 ms |
5324 KB |
Output is correct |
12 |
Correct |
84 ms |
64872 KB |
Output is correct |
13 |
Correct |
78 ms |
65184 KB |
Output is correct |
14 |
Correct |
25 ms |
3924 KB |
Output is correct |
15 |
Correct |
28 ms |
3952 KB |
Output is correct |
16 |
Correct |
6 ms |
1620 KB |
Output is correct |
17 |
Correct |
20 ms |
2272 KB |
Output is correct |
18 |
Correct |
7 ms |
1128 KB |
Output is correct |
19 |
Correct |
3 ms |
1036 KB |
Output is correct |
20 |
Correct |
78 ms |
65236 KB |
Output is correct |
21 |
Correct |
8 ms |
1236 KB |
Output is correct |
22 |
Correct |
3 ms |
1108 KB |
Output is correct |
23 |
Correct |
6 ms |
1072 KB |
Output is correct |
24 |
Correct |
20 ms |
1356 KB |
Output is correct |
25 |
Correct |
21 ms |
1364 KB |
Output is correct |
26 |
Correct |
69 ms |
59380 KB |
Output is correct |
27 |
Correct |
72 ms |
60928 KB |
Output is correct |
28 |
Correct |
23 ms |
1484 KB |
Output is correct |
29 |
Correct |
5 ms |
1620 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
31 |
Correct |
3 ms |
1236 KB |
Output is correct |
32 |
Correct |
2 ms |
1160 KB |
Output is correct |
33 |
Correct |
24 ms |
3976 KB |
Output is correct |
34 |
Correct |
23 ms |
3916 KB |
Output is correct |
35 |
Execution timed out |
1087 ms |
21248 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |