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 TASKNAME "codeforce"
#define pb push_back
#define pli pair<ll,ll>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=int;
const ll maxN=3e4+100;
const ll inf=1e9+7;
const ll mod=1e9+7;
ll n;
ll dis[maxN];
priority_queue<pli,vector<pli>,greater<pli>>pq;
vector<ll>lon[maxN];
vector<pli>be[maxN];
ll p[maxN],b[maxN];
const ll block=175;
vector<ll>vec[block+2][block+2];
ll m;
void dij(ll s)
{
for(int i=0;i<n;i++) dis[i]=inf;
dis[s]=0;
pq.push({dis[s],s});
while(!pq.empty())
{
ll u=pq.top().se;
ll d=pq.top().fi;
pq.pop();
if(d>dis[u]) continue;
if(u==b[2])
{
break;
}
for(auto i:lon[u])
{
for(int j=u-p[i];j>=0;j-=p[i])
{
if(dis[j]>dis[u]+(u-j)/p[i])
{
dis[j]=dis[u]+(u-j)/p[i];
pq.push({dis[j],j});
}
}
for(int j=u+p[i];j<n;j+=p[i])
{
if(dis[j]>dis[u]+(j-u)/p[i])
{
dis[j]=dis[u]+(j-u)/p[i];
pq.push({dis[j],j});
}
}
}
for(auto cc:be[u])
{
ll i=cc.fi;
ll j=u%i;
ll k=cc.se;
ll pos=vec[i][j][k];
ll pos2=k>0?vec[i][j][k-1]:0;
for(int q=pos-i;q>=pos2;q-=i)
{
if(dis[q]>dis[pos]+(pos-q)/i)
{
dis[q]=dis[pos]+(pos-q)/i;
pq.push({dis[q],q});
}
}
pos2=(k+1<vec[i][j].size())?vec[i][j][k+1]:n-1;
for(int q=pos+i;q<=pos2;q+=i)
{
if(dis[q]>dis[pos]+(q-pos)/i)
{
dis[q]=dis[pos]+(q-pos)/i;
pq.push({dis[q],q});
}
}
}
}
}
void solve()
{
cin >> n >> m;
for(int i=1;i<=m;i++)
{
cin >> b[i] >> p[i];
}
for(int i=1;i<=m;i++)
{
if(p[i]>block)
{
lon[b[i]].pb(i);
}
else
{
vec[p[i]][b[i]%p[i]].pb(b[i]);
}
}
for(int i=1;i<=block;i++)
{
for(int j=0;j<i;j++)
{
sort(vec[i][j].begin(),vec[i][j].end());
for(int k=0;k<vec[i][j].size();k++)
{
be[vec[i][j][k]].pb({i,k});
}
}
}
dij(b[1]);
if(dis[b[2]]==inf) cout << -1<<'\n';
else cout << dis[b[2]];
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message (stderr)
skyscraper.cpp: In function 'void dij(ll)':
skyscraper.cpp:71:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | pos2=(k+1<vec[i][j].size())?vec[i][j][k+1]:n-1;
| ~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:107:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(int k=0;k<vec[i][j].size();k++)
| ~^~~~~~~~~~~~~~~~~
# | 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... |