이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |