#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
vector<ii> graph[30001];
set<int> doge[30001];
set<int> buildings;
//priority_queue<ii, vector<ii>, greater<ii> > pq;
int dist[30001];
inline void scanint(int &x)
{
register int c = getchar();
x = 0;
long long int neg = 0;
for(;((c<48 || c>57) && c != '-');c = getchar());
if(c=='-') {neg=1;c=getchar();}
for(;c>47 && c<58;c = getchar()) {x = (x<<1) + (x<<3) + c - 48;}
if(neg) x=-x;
}
int main()
{
//ios_base::sync_with_stdio(false); cin.tie(NULL);
int n, m;
scanint(n);
scanint(m);
int x, y;
int start, end;
for(int i = 0; i < m; i++)
{
scanint(x); scanint(y);
if(i == 0) start = x;
else if(i == 1) end = x;
doge[x].insert(y);
buildings.insert(x);
}
int pos; int power;
int tmp2;
int cnt;
for(set<int,int>::iterator it = buildings.begin(); it != buildings.end(); it++)
//for(pos = 0; pos < n; pos++)
{
pos = *it;
for(set<int,int>::iterator it2 = doge[pos].begin(); it2 != doge[pos].end(); it2++)
{
power = *it2;
tmp2 = power + pos;
cnt = 1;
while(tmp2 < n)
{
graph[pos].pb(ii(tmp2, cnt));
if(doge[tmp2].find(power) != doge[tmp2].end())
{
break;
}
cnt++;
tmp2 += power;
}
cnt = -1;
tmp2 = pos - power;
while(tmp2 >= 0)
{
graph[pos].pb(ii(tmp2, -cnt));
if(doge[tmp2].find(power) != doge[tmp2].end())
{
break;
}
cnt--;
tmp2 -= power;
}
}
}
//cout << start << " " << end << endl;
for(int i = 0; i < n; i++) dist[i] = 10000000;
int yy, yz;
dist[start] = 0;
queue<int> q;
set<int> qs;
q.push(start);
qs.insert(start);
int yx;
while(!q.empty())
{
yy = q.front();
q.pop(); qs.erase(yy);
for(int i = 0; i < graph[yy].size(); i++)
{
yz = graph[yy][i].fi; yx = graph[yy][i].se;
if(dist[yz] > dist[yy] + yx)
{
dist[yz] = dist[yy] + yx;
if(qs.find(yz) == qs.end()) {q.push(yz); qs.insert(yz);}
}
}
}
if(dist[end] >= 10000000)
{
cout << -1;
}
else cout << dist[end];
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:103:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < graph[yy].size(); i++)
^
skyscraper.cpp:113:13: warning: 'end' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(dist[end] >= 10000000)
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4252 KB |
Output is correct |
2 |
Correct |
0 ms |
4252 KB |
Output is correct |
3 |
Correct |
0 ms |
4252 KB |
Output is correct |
4 |
Correct |
0 ms |
4252 KB |
Output is correct |
5 |
Correct |
0 ms |
4252 KB |
Output is correct |
6 |
Correct |
0 ms |
4252 KB |
Output is correct |
7 |
Correct |
0 ms |
4252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4252 KB |
Output is correct |
2 |
Correct |
0 ms |
4252 KB |
Output is correct |
3 |
Correct |
0 ms |
4252 KB |
Output is correct |
4 |
Correct |
0 ms |
4252 KB |
Output is correct |
5 |
Correct |
0 ms |
4252 KB |
Output is correct |
6 |
Correct |
0 ms |
4252 KB |
Output is correct |
7 |
Correct |
0 ms |
4252 KB |
Output is correct |
8 |
Correct |
0 ms |
4252 KB |
Output is correct |
9 |
Correct |
0 ms |
4252 KB |
Output is correct |
10 |
Correct |
0 ms |
4252 KB |
Output is correct |
11 |
Correct |
0 ms |
4384 KB |
Output is correct |
12 |
Correct |
0 ms |
4252 KB |
Output is correct |
13 |
Correct |
0 ms |
4252 KB |
Output is correct |
14 |
Correct |
0 ms |
4384 KB |
Output is correct |
15 |
Correct |
0 ms |
4384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4252 KB |
Output is correct |
2 |
Correct |
0 ms |
4252 KB |
Output is correct |
3 |
Correct |
0 ms |
4252 KB |
Output is correct |
4 |
Correct |
0 ms |
4252 KB |
Output is correct |
5 |
Correct |
0 ms |
4252 KB |
Output is correct |
6 |
Correct |
0 ms |
4252 KB |
Output is correct |
7 |
Correct |
0 ms |
4252 KB |
Output is correct |
8 |
Correct |
0 ms |
4252 KB |
Output is correct |
9 |
Correct |
0 ms |
4252 KB |
Output is correct |
10 |
Correct |
0 ms |
4252 KB |
Output is correct |
11 |
Correct |
0 ms |
4384 KB |
Output is correct |
12 |
Correct |
0 ms |
4252 KB |
Output is correct |
13 |
Correct |
0 ms |
4252 KB |
Output is correct |
14 |
Correct |
3 ms |
4384 KB |
Output is correct |
15 |
Correct |
3 ms |
4384 KB |
Output is correct |
16 |
Correct |
0 ms |
4384 KB |
Output is correct |
17 |
Correct |
0 ms |
4516 KB |
Output is correct |
18 |
Correct |
3 ms |
4384 KB |
Output is correct |
19 |
Correct |
0 ms |
4384 KB |
Output is correct |
20 |
Correct |
0 ms |
4516 KB |
Output is correct |
21 |
Correct |
0 ms |
4384 KB |
Output is correct |
22 |
Correct |
0 ms |
4384 KB |
Output is correct |
23 |
Correct |
0 ms |
4384 KB |
Output is correct |
24 |
Correct |
3 ms |
4648 KB |
Output is correct |
25 |
Correct |
3 ms |
4520 KB |
Output is correct |
26 |
Correct |
3 ms |
4384 KB |
Output is correct |
27 |
Correct |
0 ms |
4388 KB |
Output is correct |
28 |
Correct |
3 ms |
4648 KB |
Output is correct |
29 |
Correct |
0 ms |
5128 KB |
Output is correct |
30 |
Correct |
0 ms |
4540 KB |
Output is correct |
31 |
Correct |
0 ms |
4788 KB |
Output is correct |
32 |
Correct |
0 ms |
4648 KB |
Output is correct |
33 |
Correct |
3 ms |
5808 KB |
Output is correct |
34 |
Correct |
6 ms |
5808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4252 KB |
Output is correct |
2 |
Correct |
0 ms |
4252 KB |
Output is correct |
3 |
Correct |
0 ms |
4252 KB |
Output is correct |
4 |
Correct |
0 ms |
4252 KB |
Output is correct |
5 |
Correct |
0 ms |
4252 KB |
Output is correct |
6 |
Correct |
0 ms |
4252 KB |
Output is correct |
7 |
Correct |
0 ms |
4252 KB |
Output is correct |
8 |
Correct |
0 ms |
4252 KB |
Output is correct |
9 |
Correct |
0 ms |
4252 KB |
Output is correct |
10 |
Correct |
0 ms |
4252 KB |
Output is correct |
11 |
Correct |
0 ms |
4384 KB |
Output is correct |
12 |
Correct |
0 ms |
4252 KB |
Output is correct |
13 |
Correct |
0 ms |
4252 KB |
Output is correct |
14 |
Correct |
0 ms |
4384 KB |
Output is correct |
15 |
Correct |
0 ms |
4384 KB |
Output is correct |
16 |
Correct |
0 ms |
4384 KB |
Output is correct |
17 |
Correct |
3 ms |
4516 KB |
Output is correct |
18 |
Correct |
3 ms |
4384 KB |
Output is correct |
19 |
Correct |
0 ms |
4384 KB |
Output is correct |
20 |
Correct |
3 ms |
4516 KB |
Output is correct |
21 |
Correct |
0 ms |
4384 KB |
Output is correct |
22 |
Correct |
0 ms |
4384 KB |
Output is correct |
23 |
Correct |
3 ms |
4384 KB |
Output is correct |
24 |
Correct |
0 ms |
4648 KB |
Output is correct |
25 |
Correct |
3 ms |
4520 KB |
Output is correct |
26 |
Correct |
0 ms |
4384 KB |
Output is correct |
27 |
Correct |
0 ms |
4388 KB |
Output is correct |
28 |
Correct |
3 ms |
4648 KB |
Output is correct |
29 |
Correct |
3 ms |
5128 KB |
Output is correct |
30 |
Correct |
3 ms |
4540 KB |
Output is correct |
31 |
Correct |
3 ms |
4788 KB |
Output is correct |
32 |
Correct |
0 ms |
4648 KB |
Output is correct |
33 |
Correct |
3 ms |
5808 KB |
Output is correct |
34 |
Correct |
3 ms |
5808 KB |
Output is correct |
35 |
Correct |
26 ms |
6628 KB |
Output is correct |
36 |
Correct |
3 ms |
4784 KB |
Output is correct |
37 |
Correct |
26 ms |
7288 KB |
Output is correct |
38 |
Correct |
29 ms |
7552 KB |
Output is correct |
39 |
Correct |
19 ms |
7552 KB |
Output is correct |
40 |
Correct |
33 ms |
7552 KB |
Output is correct |
41 |
Correct |
36 ms |
7556 KB |
Output is correct |
42 |
Correct |
3 ms |
4384 KB |
Output is correct |
43 |
Correct |
3 ms |
4388 KB |
Output is correct |
44 |
Correct |
6 ms |
4516 KB |
Output is correct |
45 |
Correct |
26 ms |
10840 KB |
Output is correct |
46 |
Correct |
26 ms |
10840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4252 KB |
Output is correct |
2 |
Correct |
0 ms |
4252 KB |
Output is correct |
3 |
Correct |
0 ms |
4252 KB |
Output is correct |
4 |
Correct |
0 ms |
4252 KB |
Output is correct |
5 |
Correct |
0 ms |
4252 KB |
Output is correct |
6 |
Correct |
0 ms |
4252 KB |
Output is correct |
7 |
Correct |
3 ms |
4252 KB |
Output is correct |
8 |
Correct |
0 ms |
4252 KB |
Output is correct |
9 |
Correct |
0 ms |
4252 KB |
Output is correct |
10 |
Correct |
0 ms |
4252 KB |
Output is correct |
11 |
Correct |
0 ms |
4384 KB |
Output is correct |
12 |
Correct |
0 ms |
4252 KB |
Output is correct |
13 |
Correct |
0 ms |
4252 KB |
Output is correct |
14 |
Correct |
0 ms |
4384 KB |
Output is correct |
15 |
Correct |
0 ms |
4384 KB |
Output is correct |
16 |
Correct |
0 ms |
4384 KB |
Output is correct |
17 |
Correct |
3 ms |
4516 KB |
Output is correct |
18 |
Correct |
0 ms |
4384 KB |
Output is correct |
19 |
Correct |
0 ms |
4384 KB |
Output is correct |
20 |
Correct |
3 ms |
4516 KB |
Output is correct |
21 |
Correct |
0 ms |
4384 KB |
Output is correct |
22 |
Correct |
0 ms |
4384 KB |
Output is correct |
23 |
Correct |
3 ms |
4384 KB |
Output is correct |
24 |
Correct |
3 ms |
4648 KB |
Output is correct |
25 |
Correct |
3 ms |
4520 KB |
Output is correct |
26 |
Correct |
0 ms |
4384 KB |
Output is correct |
27 |
Correct |
0 ms |
4388 KB |
Output is correct |
28 |
Correct |
3 ms |
4648 KB |
Output is correct |
29 |
Correct |
3 ms |
5128 KB |
Output is correct |
30 |
Correct |
3 ms |
4540 KB |
Output is correct |
31 |
Correct |
0 ms |
4788 KB |
Output is correct |
32 |
Correct |
3 ms |
4648 KB |
Output is correct |
33 |
Correct |
3 ms |
5808 KB |
Output is correct |
34 |
Correct |
3 ms |
5808 KB |
Output is correct |
35 |
Correct |
19 ms |
6628 KB |
Output is correct |
36 |
Correct |
0 ms |
4784 KB |
Output is correct |
37 |
Correct |
29 ms |
7288 KB |
Output is correct |
38 |
Correct |
29 ms |
7552 KB |
Output is correct |
39 |
Correct |
36 ms |
7552 KB |
Output is correct |
40 |
Correct |
36 ms |
7552 KB |
Output is correct |
41 |
Correct |
36 ms |
7556 KB |
Output is correct |
42 |
Correct |
0 ms |
4384 KB |
Output is correct |
43 |
Correct |
0 ms |
4388 KB |
Output is correct |
44 |
Correct |
3 ms |
4516 KB |
Output is correct |
45 |
Correct |
33 ms |
10840 KB |
Output is correct |
46 |
Correct |
39 ms |
10840 KB |
Output is correct |
47 |
Correct |
83 ms |
15320 KB |
Output is correct |
48 |
Correct |
26 ms |
8556 KB |
Output is correct |
49 |
Correct |
39 ms |
7464 KB |
Output is correct |
50 |
Correct |
16 ms |
7020 KB |
Output is correct |
51 |
Correct |
153 ms |
11508 KB |
Output is correct |
52 |
Correct |
189 ms |
11752 KB |
Output is correct |
53 |
Correct |
39 ms |
8308 KB |
Output is correct |
54 |
Correct |
3 ms |
5252 KB |
Output is correct |
55 |
Correct |
3 ms |
5764 KB |
Output is correct |
56 |
Correct |
33 ms |
8080 KB |
Output is correct |
57 |
Correct |
123 ms |
13832 KB |
Output is correct |
58 |
Correct |
6 ms |
6012 KB |
Output is correct |
59 |
Correct |
13 ms |
6296 KB |
Output is correct |
60 |
Correct |
16 ms |
7048 KB |
Output is correct |
61 |
Correct |
13 ms |
6324 KB |
Output is correct |
62 |
Correct |
53 ms |
12408 KB |
Output is correct |
63 |
Correct |
116 ms |
31996 KB |
Output is correct |
64 |
Correct |
126 ms |
42120 KB |
Output is correct |
65 |
Correct |
153 ms |
56012 KB |
Output is correct |
66 |
Correct |
246 ms |
88492 KB |
Output is correct |
67 |
Correct |
239 ms |
88492 KB |
Output is correct |