Submission #355242

#TimeUsernameProblemLanguageResultExecution timeMemory
355242PetyJakarta Skyscrapers (APIO15_skyscraper)C++14
100 / 100
55 ms3948 KiB
#include <bits/stdc++.h>

using namespace std;

int n, m, dist[30002], poz;
set<int>s[30002];
bool viz[30002];

int main()
{
  cin >> n >> m;
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int> > > q;
  for (int i = 0; i < m; i++) {
    int b, p;
    cin >> b >> p;
    if (i == 1)
      poz = b;
    s[b].insert(p);
    if (i == 0)
      q.push({0, b});
  }
  for (int i = 0; i < n; i++)
    dist[i] = 1e9;
  dist[q.top().second] = 0;
  while (!q.empty()) {
    auto x = q.top();
    q.pop();
    if (dist[x.second] != x.first)
      continue;
    if (x.second == poz)
      break;
    for (auto it : s[x.second]) {
      for (int i = x.second+it; i < n; i += it) {
        if (s[i].size() && dist[i] > dist[x.second] + (i - x.second) / it) {
          dist[i] = dist[x.second] + (i - x.second) / it;
          q.push({dist[i], i});
        }
        if (s[i].count(it))
          break;
      }
      for (int i = x.second-it; i >= 0; i -= it) {
        if (s[i].size() && dist[i] > dist[x.second] + (x.second - i) / it) {
          dist[i] = dist[x.second] + (x.second - i) / it;
          q.push({dist[i], i});
        }
        if (s[i].count(it))
          break;
      }
    }
  }
  if (dist[poz] == 1e9)
    dist[poz] = -1;
  cout << dist[poz];
  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...