#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll INF=1e18+5;
const int MAXN=2005;
const int MAXM=3e4+5;
#define F first
#define S second
vector<pair<int, int>> a(MAXM);
int n, m;
ll dp[MAXM][MAXN];
vector<int> wh(MAXN);
void dfs(int x, int y) { // x je koji covik a y je koja pozicija
for(int i=y+a[x].S; i<n; i+=a[x].S) {
dp[x][i]=min(dp[x][i], dp[x][y]+(i-y)/a[x].S);
if(wh[i]!=-1) {
// cout << "ima na " << i << ' ';
if(dp[wh[i]][i]>dp[x][y]+(i-y)/a[x].S) {
dp[wh[i]][i]=dp[x][y]+(i-y)/a[x].S;
// cout << x << ' ' << " daje " << wh[i] << " na " << i << '\n';
dfs(wh[i], i);
}
// cout << dp[wh[i]][i] << '\n';
}
// cout << i << ' ' << dp[x][i] << " ";
}
for(int i=y-a[x].S; i>=0; i-=a[x].S) {
dp[x][i]=min(dp[x][i], dp[x][y]+(y-i)/a[x].S);
if(wh[i]!=-1) {
if(dp[wh[i]][i]>dp[x][y]+(y-i)/a[x].S) {
dp[wh[i]][i]=dp[x][y]+(y-i)/a[x].S;
// cout << x << ' ' << " daje " << wh[i] << " na " << i << '\n';
dfs(wh[i], i);
}
}
// cout << i << ' ' << dp[x][i] << " ";
}
// cout << '\n';
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
for(int i=0; i<MAXM; i++) {
for(int j=0; j<MAXN; j++) {
dp[i][j]=INF;
}
}
for(int i=0; i<MAXN; i++) {
wh[i]=-1;
}
cin >> n >> m;
for(int i=0; i<m; i++) {
cin >> a[i].F >> a[i].S; // pos (b), power
wh[a[i].F]=i;
}
dp[0][a[0].F]=0;
dfs(0, a[0].F);
ll rj=dp[1][a[1].F];
if(rj==INF) {
rj=-1;
}
cout << rj << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
102 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
105 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
101 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
100 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
121 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |