Submission #695192

#TimeUsernameProblemLanguageResultExecution timeMemory
695192NeroZeinJakarta Skyscrapers (APIO15_skyscraper)C++17
36 / 100
252 ms262144 KiB
#include <bits/stdc++.h>
using namespace std; 

const int M = 300004;

int n, m;
int ans;
int b[M];
int p[M];
int cost[M];
vector<pair<int, int>> g[M];

inline void dij() {
  memset(cost, -1, sizeof cost);
  cost[b[0]] = 0; 
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int >>> pq; 
  pq.emplace(0, b[0]);
  while (!pq.empty()) {
    auto src = pq.top();
    pq.pop();
    if (cost[src.second] != src.first) {
      continue;
    }
    if (src.second == b[1]) {
      ans = min(ans, src.first);
    }
    for (auto [u, w] : g[src.second]) {
      if (cost[u] == -1 || cost[u] > src.first + w) {
        cost[u] = src.first + w;
        pq.emplace(cost[u], u);
      }
    }
  }
}

signed main() {
  ans = INT_MAX;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> b[i] >> p[i];
    for (int j = 1; j < n; ++j) {
      int r = b[i] + j * p[i];
      int l = b[i] - j * p[i];
      if (l >= 0){ 
        g[b[i]].emplace_back(l, j);
      }
      if (r < n) {
        g[b[i]].emplace_back(r, j); 
      }
    }
  }
  dij();
  cout << (ans == INT_MAX ? -1 : ans) << '\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...