이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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[30000],p[30000];
vector<pair<int,int>> grph[(k+1)*30000+69];
int done[(k+1)*30000+69];
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 = 1; i <= k; i++) {
for (int j = 0; j < n-i; j++) {
grph[j+i*30000].push_back({1,j+i+i*30000});
grph[j+i+i*30000].push_back({1,j+i*30000});
}
for (int j = 0; j < n; j++) {
grph[j+i*30000].push_back({0,j});
}
}
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]*30000});
}
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;
}
for (auto i : grph[temp.s]) {
if (!done[i.s]) {
pq.push({temp.f+i.f,i.s});
}
}
}
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... |