Submission #389094

#TimeUsernameProblemLanguageResultExecution timeMemory
389094talant117408Jakarta Skyscrapers (APIO15_skyscraper)C++17
100 / 100
447 ms112588 KiB
/*
    Code written by Talant I.D.
*/
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
 
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define OK cout << "OK" << endl;
 
const int mod = 998244353;
 
ll mode(ll a) {
    a %= mod;
    if (a < 0) a += mod;
    return a;
}
 
ll subt(ll a, ll b) {
    return mode(mode(a)-mode(b));
}
 
ll add(ll a, ll b) {
    return mode(mode(a)+mode(b));
}
 
ll mult(ll a, ll b) {
    return mode(mode(a)*mode(b));
}
 
ll binpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b&1) res = mult(res, a);
        a = mult(a, a);
        b >>= 1;
    }
    return res;
}

const int N = 3e4+7;
bitset <N> used, vis[N];
vector <int> powers[N];

int main() {
    do_not_disturb
    
    int n, m;
    cin >> n >> m;
    int b[m], p[m];
    for (int i = 0; i < m; i++) {
		cin >> b[i] >> p[i];
		powers[b[i]].pb(p[i]);
	}
    
    deque <array<int, 3>> K;
    K.pb({b[0], p[0], 0});
    
    while (!K.empty()) {
		auto v = K.front(); K.pop_front();
		vis[v[0]][v[1]] = 1;
		if (v[0] == b[1]) {
			cout << v[2] << endl;
			return 0;
		}
		if (!used[v[0]]) {
			for (auto to : powers[v[0]]) {
				if (vis[v[0]][to]) continue;
				vis[v[0]][to] = 1;
				K.push_front({v[0], to, v[2]});
			}
		}
		used[v[0]] = 1;
		if (v[0]+v[1] < n && vis[v[0]+v[1]][v[1]] == 0) {
			K.pb({v[0]+v[1], v[1], v[2]+1});
		}
		if (v[0]-v[1] >= 0 && vis[v[0]-v[1]][v[1]] == 0) {
			K.pb({v[0]-v[1], v[1], v[2]+1});
		}
	}
	cout << -1;
    
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...