/*
dijstra's on the location
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define For(i, a, b) for(int i=a; i<b; i++)
#define ffi For(i, 0, N)
#define ffj For(j, 0, M)
#define ffa ffi ffj
#define w cout
#define e "\n"
#define s <<" "<<
#define pb push_back
#define mp make_pair
#define a first
#define b second
/// 500,000,000
const int MAXN = 30000, INF = 1000000000000000000;
/// Global Variables
int N, M, dist[MAXN], B[MAXN], P[MAXN];
bool vis[MAXN];
vector<int> dogs[MAXN];
priority_queue<pair<int, int> > next1; /// (-dist, loc)
void go(int loc) {
vis[loc] = true;
For (i, 0, dogs[loc].size()) {
int x = dogs[loc][i];
for (int j=loc+P[x], add = 1; j<N; j+= P[x], add++) if (dist[loc]+add < dist[j]) {
dist[j] = min(dist[j], dist[loc]+add);
next1.push(mp(-dist[j], j));
}
for (int j=loc-P[x], add = 1; j>= 0; j-= P[x], add++) if (dist[loc]+add < dist[j]) {
dist[j] = min(dist[j], dist[loc]+add);
next1.push(mp(-dist[j], j));
}
}
}
main() {
//ifstream cin("test.in");
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> M;
ffi dist[i] = INF;
ffj {
cin >> B[j] >> P[j];
dogs[B[j]].pb(j);
}
next1.push(mp(0, B[0]));
dist[B[0]] = 0;
while (!next1.empty()) {
int i = next1.top().b; next1.pop();
if (!vis[i]) go(i);
}
if (dist[B[1]] == INF) w<< -1<<e;
else w<< dist[B[1]]<<e;
}
Compilation message
skyscraper.cpp: In function 'void go(ll)':
skyscraper.cpp:8:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define For(i, a, b) for(int i=a; i<b; i++)
skyscraper.cpp:29:10:
For (i, 0, dogs[loc].size()) {
~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:29:5: note: in expansion of macro 'For'
For (i, 0, dogs[loc].size()) {
^~~
skyscraper.cpp: At global scope:
skyscraper.cpp:42:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1016 KB |
Output is correct |
2 |
Correct |
3 ms |
1124 KB |
Output is correct |
3 |
Correct |
2 ms |
1124 KB |
Output is correct |
4 |
Correct |
4 ms |
1124 KB |
Output is correct |
5 |
Correct |
2 ms |
1216 KB |
Output is correct |
6 |
Correct |
2 ms |
1216 KB |
Output is correct |
7 |
Correct |
2 ms |
1216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1312 KB |
Output is correct |
2 |
Correct |
2 ms |
1312 KB |
Output is correct |
3 |
Correct |
3 ms |
1312 KB |
Output is correct |
4 |
Correct |
3 ms |
1312 KB |
Output is correct |
5 |
Correct |
3 ms |
1312 KB |
Output is correct |
6 |
Correct |
3 ms |
1312 KB |
Output is correct |
7 |
Correct |
3 ms |
1312 KB |
Output is correct |
8 |
Correct |
3 ms |
1312 KB |
Output is correct |
9 |
Correct |
3 ms |
1312 KB |
Output is correct |
10 |
Correct |
3 ms |
1312 KB |
Output is correct |
11 |
Correct |
5 ms |
1388 KB |
Output is correct |
12 |
Correct |
3 ms |
1388 KB |
Output is correct |
13 |
Correct |
3 ms |
1388 KB |
Output is correct |
14 |
Correct |
3 ms |
1388 KB |
Output is correct |
15 |
Correct |
3 ms |
1476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1476 KB |
Output is correct |
2 |
Correct |
3 ms |
1476 KB |
Output is correct |
3 |
Correct |
3 ms |
1476 KB |
Output is correct |
4 |
Correct |
2 ms |
1476 KB |
Output is correct |
5 |
Correct |
3 ms |
1476 KB |
Output is correct |
6 |
Correct |
3 ms |
1476 KB |
Output is correct |
7 |
Correct |
2 ms |
1476 KB |
Output is correct |
8 |
Correct |
3 ms |
1476 KB |
Output is correct |
9 |
Correct |
2 ms |
1476 KB |
Output is correct |
10 |
Correct |
3 ms |
1476 KB |
Output is correct |
11 |
Correct |
3 ms |
1476 KB |
Output is correct |
12 |
Correct |
3 ms |
1476 KB |
Output is correct |
13 |
Correct |
4 ms |
1476 KB |
Output is correct |
14 |
Correct |
3 ms |
1476 KB |
Output is correct |
15 |
Correct |
4 ms |
1476 KB |
Output is correct |
16 |
Correct |
3 ms |
1476 KB |
Output is correct |
17 |
Correct |
3 ms |
1476 KB |
Output is correct |
18 |
Correct |
3 ms |
1476 KB |
Output is correct |
19 |
Correct |
3 ms |
1476 KB |
Output is correct |
20 |
Correct |
9 ms |
1532 KB |
Output is correct |
21 |
Correct |
4 ms |
1532 KB |
Output is correct |
22 |
Correct |
3 ms |
1532 KB |
Output is correct |
23 |
Correct |
4 ms |
1532 KB |
Output is correct |
24 |
Correct |
4 ms |
1532 KB |
Output is correct |
25 |
Correct |
4 ms |
1532 KB |
Output is correct |
26 |
Correct |
8 ms |
1532 KB |
Output is correct |
27 |
Correct |
6 ms |
1532 KB |
Output is correct |
28 |
Correct |
4 ms |
1532 KB |
Output is correct |
29 |
Correct |
4 ms |
1532 KB |
Output is correct |
30 |
Correct |
3 ms |
1532 KB |
Output is correct |
31 |
Correct |
4 ms |
1532 KB |
Output is correct |
32 |
Correct |
4 ms |
1532 KB |
Output is correct |
33 |
Correct |
5 ms |
1660 KB |
Output is correct |
34 |
Correct |
6 ms |
1788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1788 KB |
Output is correct |
2 |
Correct |
3 ms |
1788 KB |
Output is correct |
3 |
Correct |
3 ms |
1788 KB |
Output is correct |
4 |
Correct |
3 ms |
1788 KB |
Output is correct |
5 |
Correct |
3 ms |
1788 KB |
Output is correct |
6 |
Correct |
2 ms |
1788 KB |
Output is correct |
7 |
Correct |
2 ms |
1788 KB |
Output is correct |
8 |
Correct |
3 ms |
1788 KB |
Output is correct |
9 |
Correct |
2 ms |
1788 KB |
Output is correct |
10 |
Correct |
3 ms |
1788 KB |
Output is correct |
11 |
Correct |
3 ms |
1788 KB |
Output is correct |
12 |
Correct |
4 ms |
1788 KB |
Output is correct |
13 |
Correct |
4 ms |
1788 KB |
Output is correct |
14 |
Correct |
3 ms |
1788 KB |
Output is correct |
15 |
Correct |
4 ms |
1788 KB |
Output is correct |
16 |
Correct |
4 ms |
1788 KB |
Output is correct |
17 |
Correct |
4 ms |
1788 KB |
Output is correct |
18 |
Correct |
4 ms |
1788 KB |
Output is correct |
19 |
Correct |
3 ms |
1788 KB |
Output is correct |
20 |
Correct |
8 ms |
1788 KB |
Output is correct |
21 |
Correct |
4 ms |
1788 KB |
Output is correct |
22 |
Correct |
3 ms |
1788 KB |
Output is correct |
23 |
Correct |
4 ms |
1788 KB |
Output is correct |
24 |
Correct |
5 ms |
1788 KB |
Output is correct |
25 |
Correct |
5 ms |
1788 KB |
Output is correct |
26 |
Correct |
7 ms |
1788 KB |
Output is correct |
27 |
Correct |
7 ms |
1788 KB |
Output is correct |
28 |
Correct |
4 ms |
1788 KB |
Output is correct |
29 |
Correct |
5 ms |
1788 KB |
Output is correct |
30 |
Correct |
4 ms |
1788 KB |
Output is correct |
31 |
Correct |
4 ms |
1788 KB |
Output is correct |
32 |
Correct |
4 ms |
1788 KB |
Output is correct |
33 |
Correct |
6 ms |
1788 KB |
Output is correct |
34 |
Correct |
6 ms |
1788 KB |
Output is correct |
35 |
Correct |
11 ms |
2240 KB |
Output is correct |
36 |
Correct |
5 ms |
2240 KB |
Output is correct |
37 |
Correct |
12 ms |
2240 KB |
Output is correct |
38 |
Correct |
16 ms |
2384 KB |
Output is correct |
39 |
Correct |
13 ms |
2428 KB |
Output is correct |
40 |
Correct |
14 ms |
2428 KB |
Output is correct |
41 |
Correct |
13 ms |
2428 KB |
Output is correct |
42 |
Correct |
63 ms |
2428 KB |
Output is correct |
43 |
Correct |
69 ms |
2428 KB |
Output is correct |
44 |
Correct |
64 ms |
2428 KB |
Output is correct |
45 |
Correct |
14 ms |
2428 KB |
Output is correct |
46 |
Correct |
10 ms |
2428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2428 KB |
Output is correct |
2 |
Correct |
3 ms |
2428 KB |
Output is correct |
3 |
Correct |
2 ms |
2428 KB |
Output is correct |
4 |
Correct |
3 ms |
2428 KB |
Output is correct |
5 |
Correct |
3 ms |
2428 KB |
Output is correct |
6 |
Correct |
3 ms |
2428 KB |
Output is correct |
7 |
Correct |
3 ms |
2428 KB |
Output is correct |
8 |
Correct |
17 ms |
2428 KB |
Output is correct |
9 |
Correct |
2 ms |
2428 KB |
Output is correct |
10 |
Correct |
4 ms |
2428 KB |
Output is correct |
11 |
Correct |
3 ms |
2428 KB |
Output is correct |
12 |
Correct |
3 ms |
2428 KB |
Output is correct |
13 |
Correct |
4 ms |
2428 KB |
Output is correct |
14 |
Correct |
3 ms |
2428 KB |
Output is correct |
15 |
Correct |
3 ms |
2428 KB |
Output is correct |
16 |
Correct |
3 ms |
2428 KB |
Output is correct |
17 |
Correct |
4 ms |
2428 KB |
Output is correct |
18 |
Correct |
3 ms |
2428 KB |
Output is correct |
19 |
Correct |
4 ms |
2428 KB |
Output is correct |
20 |
Correct |
8 ms |
2428 KB |
Output is correct |
21 |
Correct |
3 ms |
2428 KB |
Output is correct |
22 |
Correct |
3 ms |
2428 KB |
Output is correct |
23 |
Correct |
4 ms |
2428 KB |
Output is correct |
24 |
Correct |
4 ms |
2428 KB |
Output is correct |
25 |
Correct |
5 ms |
2428 KB |
Output is correct |
26 |
Correct |
7 ms |
2428 KB |
Output is correct |
27 |
Correct |
7 ms |
2428 KB |
Output is correct |
28 |
Correct |
4 ms |
2428 KB |
Output is correct |
29 |
Correct |
4 ms |
2428 KB |
Output is correct |
30 |
Correct |
4 ms |
2428 KB |
Output is correct |
31 |
Correct |
5 ms |
2428 KB |
Output is correct |
32 |
Correct |
4 ms |
2428 KB |
Output is correct |
33 |
Correct |
4 ms |
2428 KB |
Output is correct |
34 |
Correct |
5 ms |
2428 KB |
Output is correct |
35 |
Correct |
10 ms |
2428 KB |
Output is correct |
36 |
Correct |
5 ms |
2428 KB |
Output is correct |
37 |
Correct |
12 ms |
2428 KB |
Output is correct |
38 |
Correct |
13 ms |
2428 KB |
Output is correct |
39 |
Correct |
12 ms |
2428 KB |
Output is correct |
40 |
Correct |
14 ms |
2428 KB |
Output is correct |
41 |
Correct |
14 ms |
2428 KB |
Output is correct |
42 |
Correct |
68 ms |
2428 KB |
Output is correct |
43 |
Correct |
68 ms |
2428 KB |
Output is correct |
44 |
Correct |
102 ms |
2428 KB |
Output is correct |
45 |
Correct |
11 ms |
2428 KB |
Output is correct |
46 |
Correct |
11 ms |
2428 KB |
Output is correct |
47 |
Correct |
24 ms |
3416 KB |
Output is correct |
48 |
Correct |
9 ms |
3416 KB |
Output is correct |
49 |
Correct |
10 ms |
3416 KB |
Output is correct |
50 |
Correct |
8 ms |
3416 KB |
Output is correct |
51 |
Correct |
37 ms |
4876 KB |
Output is correct |
52 |
Correct |
41 ms |
4876 KB |
Output is correct |
53 |
Correct |
22 ms |
4876 KB |
Output is correct |
54 |
Correct |
5 ms |
4876 KB |
Output is correct |
55 |
Correct |
5 ms |
4876 KB |
Output is correct |
56 |
Correct |
899 ms |
4876 KB |
Output is correct |
57 |
Correct |
82 ms |
5868 KB |
Output is correct |
58 |
Correct |
916 ms |
5868 KB |
Output is correct |
59 |
Correct |
904 ms |
5868 KB |
Output is correct |
60 |
Correct |
848 ms |
5868 KB |
Output is correct |
61 |
Correct |
851 ms |
5868 KB |
Output is correct |
62 |
Correct |
19 ms |
5868 KB |
Output is correct |
63 |
Correct |
47 ms |
5868 KB |
Output is correct |
64 |
Correct |
39 ms |
5868 KB |
Output is correct |
65 |
Correct |
64 ms |
5868 KB |
Output is correct |
66 |
Correct |
56 ms |
6544 KB |
Output is correct |
67 |
Correct |
56 ms |
6632 KB |
Output is correct |