#include "bits/stdc++.h"
using namespace std;
const int MAXN = 3e4+5;
vector< vector < pair<int, int> > > adj(MAXN);
vector< set < int > > doges(MAXN);
int dp[MAXN];
//map< pair<int, int>, bool > done;
//map< pair<int, int>, int> max_road;
bool visited[MAXN];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
int goal, begin;
for(int i = 0; i < m; i++)
{
pair<int, int> temp;
cin >> temp.first >> temp.second;
if(i == 1)
goal = temp.first;
else if(i == 0)
begin = temp.first;
doges[temp.first].insert(temp.second);
//done[{temp.first, temp.second}] = true;
}
for(int i = 0; i <= n; i++) {
dp[i] = 1e9;
//visited[i] = false;
}
//visited[begin] = false;
dp[begin] = 0;
set< pair<int, int> > curr;
curr.insert({0, begin});
int ans = 1e9;
for(int i = 0; i < n; i++)
{
for(int e : doges[i])
{
//cout << i << " " << e << "\n";
int cnt = 1;
int currNode = e + i;
while(currNode < n)
{
//cout << i << " " << e << " " << currNode << "\n";
adj[i].push_back({cnt, currNode});
if(doges[currNode].find(e) != doges[currNode].end())
break;
cnt++;
currNode += e;
}
cnt = 1;
currNode = i - e;
while(currNode >= 0)
{
//cout << i << " " << e << " " << currNode << "\n";
adj[i].push_back({cnt, currNode});
if(doges[currNode].find(e) != doges[currNode].end())
break;
cnt++;
currNode -= e;
}
}
}
while(!curr.empty())
{
pair<int, int> node = *(curr.begin());
int v = node.second;
curr.erase(curr.begin());
//int max_road[n];
visited[v] = true;
for(pair<int, int> to : adj[v])
{
if(dp[to.second] > dp[v] + to.first)
{
curr.erase({dp[to.second], to.second});
dp[to.second] = dp[v] + to.first;
curr.insert({dp[to.second], to.second});
}
}
}
ans = dp[goal];
if(ans >= 1e9)
ans = -1;
cout << ans << "\n";
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:100:9: warning: 'goal' may be used uninitialized in this function [-Wmaybe-uninitialized]
100 | ans = dp[goal];
| ~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2512 KB |
Output is correct |
2 |
Correct |
2 ms |
2452 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2340 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2456 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2456 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2452 KB |
Output is correct |
6 |
Correct |
2 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2432 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 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 |
2464 KB |
Output is correct |
14 |
Correct |
3 ms |
2516 KB |
Output is correct |
15 |
Correct |
3 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 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 |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2456 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
3 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2456 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 |
2464 KB |
Output is correct |
14 |
Correct |
3 ms |
2596 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 |
2724 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2460 KB |
Output is correct |
20 |
Correct |
2 ms |
2596 KB |
Output is correct |
21 |
Correct |
2 ms |
2472 KB |
Output is correct |
22 |
Correct |
2 ms |
2516 KB |
Output is correct |
23 |
Correct |
3 ms |
2588 KB |
Output is correct |
24 |
Correct |
3 ms |
2644 KB |
Output is correct |
25 |
Correct |
3 ms |
2644 KB |
Output is correct |
26 |
Correct |
3 ms |
2588 KB |
Output is correct |
27 |
Correct |
2 ms |
2516 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
6 ms |
3156 KB |
Output is correct |
30 |
Correct |
3 ms |
2712 KB |
Output is correct |
31 |
Correct |
5 ms |
2900 KB |
Output is correct |
32 |
Correct |
4 ms |
2772 KB |
Output is correct |
33 |
Correct |
9 ms |
3668 KB |
Output is correct |
34 |
Correct |
10 ms |
3668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2456 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 |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2456 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 |
2388 KB |
Output is correct |
11 |
Correct |
3 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
2464 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 |
2516 KB |
Output is correct |
17 |
Correct |
3 ms |
2680 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2440 KB |
Output is correct |
20 |
Correct |
2 ms |
2516 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
2464 KB |
Output is correct |
23 |
Correct |
2 ms |
2516 KB |
Output is correct |
24 |
Correct |
3 ms |
2724 KB |
Output is correct |
25 |
Correct |
4 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2600 KB |
Output is correct |
27 |
Correct |
2 ms |
2592 KB |
Output is correct |
28 |
Correct |
3 ms |
2724 KB |
Output is correct |
29 |
Correct |
6 ms |
3156 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
4 ms |
2840 KB |
Output is correct |
32 |
Correct |
3 ms |
2772 KB |
Output is correct |
33 |
Correct |
8 ms |
3616 KB |
Output is correct |
34 |
Correct |
8 ms |
3668 KB |
Output is correct |
35 |
Correct |
16 ms |
4916 KB |
Output is correct |
36 |
Correct |
4 ms |
2900 KB |
Output is correct |
37 |
Correct |
17 ms |
5492 KB |
Output is correct |
38 |
Correct |
28 ms |
5788 KB |
Output is correct |
39 |
Correct |
21 ms |
5860 KB |
Output is correct |
40 |
Correct |
27 ms |
5844 KB |
Output is correct |
41 |
Correct |
20 ms |
5796 KB |
Output is correct |
42 |
Correct |
6 ms |
2800 KB |
Output is correct |
43 |
Correct |
6 ms |
2676 KB |
Output is correct |
44 |
Correct |
6 ms |
2724 KB |
Output is correct |
45 |
Correct |
23 ms |
8236 KB |
Output is correct |
46 |
Correct |
23 ms |
8228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2460 KB |
Output is correct |
2 |
Correct |
2 ms |
2512 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2456 KB |
Output is correct |
7 |
Correct |
1 ms |
2456 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 |
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 |
2464 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 |
2516 KB |
Output is correct |
17 |
Correct |
3 ms |
2644 KB |
Output is correct |
18 |
Correct |
3 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2592 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
2516 KB |
Output is correct |
23 |
Correct |
2 ms |
2592 KB |
Output is correct |
24 |
Correct |
4 ms |
2724 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 |
2596 KB |
Output is correct |
28 |
Correct |
3 ms |
2800 KB |
Output is correct |
29 |
Correct |
6 ms |
3156 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
4 ms |
2900 KB |
Output is correct |
32 |
Correct |
3 ms |
2772 KB |
Output is correct |
33 |
Correct |
8 ms |
3612 KB |
Output is correct |
34 |
Correct |
8 ms |
3668 KB |
Output is correct |
35 |
Correct |
15 ms |
4904 KB |
Output is correct |
36 |
Correct |
4 ms |
2900 KB |
Output is correct |
37 |
Correct |
22 ms |
5480 KB |
Output is correct |
38 |
Correct |
21 ms |
5764 KB |
Output is correct |
39 |
Correct |
22 ms |
5844 KB |
Output is correct |
40 |
Correct |
21 ms |
5844 KB |
Output is correct |
41 |
Correct |
20 ms |
5792 KB |
Output is correct |
42 |
Correct |
8 ms |
2772 KB |
Output is correct |
43 |
Correct |
5 ms |
2772 KB |
Output is correct |
44 |
Correct |
7 ms |
2772 KB |
Output is correct |
45 |
Correct |
23 ms |
8276 KB |
Output is correct |
46 |
Correct |
23 ms |
8284 KB |
Output is correct |
47 |
Correct |
51 ms |
12796 KB |
Output is correct |
48 |
Correct |
15 ms |
6224 KB |
Output is correct |
49 |
Correct |
17 ms |
5204 KB |
Output is correct |
50 |
Correct |
10 ms |
4908 KB |
Output is correct |
51 |
Correct |
58 ms |
8880 KB |
Output is correct |
52 |
Correct |
54 ms |
9036 KB |
Output is correct |
53 |
Correct |
23 ms |
6300 KB |
Output is correct |
54 |
Correct |
6 ms |
3484 KB |
Output is correct |
55 |
Correct |
7 ms |
3864 KB |
Output is correct |
56 |
Correct |
12 ms |
5156 KB |
Output is correct |
57 |
Correct |
56 ms |
11744 KB |
Output is correct |
58 |
Correct |
12 ms |
4172 KB |
Output is correct |
59 |
Correct |
13 ms |
4560 KB |
Output is correct |
60 |
Correct |
17 ms |
5200 KB |
Output is correct |
61 |
Correct |
16 ms |
4648 KB |
Output is correct |
62 |
Correct |
24 ms |
9428 KB |
Output is correct |
63 |
Correct |
96 ms |
25508 KB |
Output is correct |
64 |
Correct |
108 ms |
29024 KB |
Output is correct |
65 |
Correct |
152 ms |
38264 KB |
Output is correct |
66 |
Correct |
261 ms |
63236 KB |
Output is correct |
67 |
Correct |
263 ms |
63068 KB |
Output is correct |