/**------------------------------------------
---------Author: BePhuong--------------------
---------From: TK4-CHT ----------------------
---------Training To Win Voi 25 !!! ---------
---------------------------------------------
--------------------PhamVuPhuong2008<BeL>-**/
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define int long long
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
#define iii pair<int,ii>
#define iv pair<ii, ii>
#define base 341
#define MASK(i) (1ll << i)
#define oo 1e18
#define isOn(x,i) ((x) & MASK(i))
#define bitOn(x,i) ((x) | MASK(i))
#define bitOff(x,i) ((x) & ~MASK(i))
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
using namespace std;
//using namespace __gnu_pbds;
const int maxn = 5e4 + 5;
int n,m, dist[maxn], b[maxn];
vector<int> a[maxn];
void djk(int st) {
memset(dist, 0x3f, sizeof dist);
priority_queue<ii, vector<ii>, greater<ii>> q;
dist[st] = 0;
q.push({0, st});
while(!q.empty()) {
int cost = q.top().fi, z = q.top().se;
q.pop();
if (cost != dist[z]) continue;
for (int cost : a[z]) {
for (int i = 1; z + i*cost < n; i++) {
if (dist[z + i*cost] > dist[z] + i) {
dist[z + i*cost] = dist[z] + i;
q.push({dist[z + i*cost], z + i*cost});
}
}
for (int i = 1; z - i*cost >= 0; i++) {
if (dist[z - i*cost] > dist[z] + i) {
dist[z - i*cost] = dist[z] + i;
q.push({dist[z - i*cost], z - i*cost});
}
}
}
}
}
main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> b[i];
int x; cin >> x;
a[b[i]].pb(x);
}
// for (int i = 0; i < m; i++) cout << b[i] << endl;
djk(b[0]);
if (dist[b[1]] >= 1e15) cout << "-1";
else cout << dist[b[1]];
}
Compilation message
skyscraper.cpp:57:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
57 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1872 KB |
Output is correct |
2 |
Correct |
2 ms |
1872 KB |
Output is correct |
3 |
Correct |
3 ms |
1872 KB |
Output is correct |
4 |
Correct |
2 ms |
1872 KB |
Output is correct |
5 |
Correct |
2 ms |
1872 KB |
Output is correct |
6 |
Correct |
2 ms |
1872 KB |
Output is correct |
7 |
Correct |
1 ms |
1872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1872 KB |
Output is correct |
2 |
Correct |
3 ms |
1872 KB |
Output is correct |
3 |
Correct |
2 ms |
2044 KB |
Output is correct |
4 |
Correct |
2 ms |
1872 KB |
Output is correct |
5 |
Correct |
2 ms |
1872 KB |
Output is correct |
6 |
Correct |
2 ms |
1872 KB |
Output is correct |
7 |
Correct |
2 ms |
1872 KB |
Output is correct |
8 |
Correct |
2 ms |
2040 KB |
Output is correct |
9 |
Correct |
2 ms |
1940 KB |
Output is correct |
10 |
Correct |
2 ms |
1872 KB |
Output is correct |
11 |
Correct |
2 ms |
1872 KB |
Output is correct |
12 |
Correct |
2 ms |
1872 KB |
Output is correct |
13 |
Correct |
2 ms |
1872 KB |
Output is correct |
14 |
Correct |
2 ms |
1872 KB |
Output is correct |
15 |
Correct |
2 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1872 KB |
Output is correct |
2 |
Correct |
2 ms |
1872 KB |
Output is correct |
3 |
Correct |
2 ms |
1872 KB |
Output is correct |
4 |
Correct |
2 ms |
2040 KB |
Output is correct |
5 |
Correct |
2 ms |
1872 KB |
Output is correct |
6 |
Correct |
2 ms |
1872 KB |
Output is correct |
7 |
Correct |
2 ms |
2096 KB |
Output is correct |
8 |
Correct |
2 ms |
1872 KB |
Output is correct |
9 |
Correct |
2 ms |
2040 KB |
Output is correct |
10 |
Correct |
2 ms |
1872 KB |
Output is correct |
11 |
Correct |
3 ms |
1872 KB |
Output is correct |
12 |
Correct |
2 ms |
1872 KB |
Output is correct |
13 |
Correct |
2 ms |
1872 KB |
Output is correct |
14 |
Correct |
2 ms |
1872 KB |
Output is correct |
15 |
Correct |
2 ms |
1872 KB |
Output is correct |
16 |
Correct |
2 ms |
1872 KB |
Output is correct |
17 |
Correct |
2 ms |
2128 KB |
Output is correct |
18 |
Correct |
2 ms |
2044 KB |
Output is correct |
19 |
Correct |
2 ms |
1872 KB |
Output is correct |
20 |
Correct |
5 ms |
2128 KB |
Output is correct |
21 |
Correct |
2 ms |
1872 KB |
Output is correct |
22 |
Correct |
2 ms |
1872 KB |
Output is correct |
23 |
Correct |
2 ms |
1872 KB |
Output is correct |
24 |
Correct |
2 ms |
2128 KB |
Output is correct |
25 |
Correct |
2 ms |
2128 KB |
Output is correct |
26 |
Correct |
4 ms |
1872 KB |
Output is correct |
27 |
Correct |
5 ms |
2040 KB |
Output is correct |
28 |
Correct |
2 ms |
1872 KB |
Output is correct |
29 |
Correct |
4 ms |
2384 KB |
Output is correct |
30 |
Correct |
3 ms |
2128 KB |
Output is correct |
31 |
Correct |
3 ms |
2128 KB |
Output is correct |
32 |
Correct |
2 ms |
2296 KB |
Output is correct |
33 |
Correct |
5 ms |
2384 KB |
Output is correct |
34 |
Correct |
4 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2040 KB |
Output is correct |
2 |
Correct |
2 ms |
1872 KB |
Output is correct |
3 |
Correct |
2 ms |
1872 KB |
Output is correct |
4 |
Correct |
2 ms |
1872 KB |
Output is correct |
5 |
Correct |
2 ms |
1872 KB |
Output is correct |
6 |
Correct |
2 ms |
1872 KB |
Output is correct |
7 |
Correct |
2 ms |
2044 KB |
Output is correct |
8 |
Correct |
2 ms |
1872 KB |
Output is correct |
9 |
Correct |
2 ms |
1872 KB |
Output is correct |
10 |
Correct |
2 ms |
1872 KB |
Output is correct |
11 |
Correct |
2 ms |
1872 KB |
Output is correct |
12 |
Correct |
2 ms |
1872 KB |
Output is correct |
13 |
Correct |
2 ms |
1872 KB |
Output is correct |
14 |
Correct |
2 ms |
1872 KB |
Output is correct |
15 |
Correct |
2 ms |
2096 KB |
Output is correct |
16 |
Correct |
2 ms |
1872 KB |
Output is correct |
17 |
Correct |
2 ms |
2128 KB |
Output is correct |
18 |
Correct |
2 ms |
1872 KB |
Output is correct |
19 |
Correct |
2 ms |
1872 KB |
Output is correct |
20 |
Correct |
5 ms |
2172 KB |
Output is correct |
21 |
Correct |
2 ms |
1872 KB |
Output is correct |
22 |
Correct |
2 ms |
1872 KB |
Output is correct |
23 |
Correct |
3 ms |
1872 KB |
Output is correct |
24 |
Correct |
3 ms |
1924 KB |
Output is correct |
25 |
Correct |
3 ms |
2128 KB |
Output is correct |
26 |
Correct |
5 ms |
2100 KB |
Output is correct |
27 |
Correct |
5 ms |
2040 KB |
Output is correct |
28 |
Correct |
2 ms |
1872 KB |
Output is correct |
29 |
Correct |
4 ms |
2384 KB |
Output is correct |
30 |
Correct |
2 ms |
2128 KB |
Output is correct |
31 |
Correct |
3 ms |
2128 KB |
Output is correct |
32 |
Correct |
3 ms |
2128 KB |
Output is correct |
33 |
Correct |
4 ms |
2384 KB |
Output is correct |
34 |
Correct |
3 ms |
2384 KB |
Output is correct |
35 |
Correct |
6 ms |
2676 KB |
Output is correct |
36 |
Correct |
3 ms |
2128 KB |
Output is correct |
37 |
Correct |
6 ms |
2640 KB |
Output is correct |
38 |
Correct |
8 ms |
2896 KB |
Output is correct |
39 |
Correct |
7 ms |
2692 KB |
Output is correct |
40 |
Correct |
7 ms |
2896 KB |
Output is correct |
41 |
Correct |
10 ms |
2912 KB |
Output is correct |
42 |
Correct |
47 ms |
2436 KB |
Output is correct |
43 |
Correct |
48 ms |
2512 KB |
Output is correct |
44 |
Correct |
50 ms |
2552 KB |
Output is correct |
45 |
Correct |
7 ms |
2896 KB |
Output is correct |
46 |
Correct |
7 ms |
2948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1872 KB |
Output is correct |
2 |
Correct |
2 ms |
1872 KB |
Output is correct |
3 |
Correct |
3 ms |
1872 KB |
Output is correct |
4 |
Correct |
2 ms |
1872 KB |
Output is correct |
5 |
Correct |
2 ms |
1872 KB |
Output is correct |
6 |
Correct |
2 ms |
1872 KB |
Output is correct |
7 |
Correct |
2 ms |
1872 KB |
Output is correct |
8 |
Correct |
2 ms |
1872 KB |
Output is correct |
9 |
Correct |
2 ms |
1872 KB |
Output is correct |
10 |
Correct |
2 ms |
1872 KB |
Output is correct |
11 |
Correct |
2 ms |
1872 KB |
Output is correct |
12 |
Correct |
2 ms |
1872 KB |
Output is correct |
13 |
Correct |
2 ms |
1872 KB |
Output is correct |
14 |
Correct |
2 ms |
1872 KB |
Output is correct |
15 |
Correct |
2 ms |
1872 KB |
Output is correct |
16 |
Correct |
3 ms |
1872 KB |
Output is correct |
17 |
Correct |
2 ms |
2128 KB |
Output is correct |
18 |
Correct |
2 ms |
1872 KB |
Output is correct |
19 |
Correct |
2 ms |
2040 KB |
Output is correct |
20 |
Correct |
6 ms |
2128 KB |
Output is correct |
21 |
Correct |
2 ms |
1872 KB |
Output is correct |
22 |
Correct |
2 ms |
1872 KB |
Output is correct |
23 |
Correct |
2 ms |
1872 KB |
Output is correct |
24 |
Correct |
2 ms |
2128 KB |
Output is correct |
25 |
Correct |
3 ms |
2128 KB |
Output is correct |
26 |
Correct |
7 ms |
1872 KB |
Output is correct |
27 |
Correct |
4 ms |
2096 KB |
Output is correct |
28 |
Correct |
2 ms |
1872 KB |
Output is correct |
29 |
Correct |
3 ms |
2384 KB |
Output is correct |
30 |
Correct |
3 ms |
2128 KB |
Output is correct |
31 |
Correct |
3 ms |
2128 KB |
Output is correct |
32 |
Correct |
2 ms |
2292 KB |
Output is correct |
33 |
Correct |
3 ms |
2384 KB |
Output is correct |
34 |
Correct |
4 ms |
2384 KB |
Output is correct |
35 |
Correct |
7 ms |
2640 KB |
Output is correct |
36 |
Correct |
3 ms |
2128 KB |
Output is correct |
37 |
Correct |
7 ms |
2816 KB |
Output is correct |
38 |
Correct |
8 ms |
2896 KB |
Output is correct |
39 |
Correct |
11 ms |
2660 KB |
Output is correct |
40 |
Correct |
8 ms |
2896 KB |
Output is correct |
41 |
Correct |
8 ms |
2896 KB |
Output is correct |
42 |
Correct |
49 ms |
2604 KB |
Output is correct |
43 |
Correct |
50 ms |
2512 KB |
Output is correct |
44 |
Correct |
50 ms |
2628 KB |
Output is correct |
45 |
Correct |
7 ms |
2888 KB |
Output is correct |
46 |
Correct |
8 ms |
2896 KB |
Output is correct |
47 |
Correct |
16 ms |
3792 KB |
Output is correct |
48 |
Correct |
7 ms |
2640 KB |
Output is correct |
49 |
Correct |
7 ms |
2640 KB |
Output is correct |
50 |
Correct |
5 ms |
2508 KB |
Output is correct |
51 |
Correct |
23 ms |
6088 KB |
Output is correct |
52 |
Correct |
25 ms |
3964 KB |
Output is correct |
53 |
Correct |
14 ms |
4044 KB |
Output is correct |
54 |
Correct |
4 ms |
2384 KB |
Output is correct |
55 |
Correct |
4 ms |
2384 KB |
Output is correct |
56 |
Correct |
775 ms |
3796 KB |
Output is correct |
57 |
Correct |
43 ms |
8384 KB |
Output is correct |
58 |
Correct |
654 ms |
2772 KB |
Output is correct |
59 |
Correct |
704 ms |
2772 KB |
Output is correct |
60 |
Correct |
669 ms |
2772 KB |
Output is correct |
61 |
Correct |
593 ms |
2768 KB |
Output is correct |
62 |
Correct |
8 ms |
3152 KB |
Output is correct |
63 |
Correct |
26 ms |
5740 KB |
Output is correct |
64 |
Correct |
30 ms |
8904 KB |
Output is correct |
65 |
Correct |
33 ms |
9064 KB |
Output is correct |
66 |
Correct |
45 ms |
8900 KB |
Output is correct |
67 |
Correct |
44 ms |
8900 KB |
Output is correct |