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 ll long long
using namespace std;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 3e4 + 10;
vector <int> save[N];
int n;
vector <ll> cal(int s) {
vector <ll> dis;
dis.assign(n, 1e12);
dis[s] = 0;
priority_queue <pair <ll, ll>, vector <pair <ll, ll>>, greater <pair <ll, ll>>> pq;
pq.push({0, s});
while (sz(pq)) {
auto t = pq.top();
pq.pop();
if (dis[t.s] < t.f)
continue;
for (auto x : save[t.s]) {
int cnt = 0;
for (int i = t.s; i >= 0; i -= x) {
if (dis[i] > t.f + cnt) {
dis[i] = t.f + cnt;
pq.push({dis[i], i});
}
cnt++;
}
cnt = 0;
for (int i = t.s; i < n; i += x) {
if (dis[i] > t.f + cnt) {
dis[i] = t.f + cnt;
pq.push({dis[i], i});
}
cnt++;
}
}
}
return dis;
}
ll solve() {
int m;
cin >> n >> m;
int s = 0, e = 0;
for (int i = 0; i < m; i++) {
int p, d;
cin >> p >> d;
save[p].pb(d);
if (i == 0) {
s = p;
}
if (i == 1) {
e = p;
}
}
vector <ll> d1 = cal(s);
ll ans = d1[e];
//for (int i = 0; i < n; i++) {
//ans = min(ans, d1[i] + d2[i]);
//}
if (ans > 1e9)
ans = -1;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << solve() << '\n';
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... |