#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int dist[30005][200];
int vis[30005][200];
vector<int> a[30005];
typedef pair<int,pair<int,int>> pp;
int n, m;
int main()
{
//freopen("input.txt","r",stdin);
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n >> m;
int t;
int k;
for (int i = 0; i < m; i++)
{
int x, y;
cin >> x >> y;
if (i == 0)
k = x;
if (i == 1)
t = x;
a[x].emplace_back(y);
}
int s=sqrt(n);
for (int i = 0; i < n; i++)
for(int j=0 ; j<=s ; j++)
dist[i][j] = INF;
dist[k][0] = 0;
priority_queue<pp,vector<pp>,greater<pp>> Q;
Q.push({0,{k,0}});
while(!Q.empty()){
auto x=Q.top(); Q.pop();
int v=x.second.first;
int w=x.second.second;
int d=x.first;
if(vis[v][w])continue;
vis[v][w]=1;
if(w==0){
for(auto e: a[v]){
if(e>t){
int cnt=0;
for(int i=v+e ; i<n ; i+=e){
cnt++;
if(dist[i][0]>d+cnt){
dist[i][0]=d+cnt;
Q.push({dist[i][0],{i,0}});
}
}
cnt=0;
for(int i=v-e ; i>=0 ;i-=e){
cnt++;
if (dist[i][0] > d + cnt)
{
dist[i][0] = d + cnt;
Q.push({dist[i][0], {i, 0}});
}
}
}
else{
if(dist[v][e]>d){
dist[v][e]=d;
Q.push({d,{v,e}});
}
}
}
}
else{
if(v+w<n && dist[v+w][w]>d+1){
dist[v+w][w]=d+1;
Q.push({dist[v+w][w],{v+w,w}});
}
if(v-w>=0 && dist[v-w][w]>d+1){
dist[v-w][w]=d+1;
Q.push({dist[v-w][w],{v-w,w}});
}
if(dist[v][0]>d){
dist[v][0]=d;
Q.push({d,{v,0}});
}
}
}
if (dist[t][0] == INF)
cout << -1;
else
cout << dist[t][0];
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:89:18: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
89 | if (dist[t][0] == INF)
| ~~~~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
1032 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |