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 pii pair<int, int>
#define ft first
#define sd second
#define MP make_pair
#define PB push_back
#define sz(x) ((int)x.size())
using namespace std;
typedef long long ll;
const ll PW = 43;
const int N = 2010;
const int oo = 2e9;
set<pii> st;
vector<int> g[N];
int n, m, bg, dst[N], ed;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("in.txt","r",stdin);
cin >> n >> m;
for (int i = 0; i < m; i++){
int ps, vl;
if (i == 0)
bg = i;
if (i == 1)
ed = i;
cin >> ps >> vl;
g[ps].PB(vl);
}
for (int i = 0; i < n; i++)
dst[i] = oo;
dst[bg] = 0;
st.insert(MP(0, bg));
while (sz(st)){
int cur = (*st.begin()).sd;
st.erase(st.begin());
for (int cr : g[cur]){
int steps = dst[cur];
for (int i = cur; i >= 0; i -= cr, steps++){
if (steps < dst[i]){
st.erase(MP(dst[i], i));
dst[i] = steps;
st.insert(MP(dst[i], i));
}
}
steps = dst[cur];
for (int i = cur; i < n; i += cr, steps++){
if (steps < dst[i]){
st.erase(MP(dst[i], i));
dst[i] = steps;
st.insert(MP(dst[i], i));
}
}
}
}
if (dst[ed] == oo) return -1;
cout << (dst[ed] == oo ? -1 : dst[ed]);
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... |