#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define pb push_back
int n,m,x,y,st,ed,cnt,go,cost,u,d[30005];
set<int> s[30005];
vector<pair<int,int> > v[30005];
priority_queue<pair<int,int> > pq;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(i==1)st=x;
if(i==2)ed=x;
s[x].insert(y);
}
for(int i=0;i<n;i++)
{
for(auto it=s[i].begin();it!=s[i].end();it++)
{
cnt=0;
for(int j=i-(*it);j>=0;j-=(*it))
{
++cnt;
v[i].pb({j,cnt});
if(s[j].find(*it)!=s[j].end())break;
}
cnt=0;
for(int j=i+(*it);j<n;j+=(*it))
{
++cnt;
v[i].pb({j,cnt});
if(s[j].find(*it)!=s[j].end())break;
}
}
}
for(int i=0;i<n;i++)d[i]=1e9;
d[st]=0;
pq.push({0,st});
while(!pq.empty())
{
u=pq.top().Y;
pq.pop();
for(int i=0;i<v[u].size();i++)
{
y=v[u][i].X,cost=v[u][i].Y;
if(d[u]+cost<d[y])
{
d[y]=d[u]+cost;
pq.push({-d[y],y});
}
}
}
if(d[ed]==1e9)
{
printf("-1\n");
}else
{
printf("%d\n",d[ed]);
}
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:47:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i=0;i<v[u].size();i++)
| ~^~~~~~~~~~~~
skyscraper.cpp:12:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
skyscraper.cpp:15:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | scanf("%d%d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2516 KB |
Output is correct |
6 |
Correct |
2 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2420 KB |
Output is correct |
9 |
Correct |
1 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2388 KB |
Output is correct |
11 |
Correct |
2 ms |
2556 KB |
Output is correct |
12 |
Correct |
2 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
2 ms |
2556 KB |
Output is correct |
15 |
Correct |
2 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2488 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2420 KB |
Output is correct |
11 |
Correct |
2 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
2 ms |
2516 KB |
Output is correct |
15 |
Correct |
2 ms |
2516 KB |
Output is correct |
16 |
Correct |
2 ms |
2388 KB |
Output is correct |
17 |
Correct |
3 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2388 KB |
Output is correct |
20 |
Correct |
2 ms |
2560 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
2 ms |
2420 KB |
Output is correct |
23 |
Correct |
2 ms |
2516 KB |
Output is correct |
24 |
Correct |
4 ms |
2644 KB |
Output is correct |
25 |
Correct |
3 ms |
2656 KB |
Output is correct |
26 |
Correct |
2 ms |
2516 KB |
Output is correct |
27 |
Correct |
2 ms |
2516 KB |
Output is correct |
28 |
Correct |
3 ms |
2684 KB |
Output is correct |
29 |
Correct |
4 ms |
3168 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
3 ms |
2772 KB |
Output is correct |
32 |
Correct |
3 ms |
2772 KB |
Output is correct |
33 |
Correct |
6 ms |
3796 KB |
Output is correct |
34 |
Correct |
6 ms |
3836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
1 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2388 KB |
Output is correct |
11 |
Correct |
2 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
2432 KB |
Output is correct |
14 |
Correct |
3 ms |
2516 KB |
Output is correct |
15 |
Correct |
2 ms |
2516 KB |
Output is correct |
16 |
Correct |
2 ms |
2516 KB |
Output is correct |
17 |
Correct |
3 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2556 KB |
Output is correct |
21 |
Correct |
2 ms |
2428 KB |
Output is correct |
22 |
Correct |
2 ms |
2516 KB |
Output is correct |
23 |
Correct |
3 ms |
2556 KB |
Output is correct |
24 |
Correct |
3 ms |
2644 KB |
Output is correct |
25 |
Correct |
3 ms |
2564 KB |
Output is correct |
26 |
Correct |
2 ms |
2564 KB |
Output is correct |
27 |
Correct |
2 ms |
2516 KB |
Output is correct |
28 |
Correct |
3 ms |
2756 KB |
Output is correct |
29 |
Correct |
4 ms |
3156 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
3 ms |
2772 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
6 ms |
3832 KB |
Output is correct |
34 |
Correct |
6 ms |
3796 KB |
Output is correct |
35 |
Correct |
16 ms |
4856 KB |
Output is correct |
36 |
Correct |
3 ms |
2780 KB |
Output is correct |
37 |
Correct |
17 ms |
5508 KB |
Output is correct |
38 |
Correct |
21 ms |
5756 KB |
Output is correct |
39 |
Correct |
21 ms |
5844 KB |
Output is correct |
40 |
Correct |
23 ms |
5848 KB |
Output is correct |
41 |
Correct |
21 ms |
5740 KB |
Output is correct |
42 |
Correct |
6 ms |
2628 KB |
Output is correct |
43 |
Correct |
6 ms |
2692 KB |
Output is correct |
44 |
Correct |
6 ms |
2668 KB |
Output is correct |
45 |
Correct |
26 ms |
8360 KB |
Output is correct |
46 |
Correct |
29 ms |
8436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2420 KB |
Output is correct |
7 |
Correct |
1 ms |
2424 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2408 KB |
Output is correct |
10 |
Correct |
1 ms |
2420 KB |
Output is correct |
11 |
Correct |
2 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2428 KB |
Output is correct |
13 |
Correct |
2 ms |
2428 KB |
Output is correct |
14 |
Correct |
2 ms |
2552 KB |
Output is correct |
15 |
Correct |
2 ms |
2552 KB |
Output is correct |
16 |
Correct |
2 ms |
2392 KB |
Output is correct |
17 |
Correct |
3 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2388 KB |
Output is correct |
20 |
Correct |
2 ms |
2560 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
2 ms |
2428 KB |
Output is correct |
23 |
Correct |
2 ms |
2516 KB |
Output is correct |
24 |
Correct |
3 ms |
2644 KB |
Output is correct |
25 |
Correct |
3 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2516 KB |
Output is correct |
27 |
Correct |
2 ms |
2432 KB |
Output is correct |
28 |
Correct |
3 ms |
2772 KB |
Output is correct |
29 |
Correct |
4 ms |
3064 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
3 ms |
2772 KB |
Output is correct |
32 |
Correct |
3 ms |
2772 KB |
Output is correct |
33 |
Correct |
6 ms |
3796 KB |
Output is correct |
34 |
Correct |
6 ms |
3796 KB |
Output is correct |
35 |
Correct |
18 ms |
4772 KB |
Output is correct |
36 |
Correct |
4 ms |
2820 KB |
Output is correct |
37 |
Correct |
17 ms |
5536 KB |
Output is correct |
38 |
Correct |
21 ms |
5808 KB |
Output is correct |
39 |
Correct |
21 ms |
5796 KB |
Output is correct |
40 |
Correct |
20 ms |
5788 KB |
Output is correct |
41 |
Correct |
20 ms |
5832 KB |
Output is correct |
42 |
Correct |
7 ms |
2644 KB |
Output is correct |
43 |
Correct |
6 ms |
2644 KB |
Output is correct |
44 |
Correct |
6 ms |
2772 KB |
Output is correct |
45 |
Correct |
25 ms |
8316 KB |
Output is correct |
46 |
Correct |
25 ms |
8304 KB |
Output is correct |
47 |
Correct |
46 ms |
12984 KB |
Output is correct |
48 |
Correct |
15 ms |
6088 KB |
Output is correct |
49 |
Correct |
13 ms |
5144 KB |
Output is correct |
50 |
Correct |
11 ms |
4820 KB |
Output is correct |
51 |
Correct |
38 ms |
8272 KB |
Output is correct |
52 |
Correct |
39 ms |
8640 KB |
Output is correct |
53 |
Correct |
21 ms |
5772 KB |
Output is correct |
54 |
Correct |
3 ms |
3028 KB |
Output is correct |
55 |
Correct |
4 ms |
3408 KB |
Output is correct |
56 |
Correct |
12 ms |
5076 KB |
Output is correct |
57 |
Correct |
38 ms |
11544 KB |
Output is correct |
58 |
Correct |
10 ms |
3676 KB |
Output is correct |
59 |
Correct |
10 ms |
4056 KB |
Output is correct |
60 |
Correct |
11 ms |
4688 KB |
Output is correct |
61 |
Correct |
12 ms |
4124 KB |
Output is correct |
62 |
Correct |
21 ms |
9300 KB |
Output is correct |
63 |
Correct |
69 ms |
25636 KB |
Output is correct |
64 |
Correct |
72 ms |
28436 KB |
Output is correct |
65 |
Correct |
93 ms |
38328 KB |
Output is correct |
66 |
Correct |
224 ms |
65400 KB |
Output is correct |
67 |
Correct |
225 ms |
65592 KB |
Output is correct |