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;
// #define int ll
#define pb push_back
typedef pair<int, int> pii;
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
const int INF = 1e9;
bool cmin(int &a, const int &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
void run() {
int n, m;
cin >> n >> m;
vector<int> b(m), p(m);
vector<vector<int>> who(n);
for (int i = 0; i < m; ++i) {
cin >> b[i] >> p[i];
who[b[i]].pb(p[i]);
}
vector<vector<pii>> g(n);
for (int u = 0; u < n; ++u) {
sort(all(who[u]));
who[u].resize(unique(all(who[u])) - who[u].begin());
for (auto x : who[u]) {
int cnt = 0;
for (int i = u; i < n; i += x) {
g[u].pb({i, cnt});
++cnt;
}
cnt = 0;
for (int i = u; i >= 0; i -= x) {
g[u].pb({i, cnt});
++cnt;
}
}
// sort(all(g[u]));
// g[u].resize(unique(all(g[u])) - g[u].begin());
}
vector<int> dist(n, INF);
priority_queue<pii> q;
dist[b[0]] = 0;
q.push({0, b[0]});
while (!q.empty()) {
pii pr = q.top();
q.pop();
if (-pr.F != dist[pr.S])
continue;
int u = pr.S;
for (auto edge : g[u]) {
int v = edge.F;
int w = edge.S;
if (cmin(dist[v], dist[u] + w)) {
q.push({-dist[v], v});
}
}
}
if (dist[b[1]] >= INF)
dist[b[1]] = -1;
cout << dist[b[1]] << '\n';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
run();
}
# | 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... |