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>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define debug(x) cout << "Line " << __LINE__ << ", " << #x << " is " << x << endl
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<ll, ll>
#define ld long double
#define nl endl
#define tb '\t'
#define sp ' '
#define sqr(x) (x)*(x)
#define arr3 array<ll, 3>
using namespace std;
const ll MX=30002, MOD=1000000007, BLOCK=327, INF=2e9+7, LG=62;
const ll INFF=1000000000000000007;
const ld ERR=1e-6, pi=3.14159265358979323846;
ll N, M, B[MX], P[MX];
map<ll, ll> vis[MX];
vector<ll> doge[MX];
int main(){
fastio;
cin >> N >> M;
for(ll i=0; i<M; i++){
cin >> B[i] >> P[i];
doge[B[i]].pb(P[i]);
}
priority_queue<arr3, vector<arr3>, greater<arr3>> PQ;
PQ.push({0, B[0], P[0]});
vis[B[0]][P[0]]=0;
while(PQ.size()){
ll cost=PQ.top()[0], idx=PQ.top()[1], p=PQ.top()[2];
PQ.pop();
if(vis[idx].count(p) && vis[idx][p]<cost)
continue;
for(ll i:doge[idx]){
if(vis[idx].count(i) && vis[idx][i]<cost)
continue;
vis[idx][i]=cost;
for(ll j=1, jj; idx+i*j<N; j++){
jj=idx+i*j;
if(!vis[jj].count(i) || vis[jj][i]>cost+j){
vis[jj][i]=cost+j;
PQ.push({cost+j, jj, i});
}
}
for(ll j=1, jj; idx-i*j>=0; j++){
jj=idx-i*j;
if(!vis[jj].count(i) || vis[jj][i]>cost+j){
vis[jj][i]=cost+j;
PQ.push({cost+j, jj, i});
}
}
}
}
ll ans=INFF;
for(ll i=0; i<=30000; i++){
if(vis[B[1]].count(i))
ans=min(ans, vis[B[1]][i]);
}
if(ans==INFF)
ans=-1;
cout << ans << nl;
}
# | 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... |