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<pair<int,int>>g[maxN];
ll p[maxN],b[maxN];
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 vv:g[u])
{
int v=vv.fi;
int w=vv.se;
if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
pq.push({dis[v],v});
}
}
}
}
ll m;
const ll block=175;
vector<ll>vec[block+2][block+2];
bool cmp(pli x,pli y)
{
if(x.fi==y.fi) return x.se<y.se;
return x.fi<y.fi;
}
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)
{
for(int j=b[i]-p[i];j>=0;j-=p[i])
{
g[b[i]].pb({j,(i-j)/p[i]});
}
for(int j=b[i]+p[i];j<n;j+=p[i])
{
g[b[i]].pb({j,(j-i)/p[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++)
{
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)
{
g[pos].pb({q,(pos-q)/i});
}
pos2=(k+1<vec[i][j].size())?vec[i][j][k+1]:n-1;
for(int q=pos+i;q<=pos2;q+=i)
{
g[pos].pb({q,(q-pos)/i});
}
}
}
}
vector<pli>xx;
for(int i=0;i<n;i++)
{
sort(g[i].begin(),g[i].end(),cmp);
for(int j=0;j<g[i].size();j++)
{
if(j==0||g[i][j].fi!=g[i][j-1].fi)
{
xx.pb(g[i][j]);
}
}
swap(xx,g[i]);
xx.clear();
}
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 solve()':
skyscraper.cpp:84:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int k=0;k<vec[i][j].size();k++)
| ~^~~~~~~~~~~~~~~~~
skyscraper.cpp:92:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | pos2=(k+1<vec[i][j].size())?vec[i][j][k+1]:n-1;
| ~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:104:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int j=0;j<g[i].size();j++)
| ~^~~~~~~~~~~~
# | 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... |