#include<bits/stdc++.h>
#define int long long
#define str string
#define task "strdel"
#define ii pair<int,int>
#define iii pair<int,ii>
#define iv pair<ii,ii>
#define se second
#define fi first
#define ffi fi.fi
#define sfi se.fi
#define sse se.se
#define fse fi.se
#define lt(i, c, d) for(int i = c; i <= d; ++i)
#define fl(i, c, d) for(int i = d; i >= c; --i)
#define pb push_back
#define emb emplace_back
#define em emplace
using namespace std;
const int N=1e6+5,lg=20,mod=1e9+7;
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int u,int v){
return u+rd()%(v-u+1);
}
int n,m,s,t,ans,dist[N];
ii a[N];
vector<int>res[N];
bool k[N];
void bfs(int u){
for(int i=0;i<n;++i)dist[i]=1e9;
dist[u]=0;
priority_queue<iii,vector<iii>,greater<iii>>p;
p.push({0,{a[1].fi,a[1].se}});
while(!p.empty()){
int u=p.top().sfi;
int nhay=p.top().sse;
int w=p.top().fi;
p.pop();
for(int i=1;i<=n;++i){
int v=u+nhay*i;
if(v>n-1)break;
if(v==t)break;
if(dist[v]>dist[u]+i){
dist[v]=dist[u]+i;
p.push({dist[v],{v,nhay}});
if(!k[v]){
k[v]=true;
for(int i:res[v])p.push({dist[v],{v,a[i].se}});
}
}
}
for(int i=1;i<=n;++i){
int v=u-nhay*i;
if(v<0)break;
if(v==t)break;
if(dist[v]>dist[u]+i){
dist[v]=dist[u]+i;
p.push({dist[v],{v,nhay}});
if(!k[v]){
k[v]=true;
for(int i:res[v])p.push({dist[v],{v,a[i].se}});
}
}
}
}
}
void solve(){
cin >> n >> m;
for(int i=1;i<=m;++i){
cin >> a[i].fi >> a[i].se;
if(i==1)s=a[i].fi;
else if(i==2)t=a[i].fi;
else res[a[i].fi].emb(i);
}
bfs(s);
// for(int i=0;i<=n-1;++i)cout<<dist[i]<<" ";
if(dist[t]==1e9)dist[t]=-1;
cout << dist[t];
}
main()
{
srand(time(0));
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
if(fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
int t=1;
// cin >> t;
while(t--){
solve();
cout<<'\n';
}
}
Compilation message
skyscraper.cpp: In function 'void bfs(long long int)':
skyscraper.cpp:37:12: warning: unused variable 'w' [-Wunused-variable]
37 | int w=p.top().fi;
| ^
skyscraper.cpp: At global scope:
skyscraper.cpp:82:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
82 | main()
| ^~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:89:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:90:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
26960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
26960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
26960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
26960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
26960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |