#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
int N, M, B[30005], P[30005];
vector<pii> adj[30005];
int mindis[30005];
set<int> dog[30005];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin>>N>>M;
for(int i=0;i<M;i++) {
cin>>B[i]>>P[i];
dog[B[i]].insert(-P[i]);
}
for(int i=0;i<N;i++) {
vector<bool> done(N, 0);
for(int pp:dog[i]) {
int p = -pp;
for(int j=1;i-(j*p)>=0;j++) if(!done[i-(j*p)]) {adj[i].push_back({i-(j*p), j}); done[i-(j*p)]=1;}
for(int j=1;i+(j*p)<N;j++) if(!done[i+(j*p)]) {adj[i].push_back({i+(j*p), j}); done[i+(j*p)]=1;}
}
mindis[i] = 2e9;
}
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0, B[0]});
mindis[B[0]] = 0;
while(!pq.empty()) {
auto [dis, u] = pq.top();
pq.pop();
if(mindis[u]!=dis) continue;
for(auto [v, w]:adj[u]) {
if(mindis[v]>dis+w) {
mindis[v] = dis+w;
pq.push({mindis[v], v});
}
}
}
if(mindis[B[1]]==2e9) cout<<-1;
else cout<<mindis[B[1]];
}
/*
5 3
0 2
1 1
4 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2680 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
55 ms |
34704 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
27 |
Correct |
1 ms |
2776 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
3160 KB |
Output is correct |
30 |
Correct |
1 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2904 KB |
Output is correct |
33 |
Correct |
3 ms |
3420 KB |
Output is correct |
34 |
Correct |
3 ms |
3420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2760 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
56 ms |
34852 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2904 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
3160 KB |
Output is correct |
30 |
Correct |
1 ms |
2904 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
3 ms |
3420 KB |
Output is correct |
34 |
Correct |
3 ms |
3420 KB |
Output is correct |
35 |
Correct |
10 ms |
5468 KB |
Output is correct |
36 |
Correct |
2 ms |
3164 KB |
Output is correct |
37 |
Correct |
10 ms |
6544 KB |
Output is correct |
38 |
Correct |
13 ms |
6492 KB |
Output is correct |
39 |
Correct |
13 ms |
6748 KB |
Output is correct |
40 |
Correct |
12 ms |
6696 KB |
Output is correct |
41 |
Correct |
12 ms |
6492 KB |
Output is correct |
42 |
Correct |
4 ms |
2908 KB |
Output is correct |
43 |
Correct |
4 ms |
3000 KB |
Output is correct |
44 |
Correct |
57 ms |
34872 KB |
Output is correct |
45 |
Correct |
13 ms |
6488 KB |
Output is correct |
46 |
Correct |
13 ms |
6580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2904 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2908 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
55 ms |
34824 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
3164 KB |
Output is correct |
30 |
Correct |
2 ms |
2904 KB |
Output is correct |
31 |
Correct |
2 ms |
3160 KB |
Output is correct |
32 |
Correct |
1 ms |
2908 KB |
Output is correct |
33 |
Correct |
3 ms |
3420 KB |
Output is correct |
34 |
Correct |
3 ms |
3420 KB |
Output is correct |
35 |
Correct |
10 ms |
5388 KB |
Output is correct |
36 |
Correct |
2 ms |
3164 KB |
Output is correct |
37 |
Correct |
10 ms |
6708 KB |
Output is correct |
38 |
Correct |
13 ms |
6488 KB |
Output is correct |
39 |
Correct |
13 ms |
6748 KB |
Output is correct |
40 |
Correct |
12 ms |
6748 KB |
Output is correct |
41 |
Correct |
12 ms |
6492 KB |
Output is correct |
42 |
Correct |
4 ms |
2904 KB |
Output is correct |
43 |
Correct |
4 ms |
2908 KB |
Output is correct |
44 |
Correct |
58 ms |
34900 KB |
Output is correct |
45 |
Correct |
17 ms |
6488 KB |
Output is correct |
46 |
Correct |
15 ms |
6748 KB |
Output is correct |
47 |
Correct |
29 ms |
14968 KB |
Output is correct |
48 |
Correct |
11 ms |
6104 KB |
Output is correct |
49 |
Correct |
10 ms |
5176 KB |
Output is correct |
50 |
Correct |
8 ms |
4952 KB |
Output is correct |
51 |
Correct |
28 ms |
8936 KB |
Output is correct |
52 |
Correct |
28 ms |
9308 KB |
Output is correct |
53 |
Correct |
15 ms |
5848 KB |
Output is correct |
54 |
Correct |
4 ms |
3164 KB |
Output is correct |
55 |
Correct |
5 ms |
3544 KB |
Output is correct |
56 |
Runtime error |
411 ms |
262144 KB |
Execution killed with signal 9 |
57 |
Halted |
0 ms |
0 KB |
- |