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;
#define f first
#define s second
#define int long long
const int N = 30069;
const int inf = 1e18;
int n, m, a[N], b[N], dis[N];;
vector<int> c[N];
vector<pair<int, int>> g[N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> m;
for (int i = 1;i <= m;i++) {
cin >> a[i] >> b[i], ++a[i];
c[a[i]].push_back(i);
}
vector<int> o;
for (int i = 1;i <= n;i++) {
if (!c[i].empty())
o.push_back(i);
}
for (int i = 1;i <= n;i++) {
dis[i] = inf;
}
for (int _i = 0;_i < (int)o.size();_i++) {
for (int _j = _i + 1;_j < (int)o.size();_j++) {
int i = o[_i];
int j = o[_j];
int mn = inf;
for (auto u : c[i]) {
if (abs(a[u] - j) % b[u] == 0) {
int d = abs(a[u] - j) / b[u];
mn = min(mn, d);
}
}
if (mn != inf)
g[i].push_back({j, mn});
mn = inf;
for (auto u : c[j]) {
if (abs(a[u] - i) % b[u] == 0) {
int d = abs(a[u] - i) / b[u];
mn = min(mn, d);
}
}
if (mn != inf)
g[j].push_back({i, mn});
}
}
priority_queue<pair<int, int>> q;
q.push({0, a[1]});
dis[a[1]] = 0;
while (!q.empty()) {
auto v = q.top().s;
q.pop();
if (v == a[2])
break;
for (auto u : g[v]) {
if (dis[u.f] <= dis[v] + u.s)
continue;
dis[u.f] = dis[v] + u.s;
q.push({-dis[u.f], u.f});
}
}
if (dis[a[2]] == inf)
cout << -1 << '\n';
else
cout << dis[a[2]] << '\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... |