제출 #622441

#제출 시각아이디문제언어결과실행 시간메모리
622441socpiteJakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
1088 ms33804 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; deque<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_front(a*maxn+b); } } int ans = -1; while(!q.empty()){ auto x = q.front(); q.pop_front(); 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_front(id); } } } if(a-b >= 0){ id = (a-b)*maxn + b; if(mp.find(id) == mp.end()){ mp[id]=val+1; q.push_back(id); } } if(a+b < n){ id = (a+b)*maxn + b; if(mp.find(id) == mp.end()){ mp[id]=val+1; q.push_back(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...