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>
using namespace std;
typedef long long int ll;
const int MAXN = 3e4 + 1;
const ll inf = LLONG_MAX / 4ll;
ll n, m, b[MAXN], p[MAXN], d[MAXN];
vector <ll> ps[MAXN];
priority_queue <pair<ll,ll>> q;
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
// scanf("%d", &n); scanf("%d", &m);
for(int i = 0; i < m; i++){
cin >> b[i] >> p[i];
// scanf("%d", &bb); scanf("%d", &pp);
ps[b[i]].push_back({p[i]});
}
for(int i = 0; i < n; i++) d[i] = inf;
d[b[0]] = 0;
q.push({0, b[0]});
while(!q.empty()){
ll u = q.top().second, dd = -q.top().second; q.pop();
if(d[u] < dd) continue;
if(u == b[1] || d[u] >= inf) break;
for(ll k: ps[u]){
for(ll i = u % k; i < n; i += k)
if(d[i] > d[u] + (abs(u - i) / k)){
d[i] = d[u] + (abs(u - i) / k);
q.push({-d[i], i});
}
}
}
cout << ((d[b[1]] >= inf)?-1:d[b[1]]);
// if(d[b[1]] >= inf) printf("%d", -1);
// else printf("%d", d[b[1]]);
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... |