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 fi first
#define si second
#define int long long
using namespace std;
const int N = 5e4 + 4;
int dist[N],n,m;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
vector<int> g[N];
pair<int,int> a[N];
signed main()
{
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
cin >> a[i].fi >> a[i].si;
a[i].fi++;
g[a[i].fi].push_back(a[i].si);
}
for(int i = 1; i <= n; i++) dist[i] = 1e18;
q.push({0,a[1].fi});
dist[a[1].first] = 0;
while(!q.empty())
{
pair<int,int> node = q.top();q.pop();
int u = node.si, d = node.fi;
if(d >= 5000) {continue;}
if(dist[u] < d) continue;
for(int p : g[u])
{ int t = d;
for(int i = u + p; i <= n; i += p)
{ t++;
if(dist[i] > t)
{
dist[i] = t;
q.push({dist[i],i});
}
}
t = d;
for(int i = u - p; i > 0; i -= p)
{
t++;
if(dist[i] > t)
{
dist[i] = t;
q.push({dist[i],i});
}
}
}
}
if(dist[a[2].first] == 1e18) cout << -1;
else cout << dist[a[2].first];
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... |