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 ll;
const int INF = (int) 1e9 + 123;
const ll LINF = (ll) 1e18 + 123;
#define rep(i, s, t) for (auto i = (s); i < (t); ++(i))
#define per(i, s, t) for (auto i = (s); i >= (t); --(i))
#define sz(x) ((int) (x).size())
#define mp make_pair
#define pb push_back
bool mini(auto &x, const auto &y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
bool maxi(auto &x, const auto &y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
void run();
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
run();
return 0;
}
const int N = 30003;
int n, m;
int b[N], p[N];
int dist[N];
bool used[N];
vector<pair<int, int>> g[N];
void run() {
cin >> n >> m;
rep(i, 0, m) {
cin >> b[i] >> p[i];
}
rep(i, 0, m) {
rep(j, 0, N) {
int to = b[i] + (j + 1) * p[i];
if (to >= n) {
break;
}
g[b[i]].pb({to, j + 1});
}
rep(j, 0, N) {
int to = b[i] - (j + 1) * p[i];
if (to < 0) {
break;
}
g[b[i]].pb({to, j + 1});
}
}
fill(dist, dist + n, INF);
dist[b[0]] = 0;
priority_queue<pair<int, int>> q;
q.push({0, b[0]});
while (sz(q)) {
int v = q.top().second;
q.pop();
if (used[v]) continue;
used[v] = 1;
for (auto &e : g[v]) {
int u = e.first;
if (mini(dist[u], dist[v] + e.second)) {
q.push({-dist[u], u});
}
}
}
cout << (dist[b[1]] == INF ? -1 : dist[b[1]]) << "\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... |