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 I inline void
using ll = long long ;
using ld = long double ;
using namespace std ;
const int N = 1e5 + 7 , mod = 1e9+7 ;
// how interesting!
int n;
int d[N] ;
vector<pair<int , int > > adj[N] ;
bool skys[30001][30001] , ed[30001][30001];
int st , en ;
I dijkstra(){
priority_queue<pair<int , int> > qu ;
qu.push({0 , st}) ;
d[st] = 0 ;
while(qu.size()){
int dst = -qu.top().first ;
int node = qu.top().second ;
qu.pop() ;
for(auto u : adj[node]){
if(d[u.first] == -1 || dst + u.second < d[u.first]){
d[u.first] = dst + u.second ;
qu.push({-d[u.first] , u.first}) ;
}
}
}
}
set<int> sts[N] ;
int main(){
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
//freopen("in.in" , "r" , stdin) ;
memset(d , -1 , sizeof d) ;
int m;
cin >> n >> m;
for(int i = 0 ;i < m;i++){
int a , b;
cin >> a >> b;
if(i == 0)
st = a ;
if(i == 1 )
en = a;
sts[a].insert(i) ;
for(int j = 1 ; a + b * j < n ;j ++){
int cur = a + b * j ;
adj[a].push_back({a + b * j , j }) ;
if(sts[cur].count(b))
break ;
}
for(int j = 1 ; a - b * j >=0 ;j ++){
int cur = a - b * j ;
adj[a].push_back({a - b * j , j }) ;
if(sts[cur].count(b))
break ;
}
}
dijkstra() ;
cout<< d[en] ;
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... |