#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(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}});
}
}
if(v==t)break;
}
for(int i=1;i<=n;++i){
int v=u-nhay*i;
if(v<0)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}});
}
}
if(v==t)break;
}
}
}
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:84:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
84 | main()
| ^~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:91:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:92:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
26960 KB |
Output is correct |
2 |
Correct |
5 ms |
26960 KB |
Output is correct |
3 |
Correct |
6 ms |
26960 KB |
Output is correct |
4 |
Correct |
6 ms |
26960 KB |
Output is correct |
5 |
Correct |
8 ms |
26960 KB |
Output is correct |
6 |
Correct |
6 ms |
26960 KB |
Output is correct |
7 |
Correct |
6 ms |
26960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
26960 KB |
Output is correct |
2 |
Correct |
8 ms |
26960 KB |
Output is correct |
3 |
Correct |
5 ms |
26960 KB |
Output is correct |
4 |
Correct |
5 ms |
26960 KB |
Output is correct |
5 |
Correct |
5 ms |
26960 KB |
Output is correct |
6 |
Correct |
6 ms |
26960 KB |
Output is correct |
7 |
Correct |
5 ms |
26960 KB |
Output is correct |
8 |
Correct |
6 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
26960 KB |
Output is correct |
10 |
Correct |
5 ms |
27116 KB |
Output is correct |
11 |
Incorrect |
8 ms |
27264 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
27144 KB |
Output is correct |
2 |
Correct |
5 ms |
26960 KB |
Output is correct |
3 |
Correct |
6 ms |
26960 KB |
Output is correct |
4 |
Correct |
6 ms |
26960 KB |
Output is correct |
5 |
Correct |
7 ms |
26960 KB |
Output is correct |
6 |
Correct |
7 ms |
26960 KB |
Output is correct |
7 |
Correct |
5 ms |
26960 KB |
Output is correct |
8 |
Correct |
4 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
26960 KB |
Output is correct |
10 |
Correct |
6 ms |
27132 KB |
Output is correct |
11 |
Incorrect |
6 ms |
27088 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
26960 KB |
Output is correct |
2 |
Correct |
5 ms |
26960 KB |
Output is correct |
3 |
Correct |
5 ms |
26960 KB |
Output is correct |
4 |
Correct |
6 ms |
26960 KB |
Output is correct |
5 |
Correct |
6 ms |
26960 KB |
Output is correct |
6 |
Correct |
6 ms |
26960 KB |
Output is correct |
7 |
Correct |
5 ms |
26960 KB |
Output is correct |
8 |
Correct |
5 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
26960 KB |
Output is correct |
10 |
Correct |
5 ms |
26960 KB |
Output is correct |
11 |
Incorrect |
6 ms |
27216 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
26960 KB |
Output is correct |
2 |
Correct |
5 ms |
27076 KB |
Output is correct |
3 |
Correct |
5 ms |
27128 KB |
Output is correct |
4 |
Correct |
6 ms |
26960 KB |
Output is correct |
5 |
Correct |
5 ms |
26960 KB |
Output is correct |
6 |
Correct |
6 ms |
27256 KB |
Output is correct |
7 |
Correct |
5 ms |
26960 KB |
Output is correct |
8 |
Correct |
6 ms |
26960 KB |
Output is correct |
9 |
Correct |
8 ms |
26960 KB |
Output is correct |
10 |
Correct |
5 ms |
26960 KB |
Output is correct |
11 |
Incorrect |
6 ms |
27168 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |