# include <bits/stdc++.h>
using namespace std;
const int N = 2000;
int n, m, b[N], p[N], d[N][N], dis[N];
vector < pair <int, int> > g[N];
int main(){
cin >> n >> m;
for(int i = 0; i < N; i ++){
dis[i] = 1e9;
for(int j = 0; j < N; j ++)
d[i][j] = 1e9;
}
if(n > N || m > N) return 0;
for(int i = 1; i <= m; i ++){
cin >> b[i] >> p[i];
d[i][b[i]] = 0;
for(int j = b[i]; j + p[i] < n; j += p[i])
d[i][j + p[i]] = d[i][j] + 1;
for(int j = b[i]; j - p[i] >= 0; j -= p[i])
d[i][j - p[i]] = d[i][j] + 1;
}
for(int i = 1; i <= m; i ++){
for(int j = 1; j <= m; j ++){
if(j == i) continue;
if(d[i][b[j]] != 1e9){
g[i].push_back({d[i][b[j]], j});
}
}
}
set < pair <int, int> > st;
dis[1] = 0;
st.insert({0, 1});
while(!st.empty()){
int v = st.begin()->second;
st.erase(st.begin());
for(int i = 0; i < g[v].size(); i ++){
int to = g[v][i].second, len = g[v][i].first;
if(dis[to] > dis[v] + len){
st.erase({dis[to], to});
dis[to] = dis[v] + len;
st.insert({dis[to], to});
}
}
}
if(dis[2] == 1e9)
cout << -1 << endl;
else
cout << dis[2] << endl;
}
/**
5 3
0 2
1 1
4 1
**/
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:46:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < g[v].size(); i ++){
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
15992 KB |
Output is correct |
2 |
Correct |
15 ms |
16104 KB |
Output is correct |
3 |
Correct |
14 ms |
16180 KB |
Output is correct |
4 |
Correct |
14 ms |
16232 KB |
Output is correct |
5 |
Correct |
14 ms |
16232 KB |
Output is correct |
6 |
Correct |
14 ms |
16232 KB |
Output is correct |
7 |
Correct |
17 ms |
16232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
16232 KB |
Output is correct |
2 |
Correct |
14 ms |
16232 KB |
Output is correct |
3 |
Correct |
15 ms |
16296 KB |
Output is correct |
4 |
Correct |
15 ms |
16296 KB |
Output is correct |
5 |
Correct |
14 ms |
16364 KB |
Output is correct |
6 |
Correct |
18 ms |
16380 KB |
Output is correct |
7 |
Correct |
13 ms |
16380 KB |
Output is correct |
8 |
Correct |
14 ms |
16380 KB |
Output is correct |
9 |
Correct |
15 ms |
16380 KB |
Output is correct |
10 |
Correct |
15 ms |
16384 KB |
Output is correct |
11 |
Incorrect |
16 ms |
16384 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
16384 KB |
Output is correct |
2 |
Correct |
13 ms |
16384 KB |
Output is correct |
3 |
Correct |
15 ms |
16384 KB |
Output is correct |
4 |
Correct |
14 ms |
16384 KB |
Output is correct |
5 |
Correct |
14 ms |
16384 KB |
Output is correct |
6 |
Correct |
13 ms |
16384 KB |
Output is correct |
7 |
Correct |
14 ms |
16384 KB |
Output is correct |
8 |
Correct |
13 ms |
16384 KB |
Output is correct |
9 |
Correct |
14 ms |
16384 KB |
Output is correct |
10 |
Correct |
14 ms |
16404 KB |
Output is correct |
11 |
Incorrect |
14 ms |
16404 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
16404 KB |
Output is correct |
2 |
Correct |
14 ms |
16404 KB |
Output is correct |
3 |
Correct |
14 ms |
16404 KB |
Output is correct |
4 |
Correct |
17 ms |
16404 KB |
Output is correct |
5 |
Correct |
14 ms |
16404 KB |
Output is correct |
6 |
Correct |
14 ms |
16404 KB |
Output is correct |
7 |
Correct |
17 ms |
16404 KB |
Output is correct |
8 |
Correct |
14 ms |
16404 KB |
Output is correct |
9 |
Correct |
14 ms |
16404 KB |
Output is correct |
10 |
Correct |
19 ms |
16420 KB |
Output is correct |
11 |
Incorrect |
15 ms |
16420 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
16420 KB |
Output is correct |
2 |
Correct |
13 ms |
16420 KB |
Output is correct |
3 |
Correct |
14 ms |
16420 KB |
Output is correct |
4 |
Correct |
13 ms |
16420 KB |
Output is correct |
5 |
Correct |
14 ms |
16420 KB |
Output is correct |
6 |
Correct |
14 ms |
16420 KB |
Output is correct |
7 |
Correct |
13 ms |
16420 KB |
Output is correct |
8 |
Correct |
15 ms |
16420 KB |
Output is correct |
9 |
Correct |
13 ms |
16420 KB |
Output is correct |
10 |
Correct |
15 ms |
16420 KB |
Output is correct |
11 |
Incorrect |
16 ms |
16420 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |