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;
typedef pair<int, int> p;
int n, m, s, t;
vector<int> g[30303];
int dst[30303];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for(int i=0; i<m; i++){
int a, b; cin >> a >> b;
g[a].push_back(b);
if(i == 0) s = a;
else if(i == 1) t = a;
}
memset(dst, 0x3f, sizeof dst);
priority_queue<p> pq;
pq.emplace(0, s); dst[s] = 0;
while(pq.size()){
int now = pq.top().second;
int cst = -pq.top().first;
pq.pop();
if(dst[now] < cst) continue;
for(auto i : g[now]){
for(int j=0; now+i*j<n; j++) if(dst[now+i*j] > cst + j){
dst[now+i*j] = cst + j;
pq.emplace(-(cst+j), now+i*j);
}
for(int j=1; now-i*j>=0; j++) if(dst[now-i*j] > cst + j){
dst[now-i*j] = cst + j;
pq.emplace(-(cst+j), now-i*j);
}
}
}
if(dst[t] == 0x3f3f3f3f) cout << -1;
else cout << dst[t];
}
# | 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... |