This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
int n, m;
vector<pair<int, int>> g[N];
bitset<30005> vis[30005];
vector<bool> VIS(N);
void solve(){
cin >> n >> m;
int z = 0, aim;
for(int i = 1; i <= m; ++i){
int b, p; cin >> b >> p;
if(i==1) z = b;
if(i==2) aim = b;
g[b].pb({i, p});
}
deque<array<int, 3>> q;
for(auto x: g[z]) q.pb({0, z, x.second}), vis[z][x.second] = 1;
VIS[z] = 1;
while(!q.empty()){
auto v = q.front(); q.pop_front();
// cout << v[0] << ' ' << v[1] << ' ' << v[2] << '\n';
if(!VIS[v[1]]){
VIS[v[1]] = 1;
for(auto x: g[v[1]]){
q.push_front({v[0], v[1], x.second});
vis[v[1]][x.second] = 1;
}
}
if(v[1] == aim){
cout << v[0];
return;
}
if(v[1] - v[2] >= 0 && !vis[v[1] - v[2]][v[2]]){
vis[v[1] - v[2]][v[2]] = 1;
q.pb({v[0] + 1, v[1] - v[2], v[2]});
}
if(v[1] + v[2] < n && !vis[v[1] + v[2]][v[2]]){
vis[v[1] + v[2]][v[2]] = 1;
q.pb({v[0] + 1, v[1] + v[2], v[2]});
}
}
cout << -1;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:59:15: warning: unused variable 'aa' [-Wunused-variable]
59 | int tt = 1, aa;
| ^~
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:39:5: warning: 'aim' may be used uninitialized in this function [-Wmaybe-uninitialized]
39 | if(v[1] == aim){
| ^~
# | 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... |