#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<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);
for(auto &z : wh[i]) {
if(dp[z][i]>dp[x][y]+(i-y)/a[x].S) {
dp[z][i]=dp[x][y]+(i-y)/a[x].S;
dfs(z, 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);
for(auto &z : wh[i]) {
if(dp[z][i]>dp[x][y]+(y-i)/a[x].S) {
dp[z][i]=dp[x][y]+(y-i)/a[x].S;
dfs(z, i);
}
}
}
}
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;
}
}
cin >> n >> m;
for(int i=0; i<m; i++) {
cin >> a[i].F >> a[i].S; // pos (b), power
wh[a[i].F].push_back(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 |
126 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
94 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
95 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
95 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
98 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |