This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define N 30005
using namespace std;
vector<int> v[N];
int group[N];
int groupx[N],groupy[N];
bool vis[N];
vector<int> mp[N];
vector<pair<int,int>> extend;
vector<pair<int,int>> extend2;
vector<int> nxt;
int n;
void add(int a,int b){
if(mp[a].empty()){
mp[a].resize(n / groupx[a] + 1);
}
if(mp[a][b])return;
mp[a][b] = 1;
if(b*groupx[a] + groupy[a] < n && !vis[b*groupx[a] + groupy[a]]){
vis[b*groupx[a] + groupy[a]] = 1;
nxt.push_back(b*groupx[a] + groupy[a]);
}
if(b+1 < mp[a].size() && !mp[a][b+1])
extend2.push_back({a,b+1});
if(b-1 >= 0 && !mp[a][b-1])
extend2.push_back({a,b-1});
}
int x[N],y[N];
void solve(){
int m;
cin >> n >> m;
int st = -1;
int nd = -1;
vector<pair<pair<int,int>,int>> compress;
for(int i=1;i<=m;i++){
int x[i],y[i];
cin >> x[i] >> y[i];
if(st == -1)st = x[i];
else if(nd == -1)nd = x[i];
v[x[i]].push_back(i);
compress.push_back({{y[i],x[i]%y[i]},i});
}
sort(compress.begin(),compress.end());
for(int i=0,cnt=0;i<compress.size();i++){
if(i && compress[i].first != compress[i-1].first)
cnt++;
group[compress[i].second] = cnt;
groupx[cnt] = compress[i].first.first;
groupy[cnt] = compress[i].first.second;
}
vector<int> nws;
vis[st] = 1;
nws.push_back(st);
int cnt = 0;
while(nws.size() || extend.size()){
nxt.clear();
extend2.clear();
for(auto u:nws){
if(u == nd){
cout << cnt << endl;
return;
}
for(auto c:v[u]){
add(group[c],u / groupx[group[c]]);
}
}
for(auto u:extend2)
extend.push_back(u);
extend2.clear();
for(auto u:extend){
add(u.first,u.second);
}
for(auto u:extend2)
extend.push_back(u);
swap(nws,nxt);
cnt++;
}
cout << -1 << endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
Compilation message (stderr)
skyscraper.cpp: In function 'void add(int, int)':
skyscraper.cpp:23:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | if(b+1 < mp[a].size() && !mp[a][b+1])
| ~~~~^~~~~~~~~~~~~~
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:44:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i=0,cnt=0;i<compress.size();i++){
| ~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |