This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = 30010;
const int MX = 1e6;
const int inf = (int)1e9;
int dist[N];
vector<int> id[MX];
vector<int> q[N];
int main(){
fastIO;
int n, m;
cin >> n >> m;
int st = -1, en = -1;
int xi, yi;
for(int i = 0 ; i < n; i ++ ){
dist[i] = inf;
}
for(int i = 0 ; i < m; i ++ ){
cin >> xi >> yi;
q[xi].push_back(yi);
if(i == 0){
st = xi;
}
else if(i == 1){
en = xi;
}
}
id[0].push_back(st);
dist[st] = 0;
for(int i = 0 ; i < MX; i ++ ){
if(id[i].empty()) continue;
for(auto x : id[i]){ // this just adds +n so its fine
if(x == en){
cout << i << "\n";
return 0;
}
}
for(auto x : id[i]){
if(dist[x] != i) continue;
for(auto y : q[x]){
for(int f = 1; x + y * f < n && i + f < MX; f ++ ){
if(dist[x + y * f] > i + f){
dist[x + y * f] = i + f;
id[i + f].push_back(x + y * f);
}
}
for(int f = 1; x - y * f >= 0 && i + f < MX; f ++ ){
if(dist[x - y * f] > i + f){
dist[x - y * f] = i + f;
id[i + f].push_back(x - y * f);
}
}
}
}
}
cout << "-1\n";
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... |