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 MOEZ ios_base::sync_with_stdio(false);cin.tie(NULL),cout.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define F first
#define S second
#define pb push_back
#define all(v) v.begin() , v.end()
#define eb emplace_back
#define getunique(v) {sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());}
#define mem(d , x) memset(d , x , sizeof(d));
#define sz(x) (int)x.size()
const int N = 2e5+5;
//const ll M = 1e9+7;
//const ll inf = 1e18;
//const ld eps = 1e-6;
int n , id[N] , p[N] , m;
vector<pair<int , int>>adj[N];
int d[N];
int main(){
MOEZ
cin >> n >> m;
for(int i = 0; i < n;i++){
d[i] = 1e8;
}
for(int i = 0; i < m;i++){
cin >> id[i];
cin >> p[i];
int cnt = 1;
for(int j = id[i]+p[i]; j < n;j += p[i]){
adj[id[i]].pb({j , cnt++});
}
cnt = 1;
for(int j = id[i]-p[i]; j >= 0;j -= p[i]){
adj[id[i]].pb({j , cnt++});
}
}
priority_queue<pair<int , int> , vector<pair<int , int>> , greater<>>q;
q.push({id[0] , 0});
d[id[0]] = 0;
while(!q.empty()){
int u = q.top().F , cost = q.top().S;
q.pop();
if(d[u] < cost)continue;
if(u == id[1])break;
for(auto x:adj[u]){
int v = x.F , nc = cost+x.S;
if(nc < d[v]){
d[v] = nc;
q.push({v , nc});
}
}
}
cout << (d[id[1]] == 1e8 ? -1 : d[id[1]]);
}
# | 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... |