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;
int n, m, zg[2009], sk[2009], tmp, v[2009][2009];
int reks(int pos, int sum){
if (v[0][pos] == -1 or v[0][pos] > sum) v[0][pos] = sum;
else if (v[0][pos] < sum) return n*n+30;
if (pos == 1){
if (v[0][1] == -1) return v[0][1] = sum;
else return v[0][1] = min(v[0][1], sum);
}
int mini = n*n+20;
for(int i=0; i<m; i++){
if (abs(zg[i] - zg[pos]) % sk[pos] == 0 and i != pos){
if (v[pos][i] == -1) v[pos][i] = abs(zg[i] - zg[pos]) / sk[pos];
if (v[0][i] == -1 or v[0][i] > v[0][pos] + v[pos][i]) v[0][i] = v[0][pos] + v[pos][i];
mini = min(reks(i, sum + v[pos][i]), mini);
}
}
return mini;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; memset(v, -1, sizeof v);
for(int i=0; i<m; i++) cin >> zg[i] >> sk[i];
tmp = reks(0, 0);
if (tmp > n*n) cout << -1;
else cout << tmp;
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... |