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>
#define int long long
#define INF 1000000000000000
#define lchild(i) (i*2 + 1)
#define rchild(i) (i*2 + 2)
#define mid(l, u) ((l+u)/2)
#define x(p) p.first
#define y(p) p.second
#define MOD 998244353
int n, m;
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
int s = 0, t= 0;
vector<int> adj[n];
map<pair<int, int>, bool> ez;
for(int i = 0;i<m;i++){
int b, p;
cin>>b>>p;
if(!ez[{b, p}]) adj[b].push_back(p);
ez[{b, p}] = true;
if(i==0) s = b;
if(i==1) t = b;
}
int dist[n];
for(int i = 0;i<n;i++) dist[i] = INF;
dist[s] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s});
while(!pq.empty()){
pair<int, int> curr = pq.top();
pq.pop();
if(curr.first != dist[curr.second]) continue;
for(int j: adj[curr.second]){
int len = 0;
for(int i = curr.second + j;i<n;i+=j){
len++;
if(curr.first + len < dist[i]){
dist[i] = curr.first + len;
pq.push({dist[i], i});
}
if(ez[{i, j}]) goto second;
}
second:
len = 0;
for(int i = curr.second - j;i>=0;i-=j){
len++;
if(curr.first + len < dist[i]){
dist[i] = curr.first + len;
pq.push({dist[i], i});
}
if(ez[{i, j}]) goto end;
}
end:
continue;
}
}
cout<<dist[t]<<"\n";
}
# | 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... |