#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=3e4+5,BLOCK=sqrt(maxN);
const ll inf=2e18;
int n,m,b[maxN+1],p[maxN+1],block,cnt[maxN+1];
ll d[maxN+1][BLOCK+1];
struct duongdi
{
int u,power;
ll w;
bool operator < (const duongdi &o)const
{
return w>o.w;
}
};
vector<duongdi> adj[maxN+1];
priority_queue<duongdi> pq;
vector<int> vec[maxN+1];
void CO(int u,int v,int pw,int power,int w)
{
if(d[v][pw]>d[u][power]+w)
{
d[v][pw]=d[u][power]+w;
pq.push({v,pw,d[v][pw]});
}
}
int main()
{
//freopen("","r",stdin);
//freopen("","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
block=sqrt(n);
for(int i=0;i<m;i++)
{
cin>>b[i]>>p[i];
if(p[i]<=block)
{
vec[b[i]].push_back(p[i]);
}
else cnt[b[i]]++;
}
for(int i=0;i<m;i++)
{
if(p[i]<=block)
{
continue;
}
for(int j=b[i]+p[i];j<n;j+=p[i])
{
if(cnt[j])
{
adj[b[i]].push_back({j,0,(j-b[i])/p[i]});
}
}
for(int j=b[i]-p[i];j>=0;j-=p[i])
{
if(cnt[j])
{
adj[b[i]].push_back({j,0,(b[i]-j)/p[i]});
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<=block;j++)
{
d[i][j]=inf;
}
}
if(p[0]<=block)
{
d[b[0]][p[0]]=0;
pq.push({b[0],p[0],0});
}
else
{
d[b[0]][0]=0;
pq.push({b[0],0,0});
}
while(!pq.empty())
{
duongdi tmp=pq.top();
pq.pop();
int u=tmp.u,power=tmp.power;
ll w=tmp.w;
//cout<<u<<" "<<power<<" "<<w<<'\n';
if(u==b[1])
{
cout<<d[u][power];
return 0;
}
if(d[u][power]<w)
{
continue;
}
if(power>0)
{
if(u+power<n)
{
CO(u,u+power,power,power,1);
}
if(u-power>=0)
{
CO(u,u-power,power,power,1);
}
if(cnt[u]>0)
{
CO(u,u,0,power,0);
}
}
else
{
for(auto i:adj[u])
{
CO(u,i.u,0,power,i.w);
}
}
for(auto i:vec[u])
{
CO(u,u,i,power,0);
}
}
cout<<-1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1624 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1880 KB |
Output is correct |
5 |
Correct |
1 ms |
1880 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1880 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Incorrect |
1 ms |
1884 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1880 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Incorrect |
1 ms |
1884 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Incorrect |
1 ms |
1884 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Incorrect |
1 ms |
1884 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |