#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
int n,m,S,T,d[2005][2005];
bool Visited[30005],Free[2005][2005];
vector<int> g[30005];
queue<ii> Q;
int main()
{
ios_base::sync_with_stdio(false);
//freopen("TEST.INP","r",stdin);
cin>>n>>m;
int pos,step;
for(int i=1;i<=m;i++)
{
cin>>pos>>step;
g[pos+1].push_back(step);
if(i==1)
S=pos+1;
if(i==2)
T=pos+1;
}
for(int i=0; i<g[S].size(); i++)
{
Free[S][g[S][i]]=true;
Q.push(ii(S,g[S][i]));
}
while(Q.size()>0)
{
int u=Q.front().first;
int v=Q.front().second;
Q.pop();
if(u==T)
{
cout<<d[u][v];
return 0;
}
if(Visited[u]==false)
{
Visited[u]=true;
for(int i=0; i<g[u].size(); i++)
{
if(u-g[u][i]>=1&&Free[u-g[u][i]][g[u][i]]==false)
{
Free[u-g[u][i]][g[u][i]]=true;
d[u-g[u][i]][g[u][i]]=d[u][v]+1;
Q.push(ii(u-g[u][i],g[u][i]));
}
if(u+g[u][i]<=n&&Free[u+g[u][i]][g[u][i]]==false)
{
Free[u+g[u][i]][g[u][i]]=true;
d[u+g[u][i]][g[u][i]]=d[u][v]+1;
Q.push(ii(u+g[u][i],g[u][i]));
}
}
}
if(u-v>=1&&Free[u-v][v]==false)
{
Free[u-v][v]=true;
d[u-v][v]=d[u][v]+1;
Q.push(ii(u-v,v));
}
if(u+v<=n&&Free[u+v][v]==false)
{
Free[u+v][v]=true;
d[u+v][v]=d[u][v]+1;
Q.push(ii(u+v,v));
}
}
cout<<-1;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:25:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<g[S].size(); i++)
~^~~~~~~~~~~~
skyscraper.cpp:43:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<g[u].size(); i++)
~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1020 KB |
Output is correct |
2 |
Correct |
2 ms |
1124 KB |
Output is correct |
3 |
Correct |
2 ms |
1124 KB |
Output is correct |
4 |
Correct |
2 ms |
1124 KB |
Output is correct |
5 |
Correct |
2 ms |
1124 KB |
Output is correct |
6 |
Correct |
2 ms |
1188 KB |
Output is correct |
7 |
Correct |
3 ms |
1192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1256 KB |
Output is correct |
2 |
Correct |
2 ms |
1256 KB |
Output is correct |
3 |
Correct |
3 ms |
1256 KB |
Output is correct |
4 |
Correct |
2 ms |
1256 KB |
Output is correct |
5 |
Correct |
3 ms |
1256 KB |
Output is correct |
6 |
Correct |
2 ms |
1256 KB |
Output is correct |
7 |
Correct |
2 ms |
1468 KB |
Output is correct |
8 |
Correct |
2 ms |
1468 KB |
Output is correct |
9 |
Correct |
2 ms |
1488 KB |
Output is correct |
10 |
Correct |
3 ms |
1748 KB |
Output is correct |
11 |
Correct |
4 ms |
1752 KB |
Output is correct |
12 |
Correct |
3 ms |
2024 KB |
Output is correct |
13 |
Correct |
3 ms |
2024 KB |
Output is correct |
14 |
Correct |
3 ms |
2024 KB |
Output is correct |
15 |
Correct |
4 ms |
2076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2076 KB |
Output is correct |
2 |
Correct |
2 ms |
2076 KB |
Output is correct |
3 |
Correct |
2 ms |
2076 KB |
Output is correct |
4 |
Correct |
2 ms |
2076 KB |
Output is correct |
5 |
Correct |
2 ms |
2076 KB |
Output is correct |
6 |
Correct |
2 ms |
2076 KB |
Output is correct |
7 |
Correct |
3 ms |
2076 KB |
Output is correct |
8 |
Correct |
2 ms |
2076 KB |
Output is correct |
9 |
Correct |
2 ms |
2076 KB |
Output is correct |
10 |
Correct |
3 ms |
2076 KB |
Output is correct |
11 |
Correct |
3 ms |
2076 KB |
Output is correct |
12 |
Correct |
3 ms |
2076 KB |
Output is correct |
13 |
Correct |
3 ms |
2088 KB |
Output is correct |
14 |
Correct |
3 ms |
2100 KB |
Output is correct |
15 |
Correct |
3 ms |
2200 KB |
Output is correct |
16 |
Correct |
3 ms |
2200 KB |
Output is correct |
17 |
Correct |
7 ms |
6612 KB |
Output is correct |
18 |
Correct |
4 ms |
6612 KB |
Output is correct |
19 |
Correct |
3 ms |
6612 KB |
Output is correct |
20 |
Correct |
13 ms |
13560 KB |
Output is correct |
21 |
Correct |
3 ms |
13560 KB |
Output is correct |
22 |
Correct |
3 ms |
13560 KB |
Output is correct |
23 |
Correct |
11 ms |
13560 KB |
Output is correct |
24 |
Correct |
14 ms |
15912 KB |
Output is correct |
25 |
Correct |
7 ms |
15912 KB |
Output is correct |
26 |
Correct |
12 ms |
15912 KB |
Output is correct |
27 |
Correct |
13 ms |
15912 KB |
Output is correct |
28 |
Correct |
16 ms |
15912 KB |
Output is correct |
29 |
Correct |
18 ms |
15912 KB |
Output is correct |
30 |
Correct |
13 ms |
15912 KB |
Output is correct |
31 |
Correct |
15 ms |
15912 KB |
Output is correct |
32 |
Correct |
14 ms |
15912 KB |
Output is correct |
33 |
Correct |
17 ms |
15912 KB |
Output is correct |
34 |
Correct |
15 ms |
15912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
15912 KB |
Output is correct |
2 |
Correct |
2 ms |
15912 KB |
Output is correct |
3 |
Correct |
2 ms |
15912 KB |
Output is correct |
4 |
Correct |
3 ms |
15912 KB |
Output is correct |
5 |
Correct |
2 ms |
15912 KB |
Output is correct |
6 |
Correct |
3 ms |
15912 KB |
Output is correct |
7 |
Correct |
2 ms |
15912 KB |
Output is correct |
8 |
Correct |
3 ms |
15912 KB |
Output is correct |
9 |
Correct |
2 ms |
15912 KB |
Output is correct |
10 |
Correct |
3 ms |
15912 KB |
Output is correct |
11 |
Correct |
4 ms |
15912 KB |
Output is correct |
12 |
Correct |
3 ms |
15912 KB |
Output is correct |
13 |
Correct |
3 ms |
15912 KB |
Output is correct |
14 |
Correct |
3 ms |
15912 KB |
Output is correct |
15 |
Correct |
3 ms |
15912 KB |
Output is correct |
16 |
Correct |
4 ms |
15912 KB |
Output is correct |
17 |
Correct |
7 ms |
15912 KB |
Output is correct |
18 |
Correct |
3 ms |
15912 KB |
Output is correct |
19 |
Correct |
3 ms |
15912 KB |
Output is correct |
20 |
Correct |
14 ms |
15912 KB |
Output is correct |
21 |
Correct |
3 ms |
15912 KB |
Output is correct |
22 |
Correct |
3 ms |
15912 KB |
Output is correct |
23 |
Correct |
12 ms |
15912 KB |
Output is correct |
24 |
Correct |
15 ms |
15912 KB |
Output is correct |
25 |
Correct |
5 ms |
15912 KB |
Output is correct |
26 |
Correct |
11 ms |
15912 KB |
Output is correct |
27 |
Correct |
15 ms |
15912 KB |
Output is correct |
28 |
Correct |
16 ms |
15912 KB |
Output is correct |
29 |
Correct |
14 ms |
15912 KB |
Output is correct |
30 |
Correct |
16 ms |
15912 KB |
Output is correct |
31 |
Correct |
15 ms |
15912 KB |
Output is correct |
32 |
Correct |
14 ms |
15912 KB |
Output is correct |
33 |
Correct |
17 ms |
15912 KB |
Output is correct |
34 |
Correct |
16 ms |
15912 KB |
Output is correct |
35 |
Correct |
24 ms |
15912 KB |
Output is correct |
36 |
Correct |
9 ms |
15912 KB |
Output is correct |
37 |
Correct |
31 ms |
19140 KB |
Output is correct |
38 |
Correct |
31 ms |
21464 KB |
Output is correct |
39 |
Correct |
11 ms |
21464 KB |
Output is correct |
40 |
Correct |
19 ms |
21464 KB |
Output is correct |
41 |
Correct |
22 ms |
21464 KB |
Output is correct |
42 |
Correct |
18 ms |
21464 KB |
Output is correct |
43 |
Correct |
18 ms |
21464 KB |
Output is correct |
44 |
Correct |
23 ms |
21464 KB |
Output is correct |
45 |
Correct |
44 ms |
21464 KB |
Output is correct |
46 |
Correct |
32 ms |
21464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
21464 KB |
Output is correct |
2 |
Correct |
2 ms |
21464 KB |
Output is correct |
3 |
Correct |
3 ms |
21464 KB |
Output is correct |
4 |
Correct |
3 ms |
21464 KB |
Output is correct |
5 |
Correct |
2 ms |
21464 KB |
Output is correct |
6 |
Correct |
3 ms |
21464 KB |
Output is correct |
7 |
Correct |
3 ms |
21464 KB |
Output is correct |
8 |
Correct |
3 ms |
21464 KB |
Output is correct |
9 |
Correct |
3 ms |
21464 KB |
Output is correct |
10 |
Correct |
3 ms |
21464 KB |
Output is correct |
11 |
Correct |
3 ms |
21464 KB |
Output is correct |
12 |
Correct |
4 ms |
21464 KB |
Output is correct |
13 |
Correct |
4 ms |
21464 KB |
Output is correct |
14 |
Correct |
4 ms |
21464 KB |
Output is correct |
15 |
Correct |
3 ms |
21464 KB |
Output is correct |
16 |
Correct |
3 ms |
21464 KB |
Output is correct |
17 |
Correct |
8 ms |
21464 KB |
Output is correct |
18 |
Correct |
3 ms |
21464 KB |
Output is correct |
19 |
Correct |
3 ms |
21464 KB |
Output is correct |
20 |
Correct |
13 ms |
21464 KB |
Output is correct |
21 |
Correct |
3 ms |
21464 KB |
Output is correct |
22 |
Correct |
3 ms |
21464 KB |
Output is correct |
23 |
Correct |
11 ms |
21464 KB |
Output is correct |
24 |
Correct |
15 ms |
21464 KB |
Output is correct |
25 |
Correct |
7 ms |
21464 KB |
Output is correct |
26 |
Correct |
14 ms |
21464 KB |
Output is correct |
27 |
Correct |
12 ms |
21464 KB |
Output is correct |
28 |
Correct |
18 ms |
21464 KB |
Output is correct |
29 |
Correct |
16 ms |
21464 KB |
Output is correct |
30 |
Correct |
13 ms |
21464 KB |
Output is correct |
31 |
Correct |
13 ms |
21464 KB |
Output is correct |
32 |
Correct |
14 ms |
21464 KB |
Output is correct |
33 |
Correct |
18 ms |
21464 KB |
Output is correct |
34 |
Correct |
20 ms |
21464 KB |
Output is correct |
35 |
Correct |
23 ms |
21464 KB |
Output is correct |
36 |
Correct |
7 ms |
21464 KB |
Output is correct |
37 |
Correct |
23 ms |
21464 KB |
Output is correct |
38 |
Correct |
32 ms |
21464 KB |
Output is correct |
39 |
Correct |
11 ms |
21464 KB |
Output is correct |
40 |
Correct |
16 ms |
21464 KB |
Output is correct |
41 |
Correct |
22 ms |
21464 KB |
Output is correct |
42 |
Correct |
20 ms |
21464 KB |
Output is correct |
43 |
Correct |
18 ms |
21464 KB |
Output is correct |
44 |
Correct |
18 ms |
21464 KB |
Output is correct |
45 |
Correct |
38 ms |
21464 KB |
Output is correct |
46 |
Correct |
28 ms |
21464 KB |
Output is correct |
47 |
Runtime error |
11 ms |
21464 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
48 |
Halted |
0 ms |
0 KB |
- |