#include <bits/stdc++.h>
#define st first
#define nd second
using namespace std;
const long long INF = 1e9;
const int MOD = 1e9+7;
const int MAXN = 30000;
int n, m, strt, nd;
vector < set <int> > v(MAXN+1);
vector < vector < pair<int,int> > > g(MAXN+1);
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
cin >> n >> m;
for(int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
if(i == 0) strt = x;
if(i == 1) nd = x;
v[x].insert(y);
}
for(int i = 0; i < n; i++){
set <int> :: iterator it;
for(it = v[i].begin(); it != v[i].end(); it++){
int p = *it;
int f = i-p;
int pr = 1;
while(f >= 0) {
g[i].push_back({f,pr});
if(v[f].count(p) != 0) break;
f -= p;
pr++;
}
f = i+p;
pr = 1;
while(f < n) {
g[i].push_back({f,pr});
if(v[f].count(p) != 0) break;
f += p;
pr++;
}
}
}
/* for(int i = 0; i < n; i++){
cout << i <<" : ";
for(int j = 0; j < g[i].size(); j++)
cout << g[i][j].first <<"("<<g[i][j].second<<") ";
cout << endl;
}*/
set < pair<int,int> > st;
set < pair<int,int> > :: iterator it;
vector < int > dis(n+1,INF);
dis[strt] = 0;
st.insert({0,strt});
while(!st.empty()) {
it = st.begin();
int plz = it->second;
dis[plz] = it->first;
//cout << plz << " " << dis[plz] << endl;
if(plz == nd){
cout << dis[plz];
return 0;
}
st.erase(it);
for(int i = 0; i < g[plz].size(); i++) {
int to = g[plz][i].first;
int pr = g[plz][i].second;
if(dis[to] <= dis[plz]+pr) continue;
st.insert({dis[plz]+pr,to});
dis[to] = dis[plz]+pr;
}
while(!st.empty() && dis[st.begin()->second] < st.begin()->first)
st.erase(st.begin());
}
cout << -1;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:80:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < g[plz].size(); i++) {
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2424 KB |
Output is correct |
2 |
Correct |
3 ms |
2528 KB |
Output is correct |
3 |
Correct |
3 ms |
2600 KB |
Output is correct |
4 |
Correct |
3 ms |
2600 KB |
Output is correct |
5 |
Correct |
3 ms |
2600 KB |
Output is correct |
6 |
Correct |
3 ms |
2600 KB |
Output is correct |
7 |
Correct |
3 ms |
2696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2696 KB |
Output is correct |
2 |
Correct |
3 ms |
2696 KB |
Output is correct |
3 |
Correct |
3 ms |
2696 KB |
Output is correct |
4 |
Correct |
3 ms |
2696 KB |
Output is correct |
5 |
Correct |
3 ms |
2696 KB |
Output is correct |
6 |
Correct |
3 ms |
2696 KB |
Output is correct |
7 |
Correct |
3 ms |
2696 KB |
Output is correct |
8 |
Correct |
3 ms |
2696 KB |
Output is correct |
9 |
Correct |
3 ms |
2696 KB |
Output is correct |
10 |
Correct |
3 ms |
2700 KB |
Output is correct |
11 |
Correct |
4 ms |
2956 KB |
Output is correct |
12 |
Correct |
4 ms |
2956 KB |
Output is correct |
13 |
Correct |
4 ms |
2956 KB |
Output is correct |
14 |
Correct |
5 ms |
3028 KB |
Output is correct |
15 |
Correct |
4 ms |
3056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3056 KB |
Output is correct |
2 |
Correct |
3 ms |
3056 KB |
Output is correct |
3 |
Correct |
3 ms |
3056 KB |
Output is correct |
4 |
Correct |
3 ms |
3056 KB |
Output is correct |
5 |
Correct |
3 ms |
3056 KB |
Output is correct |
6 |
Correct |
3 ms |
3056 KB |
Output is correct |
7 |
Correct |
4 ms |
3056 KB |
Output is correct |
8 |
Correct |
3 ms |
3056 KB |
Output is correct |
9 |
Correct |
3 ms |
3056 KB |
Output is correct |
10 |
Correct |
3 ms |
3056 KB |
Output is correct |
11 |
Correct |
4 ms |
3060 KB |
Output is correct |
12 |
Correct |
4 ms |
3060 KB |
Output is correct |
13 |
Correct |
3 ms |
3060 KB |
Output is correct |
14 |
Correct |
4 ms |
3144 KB |
Output is correct |
15 |
Correct |
4 ms |
3160 KB |
Output is correct |
16 |
Correct |
4 ms |
3160 KB |
Output is correct |
17 |
Correct |
5 ms |
3304 KB |
Output is correct |
18 |
Correct |
4 ms |
3304 KB |
Output is correct |
19 |
Correct |
3 ms |
3304 KB |
Output is correct |
20 |
Correct |
4 ms |
3304 KB |
Output is correct |
21 |
Correct |
4 ms |
3304 KB |
Output is correct |
22 |
Correct |
4 ms |
3304 KB |
Output is correct |
23 |
Correct |
4 ms |
3304 KB |
Output is correct |
24 |
Correct |
7 ms |
3388 KB |
Output is correct |
25 |
Correct |
4 ms |
3388 KB |
Output is correct |
26 |
Correct |
4 ms |
3388 KB |
Output is correct |
27 |
Correct |
5 ms |
3388 KB |
Output is correct |
28 |
Correct |
6 ms |
3460 KB |
Output is correct |
29 |
Correct |
8 ms |
4376 KB |
Output is correct |
30 |
Correct |
5 ms |
4376 KB |
Output is correct |
31 |
Correct |
6 ms |
4376 KB |
Output is correct |
32 |
Correct |
6 ms |
4376 KB |
Output is correct |
33 |
Correct |
14 ms |
5288 KB |
Output is correct |
34 |
Correct |
11 ms |
5300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5300 KB |
Output is correct |
2 |
Correct |
4 ms |
5300 KB |
Output is correct |
3 |
Correct |
3 ms |
5300 KB |
Output is correct |
4 |
Correct |
3 ms |
5300 KB |
Output is correct |
5 |
Correct |
3 ms |
5300 KB |
Output is correct |
6 |
Correct |
3 ms |
5300 KB |
Output is correct |
7 |
Correct |
3 ms |
5300 KB |
Output is correct |
8 |
Correct |
3 ms |
5300 KB |
Output is correct |
9 |
Correct |
4 ms |
5300 KB |
Output is correct |
10 |
Correct |
3 ms |
5300 KB |
Output is correct |
11 |
Correct |
4 ms |
5300 KB |
Output is correct |
12 |
Correct |
4 ms |
5300 KB |
Output is correct |
13 |
Correct |
4 ms |
5300 KB |
Output is correct |
14 |
Correct |
4 ms |
5300 KB |
Output is correct |
15 |
Correct |
4 ms |
5300 KB |
Output is correct |
16 |
Correct |
4 ms |
5300 KB |
Output is correct |
17 |
Correct |
5 ms |
5300 KB |
Output is correct |
18 |
Correct |
4 ms |
5300 KB |
Output is correct |
19 |
Correct |
3 ms |
5300 KB |
Output is correct |
20 |
Correct |
4 ms |
5300 KB |
Output is correct |
21 |
Correct |
4 ms |
5300 KB |
Output is correct |
22 |
Correct |
3 ms |
5300 KB |
Output is correct |
23 |
Correct |
4 ms |
5300 KB |
Output is correct |
24 |
Correct |
5 ms |
5300 KB |
Output is correct |
25 |
Correct |
4 ms |
5300 KB |
Output is correct |
26 |
Correct |
4 ms |
5300 KB |
Output is correct |
27 |
Correct |
4 ms |
5300 KB |
Output is correct |
28 |
Correct |
5 ms |
5300 KB |
Output is correct |
29 |
Correct |
11 ms |
5300 KB |
Output is correct |
30 |
Correct |
5 ms |
5300 KB |
Output is correct |
31 |
Correct |
5 ms |
5300 KB |
Output is correct |
32 |
Correct |
5 ms |
5300 KB |
Output is correct |
33 |
Correct |
11 ms |
5600 KB |
Output is correct |
34 |
Correct |
11 ms |
5612 KB |
Output is correct |
35 |
Correct |
24 ms |
6104 KB |
Output is correct |
36 |
Correct |
7 ms |
6104 KB |
Output is correct |
37 |
Correct |
27 ms |
6912 KB |
Output is correct |
38 |
Correct |
32 ms |
7456 KB |
Output is correct |
39 |
Correct |
30 ms |
7592 KB |
Output is correct |
40 |
Correct |
28 ms |
7856 KB |
Output is correct |
41 |
Correct |
31 ms |
8376 KB |
Output is correct |
42 |
Correct |
9 ms |
8376 KB |
Output is correct |
43 |
Correct |
8 ms |
8376 KB |
Output is correct |
44 |
Correct |
8 ms |
8376 KB |
Output is correct |
45 |
Correct |
37 ms |
12208 KB |
Output is correct |
46 |
Correct |
35 ms |
12420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12420 KB |
Output is correct |
2 |
Correct |
3 ms |
12420 KB |
Output is correct |
3 |
Correct |
3 ms |
12420 KB |
Output is correct |
4 |
Correct |
3 ms |
12420 KB |
Output is correct |
5 |
Correct |
3 ms |
12420 KB |
Output is correct |
6 |
Correct |
3 ms |
12420 KB |
Output is correct |
7 |
Correct |
3 ms |
12420 KB |
Output is correct |
8 |
Correct |
3 ms |
12420 KB |
Output is correct |
9 |
Correct |
3 ms |
12420 KB |
Output is correct |
10 |
Correct |
4 ms |
12420 KB |
Output is correct |
11 |
Correct |
4 ms |
12420 KB |
Output is correct |
12 |
Correct |
3 ms |
12420 KB |
Output is correct |
13 |
Correct |
4 ms |
12420 KB |
Output is correct |
14 |
Correct |
4 ms |
12420 KB |
Output is correct |
15 |
Correct |
4 ms |
12420 KB |
Output is correct |
16 |
Correct |
3 ms |
12420 KB |
Output is correct |
17 |
Correct |
5 ms |
12420 KB |
Output is correct |
18 |
Correct |
4 ms |
12420 KB |
Output is correct |
19 |
Correct |
3 ms |
12420 KB |
Output is correct |
20 |
Correct |
4 ms |
12420 KB |
Output is correct |
21 |
Correct |
4 ms |
12420 KB |
Output is correct |
22 |
Correct |
3 ms |
12420 KB |
Output is correct |
23 |
Correct |
4 ms |
12420 KB |
Output is correct |
24 |
Correct |
5 ms |
12420 KB |
Output is correct |
25 |
Correct |
5 ms |
12420 KB |
Output is correct |
26 |
Correct |
4 ms |
12420 KB |
Output is correct |
27 |
Correct |
3 ms |
12420 KB |
Output is correct |
28 |
Correct |
5 ms |
12420 KB |
Output is correct |
29 |
Correct |
7 ms |
12420 KB |
Output is correct |
30 |
Correct |
6 ms |
12420 KB |
Output is correct |
31 |
Correct |
5 ms |
12420 KB |
Output is correct |
32 |
Correct |
6 ms |
12420 KB |
Output is correct |
33 |
Correct |
10 ms |
12420 KB |
Output is correct |
34 |
Correct |
10 ms |
12420 KB |
Output is correct |
35 |
Correct |
24 ms |
12420 KB |
Output is correct |
36 |
Correct |
6 ms |
12420 KB |
Output is correct |
37 |
Correct |
25 ms |
12420 KB |
Output is correct |
38 |
Correct |
30 ms |
12420 KB |
Output is correct |
39 |
Correct |
31 ms |
12420 KB |
Output is correct |
40 |
Correct |
31 ms |
12420 KB |
Output is correct |
41 |
Correct |
28 ms |
12420 KB |
Output is correct |
42 |
Correct |
8 ms |
12420 KB |
Output is correct |
43 |
Correct |
9 ms |
12420 KB |
Output is correct |
44 |
Correct |
8 ms |
12420 KB |
Output is correct |
45 |
Correct |
32 ms |
15060 KB |
Output is correct |
46 |
Correct |
36 ms |
15272 KB |
Output is correct |
47 |
Correct |
66 ms |
20348 KB |
Output is correct |
48 |
Correct |
26 ms |
20348 KB |
Output is correct |
49 |
Correct |
18 ms |
20348 KB |
Output is correct |
50 |
Correct |
17 ms |
20348 KB |
Output is correct |
51 |
Correct |
67 ms |
20348 KB |
Output is correct |
52 |
Correct |
58 ms |
20348 KB |
Output is correct |
53 |
Correct |
21 ms |
20348 KB |
Output is correct |
54 |
Correct |
7 ms |
20348 KB |
Output is correct |
55 |
Correct |
10 ms |
20348 KB |
Output is correct |
56 |
Correct |
17 ms |
20348 KB |
Output is correct |
57 |
Correct |
26 ms |
20348 KB |
Output is correct |
58 |
Correct |
14 ms |
20348 KB |
Output is correct |
59 |
Correct |
15 ms |
20348 KB |
Output is correct |
60 |
Correct |
15 ms |
20348 KB |
Output is correct |
61 |
Correct |
18 ms |
20348 KB |
Output is correct |
62 |
Correct |
32 ms |
20348 KB |
Output is correct |
63 |
Correct |
128 ms |
40204 KB |
Output is correct |
64 |
Correct |
148 ms |
44036 KB |
Output is correct |
65 |
Correct |
194 ms |
55340 KB |
Output is correct |
66 |
Correct |
346 ms |
91888 KB |
Output is correct |
67 |
Correct |
324 ms |
92240 KB |
Output is correct |