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;
int n, m, b, p;
vector<int> doges[50000];
vector<pair<int, int> > graph[50000];
int powers[50000];
int positions[50000];
priority_queue<pair<int, int> > q;
int dists[50000];
bool visited[50000];
bool visited2[50000]; //position
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> b >> p;
powers[i] = p;
positions[i] = b;
doges[b].push_back(i);
dists[i] = 1000000000;
}
q.push({0, 0});
dists[0] = 0;
while (!q.empty())
{
int x = q.top().second;
//cout << "At node " << x << " which is " << dists[x] << " away\n";
if (x == 1)
{
int ans = dists[1];
cout << ans << "\n";
return 0;
}
q.pop();
if (visited[x])
{
continue;
}
visited[x] = true;
int jumpCount = -1;
for (int j = positions[x]; j < n; j+=powers[x])
{
if (visited2[j])
{
continue;
}
visited2[j] = true;
jumpCount++;
for (auto k : doges[j])
{
if (!visited[k] and dists[x] + jumpCount < dists[k])
{
if (k == 1 or powers[k] != powers[x])
{
dists[k] = dists[x] + jumpCount;
q.push({-dists[k], k});
}
}
}
}
jumpCount = 0;
for (int j = positions[x]-powers[x]; j >= 0; j-=powers[x])
{
if (visited2[j])
{
continue;
}
visited2[j] = true;
jumpCount++;
for (auto k : doges[j])
{
if (!visited[k] and dists[x] + jumpCount < dists[k])
{
if (k == 1 or powers[k] != powers[x])
{
dists[k] = dists[x] + jumpCount;
q.push({-dists[k], k});
}
}
}
}
}
cout << "-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... |