#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=305;
const ll inf=2e18;
int n,m,b[maxN+1],p[maxN+1];
ll d[maxN+1][BLOCK+1];
bool vis[maxN+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;
memset(vis,false,sizeof(vis));
for(int i=0;i<m;i++)
{
cin>>b[i]>>p[i];
if(p[i]<=BLOCK)
{
vec[b[i]].push_back(p[i]);
}
else vis[b[i]]=true;
}
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(vis[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(vis[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(vis[u])
{
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;
}
#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=305;
const ll inf=2e18;
int n,m,b[maxN+1],p[maxN+1];
ll d[maxN+1][BLOCK+1];
bool vis[maxN+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;
memset(vis,false,sizeof(vis));
for(int i=0;i<m;i++)
{
cin>>b[i]>>p[i];
if(p[i]<=BLOCK)
{
vec[b[i]].push_back(p[i]);
}
else vis[b[i]]=true;
}
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(vis[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(vis[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(vis[u])
{
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;
}
Compilation message
skyscraper.cpp:143:11: error: redefinition of 'const int maxN'
143 | const int maxN=3e4+5,BLOCK=305;
| ^~~~
skyscraper.cpp:9:11: note: 'const int maxN' previously defined here
9 | const int maxN=3e4+5,BLOCK=305;
| ^~~~
skyscraper.cpp:143:22: error: redefinition of 'const int BLOCK'
143 | const int maxN=3e4+5,BLOCK=305;
| ^~~~~
skyscraper.cpp:9:22: note: 'const int BLOCK' previously defined here
9 | const int maxN=3e4+5,BLOCK=305;
| ^~~~~
skyscraper.cpp:144:10: error: redefinition of 'const long long int inf'
144 | const ll inf=2e18;
| ^~~
skyscraper.cpp:10:10: note: 'const long long int inf' previously defined here
10 | const ll inf=2e18;
| ^~~
skyscraper.cpp:145:5: error: redefinition of 'int n'
145 | int n,m,b[maxN+1],p[maxN+1];
| ^
skyscraper.cpp:11:5: note: 'int n' previously declared here
11 | int n,m,b[maxN+1],p[maxN+1];
| ^
skyscraper.cpp:145:7: error: redefinition of 'int m'
145 | int n,m,b[maxN+1],p[maxN+1];
| ^
skyscraper.cpp:11:7: note: 'int m' previously declared here
11 | int n,m,b[maxN+1],p[maxN+1];
| ^
skyscraper.cpp:145:9: error: redefinition of 'int b [30006]'
145 | int n,m,b[maxN+1],p[maxN+1];
| ^
skyscraper.cpp:11:9: note: 'int b [30006]' previously declared here
11 | int n,m,b[maxN+1],p[maxN+1];
| ^
skyscraper.cpp:145:19: error: redefinition of 'int p [30006]'
145 | int n,m,b[maxN+1],p[maxN+1];
| ^
skyscraper.cpp:11:19: note: 'int p [30006]' previously declared here
11 | int n,m,b[maxN+1],p[maxN+1];
| ^
skyscraper.cpp:146:4: error: redefinition of 'long long int d [30006][306]'
146 | ll d[maxN+1][BLOCK+1];
| ^
skyscraper.cpp:12:4: note: 'long long int d [30006][306]' previously declared here
12 | ll d[maxN+1][BLOCK+1];
| ^
skyscraper.cpp:147:6: error: redefinition of 'bool vis [30006]'
147 | bool vis[maxN+1];
| ^~~
skyscraper.cpp:13:6: note: 'bool vis [30006]' previously declared here
13 | bool vis[maxN+1];
| ^~~
skyscraper.cpp:148:8: error: redefinition of 'struct duongdi'
148 | struct duongdi
| ^~~~~~~
skyscraper.cpp:14:8: note: previous definition of 'struct duongdi'
14 | struct duongdi
| ^~~~~~~
skyscraper.cpp:157:17: error: redefinition of 'std::vector<duongdi> adj [30006]'
157 | vector<duongdi> adj[maxN+1];
| ^~~
skyscraper.cpp:23:17: note: 'std::vector<duongdi> adj [30006]' previously declared here
23 | vector<duongdi> adj[maxN+1];
| ^~~
skyscraper.cpp:158:25: error: redefinition of 'std::priority_queue<duongdi> pq'
158 | priority_queue<duongdi> pq;
| ^~
skyscraper.cpp:24:25: note: 'std::priority_queue<duongdi> pq' previously declared here
24 | priority_queue<duongdi> pq;
| ^~
skyscraper.cpp:159:13: error: redefinition of 'std::vector<int> vec [30006]'
159 | vector<int> vec[maxN+1];
| ^~~
skyscraper.cpp:25:13: note: 'std::vector<int> vec [30006]' previously declared here
25 | vector<int> vec[maxN+1];
| ^~~
skyscraper.cpp:160:6: error: redefinition of 'void CO(int, int, int, int, int)'
160 | void CO(int u,int v,int pw,int power,int w)
| ^~
skyscraper.cpp:26:6: note: 'void CO(int, int, int, int, int)' previously defined here
26 | void CO(int u,int v,int pw,int power,int w)
| ^~
skyscraper.cpp:168:5: error: redefinition of 'int main()'
168 | int main()
| ^~~~
skyscraper.cpp:34:5: note: 'int main()' previously defined here
34 | int main()
| ^~~~