#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
int dp[30001];
vector<int> G[30001];
int A[30001][2];
bool ok[30001][800][2];
int main(){
int n, k;
cin >> n >> k;
for(int i = 0; i < k; i++){
cin >> A[i][0] >> A[i][1];
G[A[i][0]].push_back(A[i][1]);
}
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> q;
q.push({0, A[0][0], 1 << 30});
fill(dp, dp + n, 1 << 30);
while(q.size()){
tuple<int, int, int> qe = q.top(); q.pop();
int w = get<0>(qe), v = get<1>(qe), ban = get<2>(qe);
if(dp[v] != (1 << 30)) continue;
dp[v] = w;
// cout << v << ' ' << w << endl;
for(auto i : G[v]){
if(ban == i) continue;
for(int j = v % i; j < n; j += i){
int d = abs(v - j) / i;
if(dp[j] > w + d) q.push({w + d, j, i});
}
}
}
cout << (dp[A[1][0]] == (1 << 30) ? -1 : dp[A[1][0]]) << endl;
}
# |
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 |
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 |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
7 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 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 |
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 |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
7 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
3 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
2 ms |
1108 KB |
Output is correct |
21 |
Correct |
2 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
2 ms |
980 KB |
Output is correct |
24 |
Correct |
4 ms |
1108 KB |
Output is correct |
25 |
Correct |
3 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
980 KB |
Output is correct |
27 |
Correct |
85 ms |
7304 KB |
Output is correct |
28 |
Correct |
2 ms |
980 KB |
Output is correct |
29 |
Correct |
11 ms |
1872 KB |
Output is correct |
30 |
Correct |
3 ms |
1300 KB |
Output is correct |
31 |
Correct |
5 ms |
1560 KB |
Output is correct |
32 |
Correct |
3 ms |
1272 KB |
Output is correct |
33 |
Correct |
22 ms |
2636 KB |
Output is correct |
34 |
Correct |
27 ms |
2636 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 |
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 |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
7 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
3 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
2 ms |
980 KB |
Output is correct |
20 |
Correct |
2 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
2 ms |
980 KB |
Output is correct |
24 |
Correct |
4 ms |
1108 KB |
Output is correct |
25 |
Correct |
2 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
980 KB |
Output is correct |
27 |
Correct |
83 ms |
7244 KB |
Output is correct |
28 |
Correct |
2 ms |
980 KB |
Output is correct |
29 |
Correct |
15 ms |
1872 KB |
Output is correct |
30 |
Correct |
3 ms |
1300 KB |
Output is correct |
31 |
Correct |
5 ms |
1560 KB |
Output is correct |
32 |
Correct |
3 ms |
1236 KB |
Output is correct |
33 |
Correct |
28 ms |
2636 KB |
Output is correct |
34 |
Correct |
24 ms |
2636 KB |
Output is correct |
35 |
Correct |
25 ms |
2256 KB |
Output is correct |
36 |
Correct |
4 ms |
1236 KB |
Output is correct |
37 |
Correct |
38 ms |
4444 KB |
Output is correct |
38 |
Correct |
38 ms |
3080 KB |
Output is correct |
39 |
Correct |
38 ms |
3172 KB |
Output is correct |
40 |
Correct |
41 ms |
3164 KB |
Output is correct |
41 |
Correct |
41 ms |
3152 KB |
Output is correct |
42 |
Correct |
13 ms |
1492 KB |
Output is correct |
43 |
Execution timed out |
1092 ms |
99992 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
7 ms |
1876 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1144 KB |
Output is correct |
16 |
Correct |
2 ms |
980 KB |
Output is correct |
17 |
Correct |
4 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
2 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
2 ms |
980 KB |
Output is correct |
24 |
Correct |
3 ms |
1108 KB |
Output is correct |
25 |
Correct |
2 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
980 KB |
Output is correct |
27 |
Correct |
82 ms |
7232 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
12 ms |
1872 KB |
Output is correct |
30 |
Correct |
3 ms |
1300 KB |
Output is correct |
31 |
Correct |
5 ms |
1560 KB |
Output is correct |
32 |
Correct |
3 ms |
1236 KB |
Output is correct |
33 |
Correct |
29 ms |
2636 KB |
Output is correct |
34 |
Correct |
22 ms |
2636 KB |
Output is correct |
35 |
Correct |
25 ms |
2256 KB |
Output is correct |
36 |
Correct |
4 ms |
1236 KB |
Output is correct |
37 |
Correct |
38 ms |
4544 KB |
Output is correct |
38 |
Correct |
37 ms |
3148 KB |
Output is correct |
39 |
Correct |
40 ms |
3040 KB |
Output is correct |
40 |
Correct |
38 ms |
3100 KB |
Output is correct |
41 |
Correct |
36 ms |
3180 KB |
Output is correct |
42 |
Correct |
13 ms |
1492 KB |
Output is correct |
43 |
Execution timed out |
1098 ms |
99920 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |