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;
const int N = 30001;
vector<pair<int,int> >v[N];
int b[N] , p[N] ;
vector<pair<int,int> >vp;
int n , m ;
map<pair<int,int> , int>mp;
int main()
{
cin>> n>> m ;
for(int i = 0 ; i < m ; i++)
{
cin >> b[i] >> p[i];
mp[{b[i],p[i]}]++;
vp.push_back({b[i], p[i]});
}
sort(vp.begin(),vp.end());
for(int i = 0 ; i < m ; i++)
{
int c=1;
for(int j = vp[i].first+vp[i].second ; j< n ; j+= vp[i].second)
{
v[vp[i].first].push_back({j , c});
c++;
if(mp[{j,vp[i].second}])
break;
}
}
for(int i = m-1 ; i >=0 ; i--)
{
int c=1;
for(int j = vp[i].first-vp[i].second ; j>=0 ; j-= vp[i].second)
{
v[vp[i].first].push_back({j , c});
c++;
if(mp[{j,vp[i].second}])
break;
}
}
int dist[N];
priority_queue<pair<int,int> >q;
int processed[N]={0};
for (int i = 0; i <= n; i++) dist[i] = INT_MAX;
dist[0] = 0;
q.push({0,0});
while (!q.empty())
{
int a = q.top().second; q.pop();
if (processed[a]) continue;
processed[a] = 1;
//cout << a << " : " ;
for (auto u : v[a])
{
int b = u.first, w = u.second;
// cout << b << " ";
if (dist[a]+w < dist[b])
{
dist[b] = dist[a]+w;
q.push({-dist[b],b});
}
}
//cout << "\n";
}
if(dist[1]==INT_MAX)
dist[1]=-1;
cout << dist[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... |