Submission #757774

#TimeUsernameProblemLanguageResultExecution timeMemory
757774taherJakarta Skyscrapers (APIO15_skyscraper)C++17
100 / 100
175 ms67288 KiB
#include <bits/stdc++.h>

using namespace std;

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n , m;
  cin >> n >> m;
  vector<vector<pair<int,int>>> adj(n + 1);
  const int N = 30000 + 10;
  int s = -1 , d = -1;
  set<int> st[N];
  for(int i = 0 ; i < m ; i++){
    int a , b;
    cin >> a >> b;
    if(s == -1) s = a;
    else if(d == -1) d = a;
    st[a].insert(b);
  }
  for(int i = 0 ; i < n ; i++){
    for(auto x : st[i]){
      for(int j = i + x , cnt = 1 ; j < n ; j += x , cnt++){
        adj[i].emplace_back(j , cnt);
        if(st[j].find(x) != st[j].end()) break;
      }
      for(int j = i - x , cnt = 1 ; j >= 0 ; j -= x , cnt++){
        adj[i].emplace_back(j , cnt);
        if(st[j].find(x) != st[j].end()) break;
      }
    }
  }
  for(int i = 0 ; i < n ; i++) st[i].clear();
  priority_queue<pair<int,int> , vector<pair<int,int>> , greater<pair<int,int>>> pq;
  pq.emplace(0 , s);
  vector<int> dist(n + 1 , (int)1e9);
  dist[s] = 0;
  while(!pq.empty()){
    auto f = pq.top();
    pq.pop();
    if(f.second == d) break;
    if(f.first != dist[f.second]) continue;
    for(auto x : adj[f.second]){
      int node = x.first;
      int cost = x.second;
      if(dist[node] > f.first + cost){
        dist[node] = f.first + cost;
        pq.emplace(dist[node] , node);
      }
    }
  }
  if(dist[d] == (int)1e9) cout << -1 << '\n';
  else cout << dist[d] << '\n';
  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...