#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <vector>
#include <map>
#include <stack>
#include <queue>
using namespace std;
#define ll long long
#define pb push_back
#define el '\n'
#define mpair make_pair
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1)
#define fi first
#define se second
/* Author: Pham Gia Phuoc */
const ll MOD = (ll)(1e9) + 1LL * 19972207;
template <class T1, class T2>
void add(T1 &a, T2 b){
a += b;
if(a >= MOD) a -= MOD;
}
template <class T1, class T2>
void sub(T1 &a, T2 b){
a -= b;
if(a < 0) a += MOD;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if(a > b){a = b; return true;} return false;
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if(a < b){a = b; return true;} return false;
}
/** END OF TEMPLATE. DRINK A CUP OF COFFEE BEFORE READING MY CODE **/
const int MAX = 30010;
const ll INF = (ll) 1e18 + 67LL;
const int oo = (int)(1e9 + 7);
const int NUM_BIT = 62;
int N, M;
vector <int> adj[MAX];
int S, T;
void init(){
cin >> N >> M;
for(int i = 0; i < M; i++){
int a, b; cin >> a >> b;
adj[a].push_back(b);
if(i == 0) S = a;
if(i == 1) T = a;
}
}
int dist[MAX];
bool vis[MAX];
int dijkstra(int s, int t){
memset(dist, 0x3f, sizeof dist);
memset(vis, 0, sizeof vis);
dist[s] = 0;
priority_queue <pair <int, int>, vector <pair <int, int> >, greater <pair <int, int> > > pq;
#define PUSH(u) pq.push(make_pair(dist[u], u))
PUSH(s);
while(!pq.empty()){
int du = pq.top().fi;
int u = pq.top().se;
pq.pop();
if(!vis[u]){
vis[u] = 1;
for(int p:adj[u]){
for(int i = 1; u + i * p < N; i++){
int v = u + i * p;
if(minimize(dist[v], dist[u] + i)) PUSH(v);
}
for(int i = -1; u + i * p >= 0; i--){
int v = u + i * p;
if(minimize(dist[v], dist[u] + abs(i))) PUSH(v);
}
}
}
}
return dist[t];
}
void solve(){
int ans = dijkstra(S, T);
// int ans = 0;
if(ans >= oo) cout << -1;
else cout << ans;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define test "test"
srand(time(0));
int t = 1;
while(t--){
// gen();
init();
solve();
}
return 0;
}
/*** ROAD TO VOI 2025 ***/
Compilation message
skyscraper.cpp: In function 'int dijkstra(int, int)':
skyscraper.cpp:80:13: warning: unused variable 'du' [-Wunused-variable]
80 | int du = pq.top().fi;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1172 KB |
Output is correct |
5 |
Correct |
1 ms |
1164 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1112 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 |
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 |
1112 KB |
Output is correct |
14 |
Correct |
1 ms |
1112 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 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 |
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 |
2 ms |
1272 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1184 KB |
Output is correct |
20 |
Correct |
3 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1112 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
1372 KB |
Output is correct |
26 |
Correct |
3 ms |
1112 KB |
Output is correct |
27 |
Correct |
3 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1368 KB |
Output is correct |
31 |
Correct |
1 ms |
1624 KB |
Output is correct |
32 |
Correct |
1 ms |
1624 KB |
Output is correct |
33 |
Correct |
2 ms |
1372 KB |
Output is correct |
34 |
Correct |
2 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 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 |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1168 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 |
1368 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1112 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1112 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
4 ms |
1372 KB |
Output is correct |
21 |
Correct |
2 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 |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
4 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1368 KB |
Output is correct |
31 |
Correct |
1 ms |
1368 KB |
Output is correct |
32 |
Correct |
1 ms |
1372 KB |
Output is correct |
33 |
Correct |
2 ms |
1556 KB |
Output is correct |
34 |
Correct |
2 ms |
1372 KB |
Output is correct |
35 |
Correct |
4 ms |
1780 KB |
Output is correct |
36 |
Correct |
1 ms |
1372 KB |
Output is correct |
37 |
Correct |
4 ms |
1692 KB |
Output is correct |
38 |
Correct |
5 ms |
1880 KB |
Output is correct |
39 |
Correct |
7 ms |
1884 KB |
Output is correct |
40 |
Correct |
6 ms |
1692 KB |
Output is correct |
41 |
Correct |
10 ms |
1884 KB |
Output is correct |
42 |
Correct |
51 ms |
1796 KB |
Output is correct |
43 |
Correct |
42 ms |
1628 KB |
Output is correct |
44 |
Correct |
42 ms |
1628 KB |
Output is correct |
45 |
Correct |
5 ms |
1812 KB |
Output is correct |
46 |
Correct |
5 ms |
1880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1120 KB |
Output is correct |
6 |
Correct |
1 ms |
1172 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
2 ms |
1120 KB |
Output is correct |
10 |
Correct |
1 ms |
1120 KB |
Output is correct |
11 |
Correct |
1 ms |
1128 KB |
Output is correct |
12 |
Correct |
1 ms |
1120 KB |
Output is correct |
13 |
Correct |
1 ms |
1124 KB |
Output is correct |
14 |
Correct |
1 ms |
1172 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 |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1176 KB |
Output is correct |
20 |
Correct |
4 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
3 ms |
1360 KB |
Output is correct |
27 |
Correct |
3 ms |
1112 KB |
Output is correct |
28 |
Correct |
1 ms |
1368 KB |
Output is correct |
29 |
Correct |
2 ms |
1380 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 |
1416 KB |
Output is correct |
33 |
Correct |
3 ms |
1448 KB |
Output is correct |
34 |
Correct |
2 ms |
1376 KB |
Output is correct |
35 |
Correct |
4 ms |
1640 KB |
Output is correct |
36 |
Correct |
1 ms |
1248 KB |
Output is correct |
37 |
Correct |
4 ms |
1628 KB |
Output is correct |
38 |
Correct |
6 ms |
1884 KB |
Output is correct |
39 |
Correct |
6 ms |
1916 KB |
Output is correct |
40 |
Correct |
6 ms |
1696 KB |
Output is correct |
41 |
Correct |
5 ms |
2136 KB |
Output is correct |
42 |
Correct |
43 ms |
1628 KB |
Output is correct |
43 |
Correct |
42 ms |
1628 KB |
Output is correct |
44 |
Correct |
42 ms |
1628 KB |
Output is correct |
45 |
Correct |
5 ms |
1880 KB |
Output is correct |
46 |
Correct |
5 ms |
1884 KB |
Output is correct |
47 |
Correct |
11 ms |
2324 KB |
Output is correct |
48 |
Correct |
4 ms |
1956 KB |
Output is correct |
49 |
Correct |
4 ms |
1836 KB |
Output is correct |
50 |
Correct |
4 ms |
1628 KB |
Output is correct |
51 |
Correct |
23 ms |
3396 KB |
Output is correct |
52 |
Correct |
17 ms |
2776 KB |
Output is correct |
53 |
Correct |
9 ms |
2776 KB |
Output is correct |
54 |
Correct |
2 ms |
1372 KB |
Output is correct |
55 |
Correct |
2 ms |
1372 KB |
Output is correct |
56 |
Correct |
614 ms |
2780 KB |
Output is correct |
57 |
Correct |
33 ms |
3532 KB |
Output is correct |
58 |
Correct |
595 ms |
1624 KB |
Output is correct |
59 |
Correct |
593 ms |
1884 KB |
Output is correct |
60 |
Correct |
582 ms |
1884 KB |
Output is correct |
61 |
Correct |
508 ms |
1880 KB |
Output is correct |
62 |
Correct |
9 ms |
2392 KB |
Output is correct |
63 |
Correct |
19 ms |
2776 KB |
Output is correct |
64 |
Correct |
18 ms |
3792 KB |
Output is correct |
65 |
Correct |
23 ms |
3800 KB |
Output is correct |
66 |
Correct |
37 ms |
3792 KB |
Output is correct |
67 |
Correct |
34 ms |
3788 KB |
Output is correct |