이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define ss second
#define ff first
#define vt vector
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;
const int inf = 1e9;
const int mod = 1e9+7;
const int maxn = 30000;
void solve() {
int n, m;
cin >> n >> m;
int a[m], b[m];
vt<int> it[n];
int dp[n];
for(int i = 0; i < n; i++)dp[i] = inf;
for(int i = 0; i < m; i++){
cin >> a[i] >> b[i];
it[a[i]].pb(i);
}
priority_queue<pii, vt<pii>, greater<pii>> pq;
pq.push({0, a[0]});
dp[a[0]] = 0;
while(!pq.empty()){
pii nw = pq.top();
pq.pop();
if(dp[nw.ss] != nw.ff)continue;
for(auto i : it[nw.ss]){
int x = b[i];
for(int j = nw.ss - x, cnt = 1; j >= 0; cnt++, j -= x){
if(dp[j] > nw.ff + cnt){
dp[j] = nw.ff + cnt;
pq.push({dp[j], j});
}
}
for(int j = nw.ss + x, cnt = 1; j < n; cnt++, j += x){
if(dp[j] > nw.ff + cnt){
dp[j] = nw.ff + cnt;
pq.push({dp[j], j});
}
}
}
}
if(dp[a[1]] == inf)cout << -1;
else cout << dp[a[1]];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int times = 1;
//cin >> times;
for(int i = 1; i <= times; i++) {
solve();
}
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... |