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 oo 100000000
#define maxn 30004
using namespace std;
typedef long long ll;
int n, m;
int b[maxn], p[maxn];
vector < pair < int, int > > g[maxn];
set < int > s[maxn];
int d[maxn];
int main(){
#define TASK "ABC"
// freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout);
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> b[i] >> p[i];
s[b[i]].insert(p[i]);
}
for (int i = 0; i < n; ++i)
for (auto p : s[i]) {
for (int j = 1; i + j * p < n; ++j) {
g[i].push_back({i + j * p, j});
if (s[i + j * p].count(p))
break;
}
for (int j = 1; i - j * p >= 0; ++j) {
g[i].push_back({i - j * p, j});
if (s[i - j * p].count(p))
break;
}
}
for (int i = 0; i < n; ++i)
random_shuffle(g[i].begin(), g[i].end());
priority_queue < pair < int, int > > p;
fill(d, d + n + 1, oo);
d[b[0]] = 0;
p.push({0, b[0]});
while (!p.empty()) {
int dist = p.top().first, u = p.top().second; p.pop();
if (d[u] != -dist) continue;
for (auto i : g[u]) {
int v = i.first, w = i.second;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
p.push({-d[v], v});
}
}
}
cout << (d[b[1]] == oo ? -1 : 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... |