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;
const int N=3e4+5;
int n,m,p[N],b[N];
vector<int>vt[N];
int dp[N][104];
struct spm {
int val,i,p;
};
bool operator > (spm x,spm y) {
return x.val>y.val;
}
queue <spm> pq;
void dijkstra() {
int ans = 1e9;
memset(dp,0x3f,sizeof(dp));
pq.push({0,b[0],0});
dp[b[0]][0] = 0;
while (!pq.empty()) {
spm x=pq.front();
int val=x.val,
pw=x.p,
i=x.i;
if (i==b[1])
ans=min(ans,val);
pq.pop();
if(val!=dp[i][pw])
continue;
if(pw==0){
for(auto x:vt[i]){
if(p[x]<=sqrt(n)) {
if (dp[i][p[x]]>val) {
dp[i][p[x]]=val;
pq.push({val,i,p[x]});
}
}
else{
int cnt=0;
for (int j=i;j<n;j+=p[x]){
if (dp[j][0]>val+cnt) {
dp[j][0]=val+cnt;
pq.push({val+cnt,j,0});
}
cnt++;
}
cnt=0;
for (int j=i;j>=0;j-=p[x]) {
if (dp[j][0]>val+cnt) {
dp[j][0]=val+cnt;
pq.push({val+cnt,j,0});
}
cnt++;
}
}
}
}
else {
if(i+pw<n && dp[i+pw][pw]>val+1){
dp[i+pw][pw]=val+1;
pq.push({val+1,i+pw,pw});
}
if (i-pw>=0 && dp[i-pw][pw]>val+1){
dp[i-pw][pw]=val+1;
pq.push({val+1,i-pw,pw});
}
if (dp[i][0]>val){
dp[i][0]=val;
pq.push({val,i,0});
}
}
}
if(ans>=1e9)
ans=-1;
cout<<ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(),cout.tie();
cin>>n>>m;
for (int i=0;i<m;++i) {
cin>>b[i]>>p[i];
vt[b[i]].push_back(i);
}
dijkstra();
}
| # | 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... |