#include<bits/stdc++.h>
#include<unordered_set>
using namespace std;
#define se second
#define fi first
#define mk(a,b) make_pair(a,b)
typedef pair<int,int> pii;
typedef pair<int,pii> pipii;
int N,M;
vector<int> V[30010];
bool vis[30010];
int s,t;
unordered_set<int> st[30010];
int main()
{
scanf("%d%d",&N,&M);
for(int i = 0; i < M; i++)
{
int b,p;
scanf("%d%d",&b,&p);
V[b].push_back(p);
if(i == 0)s = b;
if(i == 1)t = b;
}
for(int i = 0; i < N; i++)
{
sort(V[i].begin(),V[i].end());
V[i].erase(unique(V[i].begin(),V[i].end()),V[i].end());
}
queue<pipii> que;
que.push(mk(1,mk(s,0)));
while(que.size())
{
pipii now = que.front();
que.pop();
if(st[now.se.fi].count(now.se.se) != 0)continue;
st[now.se.fi].insert(now.se.se);
if(now.se.fi == t)
{
printf("%d\n",now.fi - 1);
return 0;
}
if(!vis[now.se.fi])
{
vis[now.se.fi] = true;
for(int i = 0; i < V[now.se.fi].size(); i++)
{
if(now.se.fi + V[now.se.fi][i] < N)
que.push(mk(now.fi + 1,mk(now.se.fi + V[now.se.fi][i],V[now.se.fi][i])));
if(0 <= now.se.fi - V[now.se.fi][i])
que.push(mk(now.fi + 1,mk(now.se.fi - V[now.se.fi][i],V[now.se.fi][i])));
}
}
vis[now.se.fi] = true;
if(now.se.fi + now.se.se < N)que.push(mk(now.fi + 1,mk(now.se.fi + now.se.se,now.se.se)));
if(0 <= now.se.fi - now.se.se)que.push(mk(now.fi + 1,mk(now.se.fi - now.se.se,now.se.se)));
}
printf("-1\n");
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:52:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < V[now.se.fi].size(); i++)
~~^~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:20:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&N,&M);
~~~~~^~~~~~~~~~~~~~
skyscraper.cpp:24:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&b,&p);
~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2792 KB |
Output is correct |
3 |
Correct |
4 ms |
2792 KB |
Output is correct |
4 |
Correct |
3 ms |
2792 KB |
Output is correct |
5 |
Correct |
4 ms |
2792 KB |
Output is correct |
6 |
Correct |
4 ms |
2848 KB |
Output is correct |
7 |
Correct |
3 ms |
2932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2932 KB |
Output is correct |
2 |
Correct |
4 ms |
2976 KB |
Output is correct |
3 |
Correct |
4 ms |
2976 KB |
Output is correct |
4 |
Correct |
4 ms |
2976 KB |
Output is correct |
5 |
Correct |
4 ms |
2976 KB |
Output is correct |
6 |
Correct |
4 ms |
2976 KB |
Output is correct |
7 |
Correct |
3 ms |
2976 KB |
Output is correct |
8 |
Correct |
3 ms |
2976 KB |
Output is correct |
9 |
Correct |
4 ms |
3056 KB |
Output is correct |
10 |
Correct |
4 ms |
3056 KB |
Output is correct |
11 |
Correct |
4 ms |
3056 KB |
Output is correct |
12 |
Correct |
4 ms |
3056 KB |
Output is correct |
13 |
Correct |
4 ms |
3056 KB |
Output is correct |
14 |
Correct |
5 ms |
3196 KB |
Output is correct |
15 |
Correct |
4 ms |
3196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3196 KB |
Output is correct |
2 |
Correct |
4 ms |
3196 KB |
Output is correct |
3 |
Correct |
4 ms |
3196 KB |
Output is correct |
4 |
Correct |
3 ms |
3196 KB |
Output is correct |
5 |
Correct |
4 ms |
3196 KB |
Output is correct |
6 |
Correct |
4 ms |
3196 KB |
Output is correct |
7 |
Correct |
4 ms |
3196 KB |
Output is correct |
8 |
Correct |
4 ms |
3196 KB |
Output is correct |
9 |
Correct |
4 ms |
3196 KB |
Output is correct |
10 |
Correct |
4 ms |
3196 KB |
Output is correct |
11 |
Correct |
4 ms |
3196 KB |
Output is correct |
12 |
Correct |
4 ms |
3196 KB |
Output is correct |
13 |
Correct |
4 ms |
3196 KB |
Output is correct |
14 |
Correct |
5 ms |
3204 KB |
Output is correct |
15 |
Correct |
4 ms |
3204 KB |
Output is correct |
16 |
Correct |
4 ms |
3204 KB |
Output is correct |
17 |
Correct |
4 ms |
3204 KB |
Output is correct |
18 |
Correct |
4 ms |
3204 KB |
Output is correct |
19 |
Correct |
4 ms |
3204 KB |
Output is correct |
20 |
Correct |
5 ms |
3204 KB |
Output is correct |
21 |
Correct |
4 ms |
3204 KB |
Output is correct |
22 |
Correct |
4 ms |
3204 KB |
Output is correct |
23 |
Correct |
5 ms |
3204 KB |
Output is correct |
24 |
Correct |
6 ms |
3204 KB |
Output is correct |
25 |
Correct |
4 ms |
3204 KB |
Output is correct |
26 |
Correct |
5 ms |
3204 KB |
Output is correct |
27 |
Correct |
5 ms |
3204 KB |
Output is correct |
28 |
Correct |
10 ms |
3836 KB |
Output is correct |
29 |
Correct |
20 ms |
5552 KB |
Output is correct |
30 |
Correct |
8 ms |
5552 KB |
Output is correct |
31 |
Correct |
13 ms |
5552 KB |
Output is correct |
32 |
Correct |
10 ms |
5552 KB |
Output is correct |
33 |
Correct |
36 ms |
8060 KB |
Output is correct |
34 |
Correct |
21 ms |
8060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8060 KB |
Output is correct |
2 |
Correct |
4 ms |
8060 KB |
Output is correct |
3 |
Correct |
3 ms |
8060 KB |
Output is correct |
4 |
Correct |
4 ms |
8060 KB |
Output is correct |
5 |
Correct |
4 ms |
8060 KB |
Output is correct |
6 |
Correct |
3 ms |
8060 KB |
Output is correct |
7 |
Correct |
4 ms |
8060 KB |
Output is correct |
8 |
Correct |
3 ms |
8060 KB |
Output is correct |
9 |
Correct |
3 ms |
8060 KB |
Output is correct |
10 |
Correct |
4 ms |
8060 KB |
Output is correct |
11 |
Correct |
4 ms |
8060 KB |
Output is correct |
12 |
Correct |
4 ms |
8060 KB |
Output is correct |
13 |
Correct |
4 ms |
8060 KB |
Output is correct |
14 |
Correct |
6 ms |
8060 KB |
Output is correct |
15 |
Correct |
4 ms |
8060 KB |
Output is correct |
16 |
Correct |
4 ms |
8060 KB |
Output is correct |
17 |
Correct |
5 ms |
8060 KB |
Output is correct |
18 |
Correct |
4 ms |
8060 KB |
Output is correct |
19 |
Correct |
4 ms |
8060 KB |
Output is correct |
20 |
Correct |
5 ms |
8060 KB |
Output is correct |
21 |
Correct |
4 ms |
8060 KB |
Output is correct |
22 |
Correct |
4 ms |
8060 KB |
Output is correct |
23 |
Correct |
5 ms |
8060 KB |
Output is correct |
24 |
Correct |
6 ms |
8060 KB |
Output is correct |
25 |
Correct |
5 ms |
8060 KB |
Output is correct |
26 |
Correct |
4 ms |
8060 KB |
Output is correct |
27 |
Correct |
5 ms |
8060 KB |
Output is correct |
28 |
Correct |
9 ms |
8060 KB |
Output is correct |
29 |
Correct |
20 ms |
8060 KB |
Output is correct |
30 |
Correct |
8 ms |
8060 KB |
Output is correct |
31 |
Correct |
13 ms |
8060 KB |
Output is correct |
32 |
Correct |
10 ms |
8060 KB |
Output is correct |
33 |
Correct |
37 ms |
8188 KB |
Output is correct |
34 |
Correct |
23 ms |
8188 KB |
Output is correct |
35 |
Correct |
14 ms |
8188 KB |
Output is correct |
36 |
Correct |
5 ms |
8188 KB |
Output is correct |
37 |
Correct |
11 ms |
8188 KB |
Output is correct |
38 |
Correct |
14 ms |
8188 KB |
Output is correct |
39 |
Correct |
13 ms |
8188 KB |
Output is correct |
40 |
Correct |
14 ms |
8188 KB |
Output is correct |
41 |
Correct |
13 ms |
8188 KB |
Output is correct |
42 |
Correct |
10 ms |
8188 KB |
Output is correct |
43 |
Correct |
11 ms |
8188 KB |
Output is correct |
44 |
Correct |
13 ms |
8188 KB |
Output is correct |
45 |
Correct |
198 ms |
23548 KB |
Output is correct |
46 |
Correct |
101 ms |
23548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23548 KB |
Output is correct |
2 |
Correct |
4 ms |
23548 KB |
Output is correct |
3 |
Correct |
4 ms |
23548 KB |
Output is correct |
4 |
Correct |
4 ms |
23548 KB |
Output is correct |
5 |
Correct |
4 ms |
23548 KB |
Output is correct |
6 |
Correct |
3 ms |
23548 KB |
Output is correct |
7 |
Correct |
3 ms |
23548 KB |
Output is correct |
8 |
Correct |
3 ms |
23548 KB |
Output is correct |
9 |
Correct |
4 ms |
23548 KB |
Output is correct |
10 |
Correct |
4 ms |
23548 KB |
Output is correct |
11 |
Correct |
4 ms |
23548 KB |
Output is correct |
12 |
Correct |
4 ms |
23548 KB |
Output is correct |
13 |
Correct |
4 ms |
23548 KB |
Output is correct |
14 |
Correct |
5 ms |
23548 KB |
Output is correct |
15 |
Correct |
5 ms |
23548 KB |
Output is correct |
16 |
Correct |
4 ms |
23548 KB |
Output is correct |
17 |
Correct |
5 ms |
23548 KB |
Output is correct |
18 |
Correct |
4 ms |
23548 KB |
Output is correct |
19 |
Correct |
4 ms |
23548 KB |
Output is correct |
20 |
Correct |
5 ms |
23548 KB |
Output is correct |
21 |
Correct |
4 ms |
23548 KB |
Output is correct |
22 |
Correct |
4 ms |
23548 KB |
Output is correct |
23 |
Correct |
5 ms |
23548 KB |
Output is correct |
24 |
Correct |
6 ms |
23548 KB |
Output is correct |
25 |
Correct |
5 ms |
23548 KB |
Output is correct |
26 |
Correct |
5 ms |
23548 KB |
Output is correct |
27 |
Correct |
5 ms |
23548 KB |
Output is correct |
28 |
Correct |
10 ms |
23548 KB |
Output is correct |
29 |
Correct |
19 ms |
23548 KB |
Output is correct |
30 |
Correct |
8 ms |
23548 KB |
Output is correct |
31 |
Correct |
14 ms |
23548 KB |
Output is correct |
32 |
Correct |
9 ms |
23548 KB |
Output is correct |
33 |
Correct |
40 ms |
23548 KB |
Output is correct |
34 |
Correct |
20 ms |
23548 KB |
Output is correct |
35 |
Correct |
13 ms |
23548 KB |
Output is correct |
36 |
Correct |
6 ms |
23548 KB |
Output is correct |
37 |
Correct |
11 ms |
23548 KB |
Output is correct |
38 |
Correct |
14 ms |
23548 KB |
Output is correct |
39 |
Correct |
19 ms |
23548 KB |
Output is correct |
40 |
Correct |
13 ms |
23548 KB |
Output is correct |
41 |
Correct |
14 ms |
23548 KB |
Output is correct |
42 |
Correct |
10 ms |
23548 KB |
Output is correct |
43 |
Correct |
11 ms |
23548 KB |
Output is correct |
44 |
Correct |
10 ms |
23548 KB |
Output is correct |
45 |
Correct |
200 ms |
23548 KB |
Output is correct |
46 |
Correct |
121 ms |
23548 KB |
Output is correct |
47 |
Correct |
13 ms |
23548 KB |
Output is correct |
48 |
Correct |
12 ms |
23548 KB |
Output is correct |
49 |
Correct |
12 ms |
23548 KB |
Output is correct |
50 |
Correct |
9 ms |
23548 KB |
Output is correct |
51 |
Correct |
30 ms |
23548 KB |
Output is correct |
52 |
Correct |
38 ms |
23548 KB |
Output is correct |
53 |
Correct |
18 ms |
23548 KB |
Output is correct |
54 |
Correct |
7 ms |
23548 KB |
Output is correct |
55 |
Correct |
9 ms |
23548 KB |
Output is correct |
56 |
Correct |
23 ms |
23548 KB |
Output is correct |
57 |
Correct |
5 ms |
23548 KB |
Output is correct |
58 |
Correct |
25 ms |
23548 KB |
Output is correct |
59 |
Correct |
25 ms |
23548 KB |
Output is correct |
60 |
Correct |
27 ms |
23548 KB |
Output is correct |
61 |
Correct |
23 ms |
23548 KB |
Output is correct |
62 |
Correct |
292 ms |
23548 KB |
Output is correct |
63 |
Execution timed out |
1084 ms |
63704 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |