Submission #396449

#TimeUsernameProblemLanguageResultExecution timeMemory
396449loctildoreJakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
1102 ms52476 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define x first
#define y second
#define elif else if
#define endl '\n'
#define all(x) begin(x), end(x)
#define k 173
template <typename T>
ostream& operator << (ostream& os, const vector<T>& a) {
    os << '[';
    bool E = false;
    for(const T& x : a) {
        if(E) os << ' ';
        os << x;
        E = true;
    }
    return os << ']';
}
int n,m;
int b[30069],p[30069];
vector<pair<int,int>> grph[30069];
int done[(k+1)*30069];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin>>n>>m;
  for (int i = 0; i < m; i++) {
    cin>>b[i]>>p[i];
    if (p[i]<=k) {
      grph[b[i]].push_back({0,b[i]+p[i]*30069});
    }
    else{
      for (int j = b[i]%p[i]; j < n; j+=p[i]) {
        grph[b[i]].push_back({abs(b[i]-j)/p[i],j});
      }
    }
  }
  pq.push({0,b[0]});
  while (pq.size()) {
    auto temp=pq.top();
    pq.pop();
    if (done[temp.s]) {
      continue;
    }
    done[temp.s]=true;
    if (temp.s==b[1]) {
      cout<<temp.f<<endl;
      return 0;
    }
    if (temp.s<30069) {
      for (auto i : grph[temp.s]) {
        if (!done[i.s]) {
          pq.push({temp.f+i.f,i.s});
        }
      }
    }
    else{
      int i=temp.s%30069,j=temp.s/30069;
      pq.push({temp.f,i});
      if (i>=j) {
        pq.push({temp.f+1,temp.s-j});
      }
      if (i+j<n) {
        pq.push({temp.f+1,temp.s+j});
      }
    }
  }
  cout<<-1<<endl;
  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...