Submission #622438

#TimeUsernameProblemLanguageResultExecution timeMemory
622438socpiteJakarta Skyscrapers (APIO15_skyscraper)C++14
10 / 100
1 ms1108 KiB
#include<bits/stdc++.h>
using namespace std;

#define f first
#define s second

typedef long long ll;

const int maxn = 3e4+5;
const int inf = 1e9;

map<int, int> mp;
queue<int> q;
vector<pair<int, int>> vec[maxn];
int passed[maxn];

int n, m;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //ifstream cin("input.txt");
    cin >> n >> m;
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        vec[a].push_back({b, i});
        if(!i){
            mp[a*maxn+b]=0;
            q.push(a*maxn+b);
        }
    }
    int ans = -1;
    while(!q.empty()){
        auto x = q.front();
        q.pop();
        int a = x/maxn, b = x%maxn, val = mp[x], id;
        if(!passed[a]){
            passed[a]=1;
            for(auto v: vec[a]){
                id = a*maxn+v.f;
                if(v.s == 1)ans = val;
                if(mp.find(id) == mp.end()){
                    mp[id]=val;
                    q.push(id);
                }
            }
        }
        if(a-b >= 0){
            id = (a-b)*maxn + b;
            if(mp.find(id) == mp.end()){
                mp[id]=val+1;
                q.push(id);
            }
        }
        if(a+b < n){
            id = (a+b)*maxn + b;
            if(mp.find(id) == mp.end()){
                mp[id]=val+1;
                q.push(id);
            }
        }
    }
    cout << ans;
}
#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...