Submission #931626

#TimeUsernameProblemLanguageResultExecution timeMemory
931626dnialhJakarta Skyscrapers (APIO15_skyscraper)C++17
57 / 100
1063 ms57224 KiB
#define set unordered_set
 
#include <iostream>
#include <vector>
#include <set>
using namespace std;
 
typedef long long ll;
 
int main() {ios::sync_with_stdio(0); cin.tie(0);
    int n, m;
    cin >> n >> m;
 
    vector<vector<int>> adj(n);
    vector<int> sta;
    vector<int> power;
 
    for (int i = 0; i < m; ++i) {
        int bi, pi;
        cin >> bi >> pi;
        adj[bi].push_back(i);
        sta.push_back(bi);
        power.push_back(pi);
    }
 
    vector<int> seen(n, 0);
    set<ll> par;
    int amt = 0;
    vector<pair<int, int>> st;
    vector<pair<int, int>> nex;
    st.emplace_back(sta[0], 0);
 
    while (!seen[sta[1]]) {
        /*
        auto add = [&](ll u, int v) {
            if (0 <= u && u < n) {
                long long ind = u << 32LL | v;
                if (par.find(ind) == par.end()) {
                    par.insert(ind);
                    nex.push_back({u, v});
                }
            }
        };
        */
 
        while (!st.empty()) {
            auto [loc, p] = st.back();
            st.pop_back();
            if (!seen[loc]) {
                seen[loc] = 1;
                for (int u : adj[loc]) {
                    long long ind = (1LL * loc) << 32LL | (power[u]);
                    if (par.find(ind) == par.end()) {
                        par.insert(ind);
                        st.push_back({loc, power[u]});
                    }
                }
            }
 
            ll u = loc + p;
            if (0 <= u && u < n) {
                long long ind = (u << 32LL) | p;
                if (par.find(ind) == par.end()) {
                    par.insert(ind);
                    nex.push_back({u, p});
                }
            }
            
            u = loc - p;
            if (0 <= u && u < n) {
                long long ind = (u << 32LL) | p;
                if (par.find(ind) == par.end()) {
                    par.insert(ind);
                    nex.push_back({u, p});
                }
            }
        }
 
        amt += 1;
        st.swap(nex);
        nex.clear();
 
        if (st.empty() && !seen[sta[1]]) {
            cout << -1 << endl;
            exit(0);
        }
    }
 
    cout << amt - 1 << endl;
 
    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...