#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;
const int inf = int(1e9)+5, maxn = int(3e4)+5;
int B[maxn], P[maxn], D[maxn][2];
vector<pair<int, int>> doge[maxn];
// 0 => doge
// 1 => skyscraper
bool go(int x, int y)
{
if(B[x] == y) return 0;
auto it = lower_bound(doge[y].begin(), doge[y].end(), make_pair(P[x], -1));
return (it != doge[y].end() && it->first == P[x]);
}
int main(void)
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0;i < m;i++)
{
scanf("%d%d", &B[i], &P[i]), D[i][0] = inf;
doge[B[i]].push_back({P[i], i});
}
for(int i = 0;i < n;i++) sort(doge[i].begin(), doge[i].end()), D[i][1] = inf;
set<pair<int, pair<int, int>>> Q;
D[0][0] = 0;
Q.insert({0, {0, 0}});
while(!Q.empty())
{
pair<int, pair<int, int>> top = *Q.begin();
Q.erase(Q.begin());
if(top.second == make_pair(1, 0))
{
printf("%d\n", top.first);
return 0;
}
//cout << top.first << " " << top.second.first << " " << top.second.second << "\n";
if(top.second.second)
{
for(auto it: doge[top.second.first])
{
int node = it.second;
if(top.first < D[node][0])
{
if(D[node][0] != inf) Q.erase({D[node][0], {node, 0}});
D[node][0] = top.first;
Q.insert({D[node][0], {node, 0}});
}
}
}
else
{
auto it = lower_bound(doge[B[top.second.first]].begin(), doge[B[top.second.first]].end(), make_pair(P[top.second.first], -1));
if(it->second != top.second.first) continue;
for(int i = B[top.second.first];i < n;i += P[top.second.first])
{
int c = top.first+(abs(i-B[top.second.first])/P[top.second.first]);
if(c < D[i][1])
{
if(D[i][1] != inf) Q.erase({D[i][1], {i, 1}});
D[i][1] = c;
Q.insert({D[i][1], {i, 1}});
}
if(go(top.second.first, i)) break;
}
for(int i = B[top.second.first];i >= 0;i -= P[top.second.first])
{
int c = top.first+(abs(i-B[top.second.first])/P[top.second.first]);
if(c < D[i][1])
{
if(D[i][1] != inf) Q.erase({D[i][1], {i, 1}});
D[i][1] = c;
Q.insert({D[i][1], {i, 1}});
}
if(go(top.second.first, i)) break;
}
}
}
printf("%d\n", -1);
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:27:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
^
skyscraper.cpp:30:51: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &B[i], &P[i]), D[i][0] = inf;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3200 KB |
Output is correct |
2 |
Correct |
0 ms |
3200 KB |
Output is correct |
3 |
Correct |
0 ms |
3200 KB |
Output is correct |
4 |
Correct |
0 ms |
3200 KB |
Output is correct |
5 |
Correct |
0 ms |
3200 KB |
Output is correct |
6 |
Correct |
0 ms |
3200 KB |
Output is correct |
7 |
Correct |
0 ms |
3200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3200 KB |
Output is correct |
2 |
Correct |
0 ms |
3200 KB |
Output is correct |
3 |
Correct |
0 ms |
3200 KB |
Output is correct |
4 |
Correct |
0 ms |
3200 KB |
Output is correct |
5 |
Correct |
0 ms |
3200 KB |
Output is correct |
6 |
Correct |
0 ms |
3200 KB |
Output is correct |
7 |
Correct |
0 ms |
3200 KB |
Output is correct |
8 |
Correct |
0 ms |
3200 KB |
Output is correct |
9 |
Correct |
0 ms |
3200 KB |
Output is correct |
10 |
Correct |
0 ms |
3200 KB |
Output is correct |
11 |
Correct |
0 ms |
3200 KB |
Output is correct |
12 |
Correct |
0 ms |
3332 KB |
Output is correct |
13 |
Correct |
0 ms |
3332 KB |
Output is correct |
14 |
Correct |
0 ms |
3332 KB |
Output is correct |
15 |
Correct |
3 ms |
3332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3200 KB |
Output is correct |
2 |
Correct |
0 ms |
3200 KB |
Output is correct |
3 |
Correct |
0 ms |
3200 KB |
Output is correct |
4 |
Correct |
0 ms |
3200 KB |
Output is correct |
5 |
Correct |
0 ms |
3200 KB |
Output is correct |
6 |
Correct |
0 ms |
3200 KB |
Output is correct |
7 |
Correct |
0 ms |
3200 KB |
Output is correct |
8 |
Correct |
0 ms |
3200 KB |
Output is correct |
9 |
Correct |
0 ms |
3200 KB |
Output is correct |
10 |
Correct |
0 ms |
3200 KB |
Output is correct |
11 |
Correct |
0 ms |
3200 KB |
Output is correct |
12 |
Correct |
0 ms |
3332 KB |
Output is correct |
13 |
Correct |
0 ms |
3332 KB |
Output is correct |
14 |
Correct |
0 ms |
3332 KB |
Output is correct |
15 |
Correct |
0 ms |
3332 KB |
Output is correct |
16 |
Correct |
0 ms |
3200 KB |
Output is correct |
17 |
Correct |
0 ms |
3332 KB |
Output is correct |
18 |
Correct |
0 ms |
3200 KB |
Output is correct |
19 |
Correct |
0 ms |
3200 KB |
Output is correct |
20 |
Correct |
0 ms |
3332 KB |
Output is correct |
21 |
Correct |
0 ms |
3200 KB |
Output is correct |
22 |
Correct |
0 ms |
3200 KB |
Output is correct |
23 |
Correct |
3 ms |
3332 KB |
Output is correct |
24 |
Correct |
3 ms |
3332 KB |
Output is correct |
25 |
Correct |
0 ms |
3332 KB |
Output is correct |
26 |
Correct |
0 ms |
3332 KB |
Output is correct |
27 |
Correct |
0 ms |
3332 KB |
Output is correct |
28 |
Correct |
0 ms |
3332 KB |
Output is correct |
29 |
Correct |
6 ms |
3332 KB |
Output is correct |
30 |
Correct |
0 ms |
3332 KB |
Output is correct |
31 |
Correct |
3 ms |
3332 KB |
Output is correct |
32 |
Correct |
0 ms |
3332 KB |
Output is correct |
33 |
Correct |
6 ms |
3464 KB |
Output is correct |
34 |
Correct |
6 ms |
3464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3200 KB |
Output is correct |
2 |
Correct |
0 ms |
3200 KB |
Output is correct |
3 |
Correct |
0 ms |
3200 KB |
Output is correct |
4 |
Correct |
0 ms |
3200 KB |
Output is correct |
5 |
Correct |
0 ms |
3200 KB |
Output is correct |
6 |
Correct |
0 ms |
3200 KB |
Output is correct |
7 |
Correct |
0 ms |
3200 KB |
Output is correct |
8 |
Correct |
0 ms |
3200 KB |
Output is correct |
9 |
Correct |
0 ms |
3200 KB |
Output is correct |
10 |
Correct |
0 ms |
3200 KB |
Output is correct |
11 |
Correct |
0 ms |
3200 KB |
Output is correct |
12 |
Correct |
0 ms |
3332 KB |
Output is correct |
13 |
Correct |
0 ms |
3332 KB |
Output is correct |
14 |
Correct |
0 ms |
3332 KB |
Output is correct |
15 |
Correct |
0 ms |
3332 KB |
Output is correct |
16 |
Correct |
0 ms |
3200 KB |
Output is correct |
17 |
Correct |
0 ms |
3332 KB |
Output is correct |
18 |
Correct |
0 ms |
3200 KB |
Output is correct |
19 |
Correct |
0 ms |
3200 KB |
Output is correct |
20 |
Correct |
0 ms |
3332 KB |
Output is correct |
21 |
Correct |
0 ms |
3200 KB |
Output is correct |
22 |
Correct |
0 ms |
3200 KB |
Output is correct |
23 |
Correct |
0 ms |
3332 KB |
Output is correct |
24 |
Correct |
0 ms |
3332 KB |
Output is correct |
25 |
Correct |
0 ms |
3332 KB |
Output is correct |
26 |
Correct |
0 ms |
3332 KB |
Output is correct |
27 |
Correct |
0 ms |
3332 KB |
Output is correct |
28 |
Correct |
0 ms |
3332 KB |
Output is correct |
29 |
Correct |
3 ms |
3332 KB |
Output is correct |
30 |
Correct |
0 ms |
3332 KB |
Output is correct |
31 |
Correct |
3 ms |
3332 KB |
Output is correct |
32 |
Correct |
0 ms |
3332 KB |
Output is correct |
33 |
Correct |
6 ms |
3464 KB |
Output is correct |
34 |
Correct |
6 ms |
3464 KB |
Output is correct |
35 |
Correct |
19 ms |
4520 KB |
Output is correct |
36 |
Correct |
0 ms |
3332 KB |
Output is correct |
37 |
Correct |
16 ms |
4124 KB |
Output is correct |
38 |
Correct |
13 ms |
3992 KB |
Output is correct |
39 |
Correct |
13 ms |
3596 KB |
Output is correct |
40 |
Correct |
16 ms |
3732 KB |
Output is correct |
41 |
Correct |
9 ms |
3860 KB |
Output is correct |
42 |
Correct |
6 ms |
3672 KB |
Output is correct |
43 |
Correct |
13 ms |
5388 KB |
Output is correct |
44 |
Correct |
23 ms |
5272 KB |
Output is correct |
45 |
Correct |
43 ms |
5444 KB |
Output is correct |
46 |
Correct |
33 ms |
5444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3200 KB |
Output is correct |
2 |
Correct |
0 ms |
3200 KB |
Output is correct |
3 |
Correct |
0 ms |
3200 KB |
Output is correct |
4 |
Correct |
0 ms |
3200 KB |
Output is correct |
5 |
Correct |
0 ms |
3200 KB |
Output is correct |
6 |
Correct |
0 ms |
3200 KB |
Output is correct |
7 |
Correct |
0 ms |
3200 KB |
Output is correct |
8 |
Correct |
0 ms |
3200 KB |
Output is correct |
9 |
Correct |
0 ms |
3200 KB |
Output is correct |
10 |
Correct |
0 ms |
3200 KB |
Output is correct |
11 |
Correct |
0 ms |
3200 KB |
Output is correct |
12 |
Correct |
0 ms |
3332 KB |
Output is correct |
13 |
Correct |
0 ms |
3332 KB |
Output is correct |
14 |
Correct |
0 ms |
3332 KB |
Output is correct |
15 |
Correct |
0 ms |
3332 KB |
Output is correct |
16 |
Correct |
0 ms |
3200 KB |
Output is correct |
17 |
Correct |
3 ms |
3332 KB |
Output is correct |
18 |
Correct |
0 ms |
3200 KB |
Output is correct |
19 |
Correct |
0 ms |
3200 KB |
Output is correct |
20 |
Correct |
3 ms |
3332 KB |
Output is correct |
21 |
Correct |
0 ms |
3200 KB |
Output is correct |
22 |
Correct |
0 ms |
3200 KB |
Output is correct |
23 |
Correct |
3 ms |
3332 KB |
Output is correct |
24 |
Correct |
0 ms |
3332 KB |
Output is correct |
25 |
Correct |
0 ms |
3332 KB |
Output is correct |
26 |
Correct |
0 ms |
3332 KB |
Output is correct |
27 |
Correct |
0 ms |
3332 KB |
Output is correct |
28 |
Correct |
0 ms |
3332 KB |
Output is correct |
29 |
Correct |
3 ms |
3332 KB |
Output is correct |
30 |
Correct |
0 ms |
3332 KB |
Output is correct |
31 |
Correct |
3 ms |
3332 KB |
Output is correct |
32 |
Correct |
0 ms |
3332 KB |
Output is correct |
33 |
Correct |
6 ms |
3464 KB |
Output is correct |
34 |
Correct |
3 ms |
3464 KB |
Output is correct |
35 |
Correct |
16 ms |
4520 KB |
Output is correct |
36 |
Correct |
3 ms |
3332 KB |
Output is correct |
37 |
Correct |
9 ms |
4124 KB |
Output is correct |
38 |
Correct |
9 ms |
3992 KB |
Output is correct |
39 |
Correct |
13 ms |
3596 KB |
Output is correct |
40 |
Correct |
9 ms |
3732 KB |
Output is correct |
41 |
Correct |
19 ms |
3860 KB |
Output is correct |
42 |
Correct |
13 ms |
3672 KB |
Output is correct |
43 |
Correct |
23 ms |
5388 KB |
Output is correct |
44 |
Correct |
19 ms |
5272 KB |
Output is correct |
45 |
Correct |
43 ms |
5444 KB |
Output is correct |
46 |
Correct |
43 ms |
5444 KB |
Output is correct |
47 |
Correct |
43 ms |
4388 KB |
Output is correct |
48 |
Correct |
9 ms |
3728 KB |
Output is correct |
49 |
Correct |
13 ms |
3728 KB |
Output is correct |
50 |
Correct |
9 ms |
3596 KB |
Output is correct |
51 |
Correct |
86 ms |
5576 KB |
Output is correct |
52 |
Correct |
66 ms |
5312 KB |
Output is correct |
53 |
Correct |
19 ms |
4784 KB |
Output is correct |
54 |
Correct |
3 ms |
4124 KB |
Output is correct |
55 |
Correct |
3 ms |
4124 KB |
Output is correct |
56 |
Correct |
13 ms |
4124 KB |
Output is correct |
57 |
Correct |
6 ms |
4256 KB |
Output is correct |
58 |
Correct |
19 ms |
5912 KB |
Output is correct |
59 |
Correct |
6 ms |
4460 KB |
Output is correct |
60 |
Correct |
13 ms |
4468 KB |
Output is correct |
61 |
Correct |
13 ms |
5276 KB |
Output is correct |
62 |
Correct |
26 ms |
4124 KB |
Output is correct |
63 |
Correct |
113 ms |
5708 KB |
Output is correct |
64 |
Correct |
106 ms |
5576 KB |
Output is correct |
65 |
Correct |
149 ms |
5972 KB |
Output is correct |
66 |
Correct |
273 ms |
7160 KB |
Output is correct |
67 |
Correct |
256 ms |
7292 KB |
Output is correct |