#include <bits/stdc++.h>
using namespace std;
const int lim=1e3;
typedef pair<int,int> ii;
int n,m,S,T,d[30005][1005];
bool Visited[30005],Free[30005][1005];
map<int,int> dd[30005];
map<int,bool> FFree[30005];
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++)
{
if(g[S][i]<=lim)
Free[S][g[S][i]]=true;
else
FFree[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;
int w;
if(v<=lim)
w=d[u][v];
else
w=dd[u][v];
Q.pop();
if(u==T)
{
cout<<w;
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)
{
if(g[u][i]<=lim)
{
Free[u-g[u][i]][g[u][i]]=true;
d[u-g[u][i]][g[u][i]]=w+1;
}
else
{
FFree[u-g[u][i]][g[u][i]]=true;
dd[u-g[u][i]][g[u][i]]=w+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)
{
if(g[u][i]<=lim)
{
Free[u+g[u][i]][g[u][i]]=true;
d[u+g[u][i]][g[u][i]]=w+1;
}
else
{
FFree[u+g[u][i]][g[u][i]]=true;
dd[u+g[u][i]][g[u][i]]=w+1;
}
Q.push(ii(u+g[u][i],g[u][i]));
}
}
}
if(u-v>=1&&Free[u-v][v]==false)
{
if(v<=lim)
{
Free[u-v][v]=true;
d[u-v][v]=w+1;
}
else
{
FFree[u-v][v]=true;
dd[u-v][v]=w+1;
}
Q.push(ii(u-v,v));
}
if(u+v<=n&&Free[u+v][v]==false)
{
if(v<=lim)
{
Free[u+v][v]=true;
d[u+v][v]=w+1;
}
else
{
FFree[u+v][v]=true;
dd[u+v][v]=w+1;
}
Q.push(ii(u+v,v));
}
}
cout<<-1;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:28:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<g[S].size(); i++)
~^~~~~~~~~~~~
skyscraper.cpp:54: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 |
4 ms |
3836 KB |
Output is correct |
2 |
Correct |
4 ms |
3944 KB |
Output is correct |
3 |
Correct |
4 ms |
3944 KB |
Output is correct |
4 |
Correct |
5 ms |
3944 KB |
Output is correct |
5 |
Correct |
4 ms |
4044 KB |
Output is correct |
6 |
Correct |
5 ms |
4096 KB |
Output is correct |
7 |
Correct |
4 ms |
4112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4140 KB |
Output is correct |
2 |
Correct |
4 ms |
4160 KB |
Output is correct |
3 |
Correct |
5 ms |
4160 KB |
Output is correct |
4 |
Correct |
4 ms |
4160 KB |
Output is correct |
5 |
Correct |
5 ms |
4160 KB |
Output is correct |
6 |
Correct |
4 ms |
4160 KB |
Output is correct |
7 |
Correct |
4 ms |
4176 KB |
Output is correct |
8 |
Correct |
4 ms |
4176 KB |
Output is correct |
9 |
Correct |
4 ms |
4348 KB |
Output is correct |
10 |
Correct |
5 ms |
4604 KB |
Output is correct |
11 |
Correct |
5 ms |
4604 KB |
Output is correct |
12 |
Correct |
5 ms |
4604 KB |
Output is correct |
13 |
Correct |
5 ms |
4604 KB |
Output is correct |
14 |
Correct |
5 ms |
4604 KB |
Output is correct |
15 |
Correct |
6 ms |
4604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4604 KB |
Output is correct |
2 |
Correct |
4 ms |
4604 KB |
Output is correct |
3 |
Correct |
5 ms |
4604 KB |
Output is correct |
4 |
Correct |
5 ms |
4604 KB |
Output is correct |
5 |
Correct |
22 ms |
4604 KB |
Output is correct |
6 |
Correct |
4 ms |
4604 KB |
Output is correct |
7 |
Correct |
4 ms |
4604 KB |
Output is correct |
8 |
Correct |
4 ms |
4604 KB |
Output is correct |
9 |
Correct |
4 ms |
4604 KB |
Output is correct |
10 |
Correct |
6 ms |
4604 KB |
Output is correct |
11 |
Correct |
5 ms |
4604 KB |
Output is correct |
12 |
Correct |
5 ms |
4604 KB |
Output is correct |
13 |
Correct |
6 ms |
4608 KB |
Output is correct |
14 |
Correct |
5 ms |
4608 KB |
Output is correct |
15 |
Correct |
5 ms |
4608 KB |
Output is correct |
16 |
Correct |
4 ms |
4608 KB |
Output is correct |
17 |
Correct |
8 ms |
7804 KB |
Output is correct |
18 |
Correct |
5 ms |
7804 KB |
Output is correct |
19 |
Execution timed out |
1077 ms |
7804 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7804 KB |
Output is correct |
2 |
Correct |
4 ms |
7804 KB |
Output is correct |
3 |
Correct |
4 ms |
7804 KB |
Output is correct |
4 |
Correct |
5 ms |
7804 KB |
Output is correct |
5 |
Correct |
4 ms |
7804 KB |
Output is correct |
6 |
Correct |
4 ms |
7804 KB |
Output is correct |
7 |
Correct |
4 ms |
7804 KB |
Output is correct |
8 |
Correct |
5 ms |
7804 KB |
Output is correct |
9 |
Correct |
5 ms |
7804 KB |
Output is correct |
10 |
Correct |
5 ms |
7804 KB |
Output is correct |
11 |
Correct |
5 ms |
7804 KB |
Output is correct |
12 |
Correct |
5 ms |
7804 KB |
Output is correct |
13 |
Correct |
5 ms |
7804 KB |
Output is correct |
14 |
Correct |
5 ms |
7804 KB |
Output is correct |
15 |
Correct |
5 ms |
7804 KB |
Output is correct |
16 |
Correct |
5 ms |
7804 KB |
Output is correct |
17 |
Correct |
8 ms |
7804 KB |
Output is correct |
18 |
Correct |
5 ms |
7804 KB |
Output is correct |
19 |
Execution timed out |
1069 ms |
7804 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7804 KB |
Output is correct |
2 |
Correct |
5 ms |
7804 KB |
Output is correct |
3 |
Correct |
4 ms |
7804 KB |
Output is correct |
4 |
Correct |
4 ms |
7804 KB |
Output is correct |
5 |
Correct |
4 ms |
7804 KB |
Output is correct |
6 |
Correct |
4 ms |
7804 KB |
Output is correct |
7 |
Correct |
5 ms |
7804 KB |
Output is correct |
8 |
Correct |
5 ms |
7804 KB |
Output is correct |
9 |
Correct |
4 ms |
7804 KB |
Output is correct |
10 |
Correct |
6 ms |
7804 KB |
Output is correct |
11 |
Correct |
5 ms |
7804 KB |
Output is correct |
12 |
Correct |
5 ms |
7804 KB |
Output is correct |
13 |
Correct |
5 ms |
7804 KB |
Output is correct |
14 |
Correct |
6 ms |
7804 KB |
Output is correct |
15 |
Correct |
5 ms |
7804 KB |
Output is correct |
16 |
Correct |
5 ms |
7804 KB |
Output is correct |
17 |
Correct |
8 ms |
7804 KB |
Output is correct |
18 |
Correct |
5 ms |
7804 KB |
Output is correct |
19 |
Execution timed out |
1071 ms |
7804 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |