#include<bits/stdc++.h>
using namespace std;
void fff(string s = "") {
cin >> s;
if ((int)s.size() >= 4) {
freopen(s.c_str(),"r",stdin);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef LOCAL
fff();
#endif
int n,m;
cin >> n >> m;
vector<pair<int,int>> a(m);
vector<vector<int>> inner(n);
for (int i = 0; i < m; i++) {
cin >> a[i].first >> a[i].second;
inner[a[i].first].push_back(a[i].second);
}
for (int i = 0; i < n; i++) {
sort(inner[i].begin(),inner[i].end());
inner[i].resize(unique(inner[i].begin(),inner[i].end()) - inner[i].begin());
}
const int inf = (int)2e9;
vector<int> d(n,inf);
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
d[a[0].first] = 0;
pq.emplace(0,a[0].first);
while ((int)pq.size()) {
auto x = pq.top();pq.pop();
if (x.first != d[x.second]) continue;
for (auto&step : inner[x.second]) {
for (int num = 1; x.second + num * step < n; num++) {
int nxt = x.second + num * step;
if (d[nxt] > x.first + num) {
d[nxt] = x.first + num;
pq.emplace(d[nxt],nxt);
}
auto it = lower_bound(inner[nxt].begin(),inner[nxt].end(),step);
if (it != inner[nxt].end() && *it == step) {
break;
}
}
for (int num = 1; x.second - num * step >= 0; num++) {
int nxt = x.second - num * step;
if (d[nxt] > x.first + num) {
d[nxt] = x.first + num;
pq.emplace(d[nxt],nxt);
}
auto it = lower_bound(inner[nxt].begin(),inner[nxt].end(),step);
if (it != inner[nxt].end() && *it == step) {
break;
}
}
}
}
if (d[a[1].first] == inf) d[a[1].first] = -1;
cout << d[a[1].first];
}
Compilation message
skyscraper.cpp: In function 'void fff(std::string)':
skyscraper.cpp:8:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
8 | freopen(s.c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
416 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
416 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
3 ms |
640 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
2 ms |
512 KB |
Output is correct |
32 |
Correct |
2 ms |
512 KB |
Output is correct |
33 |
Correct |
4 ms |
768 KB |
Output is correct |
34 |
Correct |
4 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
3 ms |
640 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
2 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
4 ms |
768 KB |
Output is correct |
34 |
Correct |
5 ms |
768 KB |
Output is correct |
35 |
Correct |
9 ms |
896 KB |
Output is correct |
36 |
Correct |
2 ms |
512 KB |
Output is correct |
37 |
Correct |
10 ms |
768 KB |
Output is correct |
38 |
Correct |
12 ms |
1024 KB |
Output is correct |
39 |
Correct |
12 ms |
1024 KB |
Output is correct |
40 |
Correct |
13 ms |
1024 KB |
Output is correct |
41 |
Correct |
14 ms |
1024 KB |
Output is correct |
42 |
Correct |
6 ms |
896 KB |
Output is correct |
43 |
Correct |
6 ms |
896 KB |
Output is correct |
44 |
Correct |
6 ms |
896 KB |
Output is correct |
45 |
Correct |
12 ms |
1152 KB |
Output is correct |
46 |
Correct |
12 ms |
1156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
3 ms |
640 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
2 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
5 ms |
896 KB |
Output is correct |
34 |
Correct |
4 ms |
768 KB |
Output is correct |
35 |
Correct |
9 ms |
832 KB |
Output is correct |
36 |
Correct |
2 ms |
512 KB |
Output is correct |
37 |
Correct |
11 ms |
768 KB |
Output is correct |
38 |
Correct |
12 ms |
1024 KB |
Output is correct |
39 |
Correct |
12 ms |
1024 KB |
Output is correct |
40 |
Correct |
12 ms |
1024 KB |
Output is correct |
41 |
Correct |
12 ms |
1024 KB |
Output is correct |
42 |
Correct |
6 ms |
896 KB |
Output is correct |
43 |
Correct |
6 ms |
896 KB |
Output is correct |
44 |
Correct |
5 ms |
896 KB |
Output is correct |
45 |
Correct |
12 ms |
1152 KB |
Output is correct |
46 |
Correct |
12 ms |
1156 KB |
Output is correct |
47 |
Correct |
29 ms |
1916 KB |
Output is correct |
48 |
Correct |
8 ms |
1664 KB |
Output is correct |
49 |
Correct |
7 ms |
1664 KB |
Output is correct |
50 |
Correct |
5 ms |
1536 KB |
Output is correct |
51 |
Correct |
32 ms |
2684 KB |
Output is correct |
52 |
Correct |
33 ms |
2684 KB |
Output is correct |
53 |
Correct |
17 ms |
2684 KB |
Output is correct |
54 |
Correct |
2 ms |
1408 KB |
Output is correct |
55 |
Correct |
3 ms |
1408 KB |
Output is correct |
56 |
Correct |
10 ms |
2304 KB |
Output is correct |
57 |
Correct |
27 ms |
1916 KB |
Output is correct |
58 |
Correct |
8 ms |
1920 KB |
Output is correct |
59 |
Correct |
9 ms |
2048 KB |
Output is correct |
60 |
Correct |
10 ms |
2048 KB |
Output is correct |
61 |
Correct |
10 ms |
2048 KB |
Output is correct |
62 |
Correct |
18 ms |
2688 KB |
Output is correct |
63 |
Correct |
44 ms |
4084 KB |
Output is correct |
64 |
Correct |
48 ms |
4084 KB |
Output is correct |
65 |
Correct |
59 ms |
4084 KB |
Output is correct |
66 |
Correct |
140 ms |
6000 KB |
Output is correct |
67 |
Correct |
123 ms |
6000 KB |
Output is correct |