#include <bits/stdc++.h>
using namespace std;
const int N = 30030;
const int base = 180;
#define Data pair < int, pair < int, int > >
int n, m;
int f[N][base];
int d[N][base];
vector < int > doges[N];
list < Data > que[N * 20];
bool minimize(int &u, int v){
if(u > v){
u = v;
return true;
}
return false;
}
int main(){
if(fopen("1.inp", "r")){
freopen("1.inp", "r", stdin);
}
scanf("%d%d", &n, &m);
int start = -1, finish = -1;
for(int i = 1; i <= m; ++i){
int u, v;
scanf("%d%d", &u, &v);
if(start == -1){
start = u;
}
else if(finish == -1){
finish = u;
}
doges[u].push_back(v);
}
priority_queue < Data, vector < Data >, greater < Data > > heap;
memset(f, 60, sizeof f);
f[start][0] = 0;
que[0].emplace_back(0, make_pair(start, 0));
for(int t = 0; t < N * 20; ++t){
for(auto p : que[t]){
int u = p.second.first;;
int x = p.second.second;
heap.pop();
if(d[u][x] == 1) continue;
d[u][x] = 1;
if(u == finish){
cout << f[u][x];
return 0;
}
if(u - x >= 0){
if(minimize(f[u - x][x], f[u][x] + 1)){
que[f[u - x][x]].emplace_back(f[u - x][x], make_pair(u - x, x));
}
}
if(u + x < n){
if(minimize(f[u + x][x], f[u][x] + 1)){
que[f[u + x][x]].emplace_back(f[u + x][x], make_pair(u + x, x));
}
}
for(int val : doges[u]){
if(val < base){
if(u - val >= 0){
if(minimize(f[u - val][val], f[u][x] + 1)){
que[f[u - val][val]].emplace_back(f[u - val][val], make_pair(u - val, val));
}
}
if(u + val < n){
if(minimize(f[u + val][val], f[u][x] + 1)){
que[f[u + val][val]].emplace_back(f[u + val][val], make_pair(u + val, val));
}
}
}
else{
for(int t = 1; t < N / base; ++t){
if(u - t * val < 0 && u + t * val >= n) break;
if(u - t * val >= 0){
if(minimize(f[u - t * val][0], f[u][x] + t)){
que[f[u - t * val][0]].emplace_back(f[u - t * val][0], make_pair(u - t * val, 0));
}
}
if(u + t * val < n){
if(minimize(f[u + t * val][0], f[u][x] + t)){
que[f[u + t * val][0]].emplace_back(f[u + t * val][0], make_pair(u + t * val, 0));
}
}
}
}
}
}
}
cout << -1;
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:27:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("1.inp", "r", stdin);
^
skyscraper.cpp:30:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
^
skyscraper.cpp:36:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &u, &v);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
59028 KB |
Output is correct |
2 |
Correct |
6 ms |
59028 KB |
Output is correct |
3 |
Correct |
3 ms |
59028 KB |
Output is correct |
4 |
Correct |
6 ms |
59028 KB |
Output is correct |
5 |
Correct |
13 ms |
59028 KB |
Output is correct |
6 |
Correct |
6 ms |
59028 KB |
Output is correct |
7 |
Correct |
3 ms |
59028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
59028 KB |
Output is correct |
2 |
Correct |
3 ms |
59028 KB |
Output is correct |
3 |
Correct |
0 ms |
59028 KB |
Output is correct |
4 |
Correct |
9 ms |
59028 KB |
Output is correct |
5 |
Correct |
6 ms |
59028 KB |
Output is correct |
6 |
Correct |
9 ms |
59028 KB |
Output is correct |
7 |
Correct |
6 ms |
59028 KB |
Output is correct |
8 |
Correct |
13 ms |
59028 KB |
Output is correct |
9 |
Correct |
9 ms |
59028 KB |
Output is correct |
10 |
Correct |
3 ms |
59028 KB |
Output is correct |
11 |
Correct |
9 ms |
59028 KB |
Output is correct |
12 |
Correct |
6 ms |
59028 KB |
Output is correct |
13 |
Correct |
6 ms |
59028 KB |
Output is correct |
14 |
Correct |
3 ms |
59292 KB |
Output is correct |
15 |
Correct |
3 ms |
59160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
59028 KB |
Output is correct |
2 |
Correct |
3 ms |
59028 KB |
Output is correct |
3 |
Correct |
6 ms |
59028 KB |
Output is correct |
4 |
Correct |
6 ms |
59028 KB |
Output is correct |
5 |
Correct |
9 ms |
59028 KB |
Output is correct |
6 |
Correct |
3 ms |
59028 KB |
Output is correct |
7 |
Correct |
3 ms |
59028 KB |
Output is correct |
8 |
Correct |
3 ms |
59028 KB |
Output is correct |
9 |
Correct |
3 ms |
59028 KB |
Output is correct |
10 |
Correct |
3 ms |
59028 KB |
Output is correct |
11 |
Correct |
9 ms |
59028 KB |
Output is correct |
12 |
Correct |
3 ms |
59028 KB |
Output is correct |
13 |
Correct |
16 ms |
59028 KB |
Output is correct |
14 |
Correct |
13 ms |
59292 KB |
Output is correct |
15 |
Correct |
6 ms |
59160 KB |
Output is correct |
16 |
Correct |
3 ms |
59028 KB |
Output is correct |
17 |
Correct |
3 ms |
59160 KB |
Output is correct |
18 |
Correct |
6 ms |
59028 KB |
Output is correct |
19 |
Correct |
16 ms |
59028 KB |
Output is correct |
20 |
Correct |
6 ms |
59160 KB |
Output is correct |
21 |
Correct |
13 ms |
59028 KB |
Output is correct |
22 |
Correct |
6 ms |
59028 KB |
Output is correct |
23 |
Correct |
9 ms |
59292 KB |
Output is correct |
24 |
Correct |
9 ms |
59292 KB |
Output is correct |
25 |
Correct |
0 ms |
59160 KB |
Output is correct |
26 |
Correct |
6 ms |
59160 KB |
Output is correct |
27 |
Correct |
6 ms |
59292 KB |
Output is correct |
28 |
Correct |
3 ms |
59820 KB |
Output is correct |
29 |
Correct |
13 ms |
61932 KB |
Output is correct |
30 |
Correct |
3 ms |
59820 KB |
Output is correct |
31 |
Correct |
6 ms |
60612 KB |
Output is correct |
32 |
Correct |
6 ms |
60084 KB |
Output is correct |
33 |
Correct |
19 ms |
64704 KB |
Output is correct |
34 |
Correct |
19 ms |
62064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
59028 KB |
Output is correct |
2 |
Correct |
9 ms |
59028 KB |
Output is correct |
3 |
Correct |
6 ms |
59028 KB |
Output is correct |
4 |
Correct |
9 ms |
59028 KB |
Output is correct |
5 |
Correct |
9 ms |
59028 KB |
Output is correct |
6 |
Correct |
6 ms |
59028 KB |
Output is correct |
7 |
Correct |
3 ms |
59028 KB |
Output is correct |
8 |
Correct |
6 ms |
59028 KB |
Output is correct |
9 |
Correct |
3 ms |
59028 KB |
Output is correct |
10 |
Correct |
13 ms |
59028 KB |
Output is correct |
11 |
Correct |
3 ms |
59028 KB |
Output is correct |
12 |
Correct |
0 ms |
59028 KB |
Output is correct |
13 |
Correct |
16 ms |
59028 KB |
Output is correct |
14 |
Correct |
6 ms |
59292 KB |
Output is correct |
15 |
Correct |
9 ms |
59160 KB |
Output is correct |
16 |
Correct |
13 ms |
59028 KB |
Output is correct |
17 |
Correct |
3 ms |
59160 KB |
Output is correct |
18 |
Correct |
13 ms |
59028 KB |
Output is correct |
19 |
Correct |
13 ms |
59028 KB |
Output is correct |
20 |
Correct |
0 ms |
59160 KB |
Output is correct |
21 |
Correct |
6 ms |
59028 KB |
Output is correct |
22 |
Correct |
13 ms |
59028 KB |
Output is correct |
23 |
Correct |
13 ms |
59292 KB |
Output is correct |
24 |
Correct |
13 ms |
59292 KB |
Output is correct |
25 |
Correct |
6 ms |
59160 KB |
Output is correct |
26 |
Correct |
0 ms |
59160 KB |
Output is correct |
27 |
Correct |
6 ms |
59292 KB |
Output is correct |
28 |
Correct |
13 ms |
59820 KB |
Output is correct |
29 |
Correct |
6 ms |
61932 KB |
Output is correct |
30 |
Correct |
9 ms |
59820 KB |
Output is correct |
31 |
Correct |
6 ms |
60612 KB |
Output is correct |
32 |
Correct |
6 ms |
60084 KB |
Output is correct |
33 |
Correct |
19 ms |
64704 KB |
Output is correct |
34 |
Correct |
13 ms |
62064 KB |
Output is correct |
35 |
Correct |
19 ms |
59556 KB |
Output is correct |
36 |
Correct |
0 ms |
59160 KB |
Output is correct |
37 |
Correct |
16 ms |
59556 KB |
Output is correct |
38 |
Correct |
19 ms |
59556 KB |
Output is correct |
39 |
Correct |
13 ms |
59292 KB |
Output is correct |
40 |
Correct |
19 ms |
59292 KB |
Output is correct |
41 |
Correct |
16 ms |
59424 KB |
Output is correct |
42 |
Correct |
9 ms |
59372 KB |
Output is correct |
43 |
Correct |
13 ms |
59372 KB |
Output is correct |
44 |
Correct |
3 ms |
59300 KB |
Output is correct |
45 |
Correct |
146 ms |
75660 KB |
Output is correct |
46 |
Correct |
113 ms |
66024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
59028 KB |
Output is correct |
2 |
Correct |
3 ms |
59028 KB |
Output is correct |
3 |
Correct |
9 ms |
59028 KB |
Output is correct |
4 |
Correct |
6 ms |
59028 KB |
Output is correct |
5 |
Correct |
9 ms |
59028 KB |
Output is correct |
6 |
Correct |
3 ms |
59028 KB |
Output is correct |
7 |
Correct |
6 ms |
59028 KB |
Output is correct |
8 |
Correct |
6 ms |
59028 KB |
Output is correct |
9 |
Correct |
9 ms |
59028 KB |
Output is correct |
10 |
Correct |
0 ms |
59028 KB |
Output is correct |
11 |
Correct |
3 ms |
59028 KB |
Output is correct |
12 |
Correct |
3 ms |
59028 KB |
Output is correct |
13 |
Correct |
9 ms |
59028 KB |
Output is correct |
14 |
Correct |
13 ms |
59292 KB |
Output is correct |
15 |
Correct |
6 ms |
59160 KB |
Output is correct |
16 |
Correct |
16 ms |
59028 KB |
Output is correct |
17 |
Correct |
9 ms |
59160 KB |
Output is correct |
18 |
Correct |
9 ms |
59028 KB |
Output is correct |
19 |
Correct |
9 ms |
59028 KB |
Output is correct |
20 |
Correct |
9 ms |
59160 KB |
Output is correct |
21 |
Correct |
6 ms |
59028 KB |
Output is correct |
22 |
Correct |
13 ms |
59028 KB |
Output is correct |
23 |
Correct |
6 ms |
59292 KB |
Output is correct |
24 |
Correct |
3 ms |
59292 KB |
Output is correct |
25 |
Correct |
6 ms |
59160 KB |
Output is correct |
26 |
Correct |
3 ms |
59160 KB |
Output is correct |
27 |
Correct |
6 ms |
59292 KB |
Output is correct |
28 |
Correct |
9 ms |
59820 KB |
Output is correct |
29 |
Correct |
13 ms |
61932 KB |
Output is correct |
30 |
Correct |
6 ms |
59820 KB |
Output is correct |
31 |
Correct |
13 ms |
60612 KB |
Output is correct |
32 |
Correct |
3 ms |
60084 KB |
Output is correct |
33 |
Correct |
19 ms |
64704 KB |
Output is correct |
34 |
Correct |
13 ms |
62064 KB |
Output is correct |
35 |
Correct |
13 ms |
59556 KB |
Output is correct |
36 |
Correct |
6 ms |
59160 KB |
Output is correct |
37 |
Correct |
9 ms |
59556 KB |
Output is correct |
38 |
Correct |
19 ms |
59556 KB |
Output is correct |
39 |
Correct |
16 ms |
59292 KB |
Output is correct |
40 |
Correct |
13 ms |
59292 KB |
Output is correct |
41 |
Correct |
13 ms |
59424 KB |
Output is correct |
42 |
Correct |
13 ms |
59372 KB |
Output is correct |
43 |
Correct |
13 ms |
59372 KB |
Output is correct |
44 |
Correct |
9 ms |
59300 KB |
Output is correct |
45 |
Correct |
146 ms |
75660 KB |
Output is correct |
46 |
Correct |
109 ms |
66024 KB |
Output is correct |
47 |
Correct |
26 ms |
60612 KB |
Output is correct |
48 |
Correct |
16 ms |
59556 KB |
Output is correct |
49 |
Correct |
13 ms |
59556 KB |
Output is correct |
50 |
Correct |
19 ms |
59424 KB |
Output is correct |
51 |
Correct |
43 ms |
61404 KB |
Output is correct |
52 |
Correct |
46 ms |
61536 KB |
Output is correct |
53 |
Correct |
26 ms |
59820 KB |
Output is correct |
54 |
Correct |
13 ms |
59688 KB |
Output is correct |
55 |
Correct |
19 ms |
60216 KB |
Output is correct |
56 |
Correct |
23 ms |
61404 KB |
Output is correct |
57 |
Correct |
6 ms |
59160 KB |
Output is correct |
58 |
Correct |
33 ms |
62272 KB |
Output is correct |
59 |
Correct |
19 ms |
61876 KB |
Output is correct |
60 |
Correct |
29 ms |
61884 KB |
Output is correct |
61 |
Correct |
19 ms |
61628 KB |
Output is correct |
62 |
Correct |
116 ms |
71964 KB |
Output is correct |
63 |
Correct |
103 ms |
64704 KB |
Output is correct |
64 |
Correct |
109 ms |
64308 KB |
Output is correct |
65 |
Execution timed out |
1000 ms |
110640 KB |
Execution timed out |
66 |
Halted |
0 ms |
0 KB |
- |