This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 100
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];
bool done[30069];
set<pair<int,int>> st;
vector<int> grph[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];
grph[b[i]].push_back(p[i]);
}
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;
}
for (int i:grph[temp.s]) {
if (st.find({temp.s%i,i})!=st.end()) {
continue;
}
st.insert({temp.s%i,i});
for (int j = temp.s%i; j < n; j+=i) {
pq.push({temp.f+abs(j-temp.s)/i,j});
}
}
}
cout<<-1<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |