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