#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 3e4;
const int INF = 1e9;
int N, M, S, E;
vector<int> A[MAXN+10];
vector<pii> adj[MAXN+10];
int dist[MAXN+10];
struct Queue
{
int u, w;
bool operator < (const Queue &p) const { return w>p.w; }
};
int main()
{
scanf("%d%d", &N, &M);
for(int i=1; i<=M; i++)
{
int b, p;
scanf("%d%d", &b, &p);
if(i==1) S=b;
if(i==2) E=b;
A[p].push_back(b);
}
for(int i=1; i<=MAXN; i++)
{
if(A[i].empty()) continue;
vector<vector<int>> V;
vector<int> comp;
V.resize(i);
for(auto it : A[i]) V[it%i].push_back(it);
for(auto it : A[i]) comp.push_back(it%i);
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for(auto it : comp)
{
sort(V[it].begin(), V[it].end());
for(int j=1; j<V[it].size(); j++)
{
int now=V[it][j], bef=V[it][j-1];
for(int k=bef+i; k<=now; k+=i) adj[bef].push_back({k, (k-bef)/i});
for(int k=now-i; k>=bef; k-=i) adj[now].push_back({k, (now-k)/i});
}
for(int k=V[it].back()+i; k<N; k+=i) adj[V[it].back()].push_back({k, (k-V[it].back())/i});
for(int k=V[it][0]-i; k>=0; k-=i) adj[V[it][0]].push_back({k, (V[it][0]-k)/i});
}
}
priority_queue<Queue> PQ;
for(int i=0; i<N; i++) dist[i]=INF;
PQ.push({S, 0}); dist[S]=0;
while(!PQ.empty())
{
Queue now=PQ.top(); PQ.pop();
if(dist[now.u]<now.w) continue;
for(auto nxt : adj[now.u]) if(dist[nxt.first]>now.w+nxt.second) PQ.push({nxt.first, now.w+nxt.second}), dist[nxt.first]=now.w+nxt.second;
}
if(dist[E]==INF) dist[E]=-1;
printf("%d\n", dist[E]);
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:49:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int j=1; j<V[it].size(); j++)
| ~^~~~~~~~~~~~~
skyscraper.cpp:24:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | scanf("%d%d", &N, &M);
| ~~~~~^~~~~~~~~~~~~~~~
skyscraper.cpp:28:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf("%d%d", &b, &p);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1620 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1620 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
2 ms |
1620 KB |
Output is correct |
6 |
Correct |
2 ms |
1712 KB |
Output is correct |
7 |
Correct |
2 ms |
1620 KB |
Output is correct |
8 |
Correct |
2 ms |
1716 KB |
Output is correct |
9 |
Correct |
1 ms |
1716 KB |
Output is correct |
10 |
Correct |
2 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
2 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1724 KB |
Output is correct |
14 |
Correct |
2 ms |
1724 KB |
Output is correct |
15 |
Correct |
2 ms |
1720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1620 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1716 KB |
Output is correct |
6 |
Correct |
1 ms |
1712 KB |
Output is correct |
7 |
Correct |
2 ms |
1644 KB |
Output is correct |
8 |
Correct |
1 ms |
1620 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
10 |
Correct |
2 ms |
1712 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
2 ms |
1724 KB |
Output is correct |
13 |
Correct |
2 ms |
1748 KB |
Output is correct |
14 |
Correct |
2 ms |
1760 KB |
Output is correct |
15 |
Correct |
2 ms |
1716 KB |
Output is correct |
16 |
Correct |
2 ms |
1748 KB |
Output is correct |
17 |
Correct |
4 ms |
1852 KB |
Output is correct |
18 |
Correct |
3 ms |
1852 KB |
Output is correct |
19 |
Correct |
3 ms |
1876 KB |
Output is correct |
20 |
Correct |
2 ms |
1748 KB |
Output is correct |
21 |
Correct |
4 ms |
1728 KB |
Output is correct |
22 |
Correct |
3 ms |
1748 KB |
Output is correct |
23 |
Correct |
4 ms |
1852 KB |
Output is correct |
24 |
Correct |
6 ms |
1876 KB |
Output is correct |
25 |
Correct |
5 ms |
1852 KB |
Output is correct |
26 |
Correct |
2 ms |
1876 KB |
Output is correct |
27 |
Correct |
2 ms |
1876 KB |
Output is correct |
28 |
Correct |
3 ms |
1984 KB |
Output is correct |
29 |
Correct |
4 ms |
2612 KB |
Output is correct |
30 |
Correct |
2 ms |
2004 KB |
Output is correct |
31 |
Correct |
3 ms |
2132 KB |
Output is correct |
32 |
Correct |
2 ms |
2004 KB |
Output is correct |
33 |
Correct |
5 ms |
3284 KB |
Output is correct |
34 |
Correct |
6 ms |
3284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1620 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1592 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
8 |
Correct |
1 ms |
1620 KB |
Output is correct |
9 |
Correct |
2 ms |
1712 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
3 ms |
1852 KB |
Output is correct |
12 |
Correct |
1 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1752 KB |
Output is correct |
14 |
Correct |
2 ms |
1724 KB |
Output is correct |
15 |
Correct |
2 ms |
1748 KB |
Output is correct |
16 |
Correct |
2 ms |
1724 KB |
Output is correct |
17 |
Correct |
3 ms |
1876 KB |
Output is correct |
18 |
Correct |
5 ms |
1876 KB |
Output is correct |
19 |
Correct |
3 ms |
1748 KB |
Output is correct |
20 |
Correct |
3 ms |
1728 KB |
Output is correct |
21 |
Correct |
4 ms |
1748 KB |
Output is correct |
22 |
Correct |
3 ms |
1748 KB |
Output is correct |
23 |
Correct |
3 ms |
1876 KB |
Output is correct |
24 |
Correct |
5 ms |
1876 KB |
Output is correct |
25 |
Correct |
5 ms |
1876 KB |
Output is correct |
26 |
Correct |
2 ms |
1852 KB |
Output is correct |
27 |
Correct |
2 ms |
1876 KB |
Output is correct |
28 |
Correct |
3 ms |
2004 KB |
Output is correct |
29 |
Correct |
4 ms |
2516 KB |
Output is correct |
30 |
Correct |
2 ms |
1972 KB |
Output is correct |
31 |
Correct |
2 ms |
2132 KB |
Output is correct |
32 |
Correct |
2 ms |
1972 KB |
Output is correct |
33 |
Correct |
5 ms |
3284 KB |
Output is correct |
34 |
Correct |
7 ms |
3284 KB |
Output is correct |
35 |
Correct |
16 ms |
3212 KB |
Output is correct |
36 |
Correct |
7 ms |
2048 KB |
Output is correct |
37 |
Correct |
12 ms |
4164 KB |
Output is correct |
38 |
Correct |
24 ms |
3952 KB |
Output is correct |
39 |
Correct |
18 ms |
4024 KB |
Output is correct |
40 |
Correct |
18 ms |
4032 KB |
Output is correct |
41 |
Correct |
19 ms |
3948 KB |
Output is correct |
42 |
Correct |
7 ms |
2368 KB |
Output is correct |
43 |
Correct |
8 ms |
2388 KB |
Output is correct |
44 |
Correct |
8 ms |
2432 KB |
Output is correct |
45 |
Correct |
17 ms |
6740 KB |
Output is correct |
46 |
Correct |
18 ms |
6740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1620 KB |
Output is correct |
4 |
Correct |
1 ms |
1708 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
8 |
Correct |
1 ms |
1716 KB |
Output is correct |
9 |
Correct |
1 ms |
1716 KB |
Output is correct |
10 |
Correct |
2 ms |
1712 KB |
Output is correct |
11 |
Correct |
2 ms |
1720 KB |
Output is correct |
12 |
Correct |
2 ms |
1728 KB |
Output is correct |
13 |
Correct |
2 ms |
1748 KB |
Output is correct |
14 |
Correct |
2 ms |
1724 KB |
Output is correct |
15 |
Correct |
2 ms |
1748 KB |
Output is correct |
16 |
Correct |
2 ms |
1724 KB |
Output is correct |
17 |
Correct |
3 ms |
1876 KB |
Output is correct |
18 |
Correct |
3 ms |
1852 KB |
Output is correct |
19 |
Correct |
3 ms |
1844 KB |
Output is correct |
20 |
Correct |
2 ms |
1748 KB |
Output is correct |
21 |
Correct |
3 ms |
1748 KB |
Output is correct |
22 |
Correct |
3 ms |
1844 KB |
Output is correct |
23 |
Correct |
4 ms |
1876 KB |
Output is correct |
24 |
Correct |
5 ms |
1988 KB |
Output is correct |
25 |
Correct |
5 ms |
1876 KB |
Output is correct |
26 |
Correct |
2 ms |
1856 KB |
Output is correct |
27 |
Correct |
3 ms |
1876 KB |
Output is correct |
28 |
Correct |
3 ms |
2004 KB |
Output is correct |
29 |
Correct |
3 ms |
2516 KB |
Output is correct |
30 |
Correct |
2 ms |
2004 KB |
Output is correct |
31 |
Correct |
3 ms |
2184 KB |
Output is correct |
32 |
Correct |
3 ms |
2004 KB |
Output is correct |
33 |
Correct |
5 ms |
3284 KB |
Output is correct |
34 |
Correct |
6 ms |
3292 KB |
Output is correct |
35 |
Correct |
15 ms |
3156 KB |
Output is correct |
36 |
Correct |
7 ms |
2008 KB |
Output is correct |
37 |
Correct |
14 ms |
4196 KB |
Output is correct |
38 |
Correct |
18 ms |
4032 KB |
Output is correct |
39 |
Correct |
18 ms |
4008 KB |
Output is correct |
40 |
Correct |
17 ms |
3984 KB |
Output is correct |
41 |
Correct |
18 ms |
4052 KB |
Output is correct |
42 |
Correct |
7 ms |
2388 KB |
Output is correct |
43 |
Correct |
8 ms |
2356 KB |
Output is correct |
44 |
Correct |
8 ms |
2388 KB |
Output is correct |
45 |
Correct |
17 ms |
6736 KB |
Output is correct |
46 |
Correct |
17 ms |
6732 KB |
Output is correct |
47 |
Correct |
36 ms |
11336 KB |
Output is correct |
48 |
Correct |
321 ms |
5764 KB |
Output is correct |
49 |
Correct |
460 ms |
5724 KB |
Output is correct |
50 |
Correct |
316 ms |
5072 KB |
Output is correct |
51 |
Correct |
543 ms |
7276 KB |
Output is correct |
52 |
Correct |
520 ms |
7820 KB |
Output is correct |
53 |
Correct |
260 ms |
4024 KB |
Output is correct |
54 |
Correct |
3 ms |
2260 KB |
Output is correct |
55 |
Correct |
4 ms |
2772 KB |
Output is correct |
56 |
Correct |
11 ms |
3136 KB |
Output is correct |
57 |
Correct |
28 ms |
10956 KB |
Output is correct |
58 |
Correct |
11 ms |
3132 KB |
Output is correct |
59 |
Correct |
13 ms |
3492 KB |
Output is correct |
60 |
Correct |
13 ms |
4072 KB |
Output is correct |
61 |
Correct |
93 ms |
4388 KB |
Output is correct |
62 |
Correct |
52 ms |
8448 KB |
Output is correct |
63 |
Correct |
62 ms |
24764 KB |
Output is correct |
64 |
Correct |
63 ms |
33284 KB |
Output is correct |
65 |
Correct |
87 ms |
44184 KB |
Output is correct |
66 |
Correct |
178 ms |
71856 KB |
Output is correct |
67 |
Correct |
171 ms |
72020 KB |
Output is correct |