#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=3e4+5;
const ll inf=2e18;
int n,m,b[maxN+1],p[maxN+1];
namespace sub2
{
int d[2001][2001];
struct node
{
int u,power;
};
queue<node> q;
void solve()
{
memset(d,-1,sizeof(d));
q.push({b[0],p[0]});
d[b[0]][p[0]]=0;
ll dem=0;
while(!q.empty())
{
node tmp=q.front();
q.pop();
int u=tmp.u,power=tmp.power;
//cout<<u<<" "<<power<<'\n';
if(u==b[1])
{
cout<<d[u][power];
return;
}
if(u+power<n)
{
if(d[u+power][power]==-1)
{
d[u+power][power]=d[u][power]+1;
q.push({u+power,power});
}
}
if(u-power>=0)
{
if(d[u-power][power]==-1)
{
d[u-power][power]=d[u][power]+1;
q.push({u-power,power});
}
}
for(int i=0;i<m;i++)
{
if(b[i]==u)
{
if(d[u][p[i]]==-1)
{
d[u][p[i]]=d[u][power];
q.push({u,p[i]});
}
}
}
}
cout<<-1;
}
}
int main()
{
//freopen("","r",stdin);
//freopen("","w",stdout);
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]>>p[i];
}
sub2::solve();
}
Compilation message
skyscraper.cpp: In function 'void sub2::solve()':
skyscraper.cpp:23:12: warning: unused variable 'dem' [-Wunused-variable]
23 | ll dem=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
6 ms |
15964 KB |
Output is correct |
4 |
Correct |
6 ms |
16112 KB |
Output is correct |
5 |
Correct |
6 ms |
15964 KB |
Output is correct |
6 |
Correct |
6 ms |
16052 KB |
Output is correct |
7 |
Correct |
7 ms |
16052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16084 KB |
Output is correct |
2 |
Correct |
7 ms |
15964 KB |
Output is correct |
3 |
Correct |
8 ms |
15964 KB |
Output is correct |
4 |
Correct |
7 ms |
15964 KB |
Output is correct |
5 |
Correct |
6 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15964 KB |
Output is correct |
7 |
Correct |
7 ms |
15964 KB |
Output is correct |
8 |
Correct |
6 ms |
15964 KB |
Output is correct |
9 |
Incorrect |
7 ms |
15960 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15964 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
10 ms |
15960 KB |
Output is correct |
4 |
Correct |
6 ms |
16140 KB |
Output is correct |
5 |
Correct |
6 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15964 KB |
Output is correct |
7 |
Correct |
6 ms |
16084 KB |
Output is correct |
8 |
Correct |
7 ms |
15964 KB |
Output is correct |
9 |
Incorrect |
6 ms |
15964 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15960 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
6 ms |
16076 KB |
Output is correct |
4 |
Correct |
6 ms |
15920 KB |
Output is correct |
5 |
Correct |
6 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15920 KB |
Output is correct |
7 |
Correct |
7 ms |
15964 KB |
Output is correct |
8 |
Correct |
7 ms |
15936 KB |
Output is correct |
9 |
Incorrect |
6 ms |
16104 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15960 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
6 ms |
15964 KB |
Output is correct |
4 |
Correct |
6 ms |
15964 KB |
Output is correct |
5 |
Correct |
8 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15964 KB |
Output is correct |
7 |
Correct |
8 ms |
15964 KB |
Output is correct |
8 |
Correct |
7 ms |
15964 KB |
Output is correct |
9 |
Incorrect |
7 ms |
16072 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |