#include<bits/stdc++.h>
#define lf double
#define ll long long
#define cc pair<char,char>
#define ull unsigned ll
#define ii pair<int,int>
#define li pair<ll,int>
#define iii pair<ii,int>
#define iiii pair<ii,ii>
#define iiii2 pair<int,iii>
#define lii pair<ll,ii>
#define lolo pair<ll,ll>
#define heap priority_queue
#define mp make_pair
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define all(x) x.begin(),x.end()
#define len(x) strlen(x)
#define sz(x) (int) x.size()
#define orta ((bas+son)/2)
#define min3(x,y,z) min(min(x,y),z)
#define max3(x,y,z) max(max(x,y),z)
#define umin(x,y) x=min(x,y)
#define umax(x,y) x=max(x,y)
#define dbgs(x) cerr<<(#x)<<" --> "<<(x)<<" "
#define dbg(x) cerr<<(#x)<<" --> "<<(x)<<endl;getchar()
#define MOD 1000000007
#define inf 1000000005
#define M 10000002
#define N 30005
#define LOG 40
#define magic 100
#define KOK 250
#define EPS 0.0025
#define pw(x) ((1ll)<<(x))
#define PI 3.1415926535
using namespace std;
int n,m,b,p;
int dst[N];
vector<int> go[N];
int main() {
#if 0
freopen("input.txt","r",stdin);
#endif
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++) {
scanf("%d %d",&b,&p);
go[b].pb(p);
}
for(int i=0;i<n;i++) {
sort(all(go[i]));
unique(all(go[i]));
}
fill(dst,dst+N,inf);
heap<ii> H;
H.push({0,0});
dst[0]=0;
while(!H.empty()) {
ii x=H.top();
H.pop();
if(dst[x.nd]<-x.st) continue ;
if(x.nd==1) {
printf("%d",dst[x.nd]);
return 0;
}
for(int p:go[x.nd]) {
for(int i=x.nd+p;i<n;i+=p) {
umin(dst[i],-x.st+(i-x.nd)/p);
H.push({x.st-(i-x.nd)/p,i});
if(binary_search(all(go[i]),p)) break ;
}
for(int i=x.nd-p;i>=0;i-=p) {
umin(dst[i],-x.st+(x.nd-i)/p);
H.push({x.st-(x.nd-i)/p,i});
if(binary_search(all(go[i]),p)) break ;
}
}
}
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:53:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&m);
~~~~~^~~~~~~~~~~~~~~
skyscraper.cpp:57:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&b,&p);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1140 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1140 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
1160 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1160 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1364 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1364 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1364 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |