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];
bool vis[N];
map<pair<int,int>,vector<int>> mp;
vector<pair<pair<int,int>,int>> extend;
vector<pair<pair<int,int>,int>> extend2;
vector<int> nxt;
int n;
void add(pair<int,int> a,int b){
if(mp[a].empty()){
mp[a].resize(n / a.first + 1);
}
if(mp[a][b])return;
mp[a][b] = 1;
if(b*a.first + a.second < n && !vis[b*a.first + a.second]){
vis[b*a.first + a.second] = 1;
nxt.push_back(b*a.first + a.second);
}
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});
}
void solve(){
int m;
cin >> n >> m;
int st = -1;
int nd = -1;
for(int i=1;i<=m;i++){
int a,b;
cin >> a >> b;
if(st == -1)st = a;
else if(nd == -1)nd = a;
v[a].push_back(b);
}
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({c,u % c},u / 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(std::pair<int, int>, int)':
skyscraper.cpp:21:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | if(b+1 < mp[a].size() && !mp[a][b+1])
| ~~~~^~~~~~~~~~~~~~
# | 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... |