Submission #486539

#TimeUsernameProblemLanguageResultExecution timeMemory
486539fun_dayJakarta Skyscrapers (APIO15_skyscraper)C++14
10 / 100
121 ms220748 KiB
#include <bits/stdc++.h>

using namespace std;

string to_string(string s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

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;
  bitset<N> vis[N][2];
  int s = -1 , d = -1;
  for(int i = 0 ; i < m ; i++){
    int a , b;
    cin >> a >> b;
    if(s == -1) s = a;
    else if(d == -1) d = a;
    if(vis[a][b][0] && vis[a][b][1]) continue;
    vis[a][b][0] = true; vis[a][b][1] = true;
    for(int j = a + b , cnt = 1 ; j < n ; j += b , cnt++){
      adj[a].emplace_back(j , cnt);
      if(vis[j][b][1]) break;
      vis[j][b][1] = true;
    }
    for(int j = a - b , cnt = 1 ; j >= 0 ; j -= b , cnt++){
      adj[a].emplace_back(j , cnt);
      if(vis[j][b][0]) break;
      vis[j][b][0] = true;
    }
  }
  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...