#include <bits/stdc++.h>
using namespace std;
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int B[30000],P[30000];
vi doge[30000];
int dist[30000];
priority_queue<pii> H;
bool comp(int a,int b) {
if ((a < 2) || (b < 2)) return a < b;
else return P[a] < P[b];
}
bool comp2(int a,int b) {
if ((a < 2) || (b < 2)) return a == b;
else return P[a] == P[b];
}
int main() {
int i;
int N,M;
scanf("%d %d",&N,&M);
for (i = 0; i < M; i++) {
scanf("%d %d",&B[i],&P[i]);
doge[B[i]].push_back(i);
}
P[1] = -1;
for (i = 0; i < N; i++) {
sort(doge[i].begin(),doge[i].end(),comp);
doge[i].resize(unique(doge[i].begin(),doge[i].end(),comp2)-doge[i].begin());
}
int j,u,d,w;
fill(dist,dist+M,-1);
for (i = 0; i < doge[B[0]].size(); i++) {
u = doge[B[0]][i];
dist[u] = 0;
H.push(make_pair(0,u));
}
while (!H.empty()) {
u = H.top().second;
d = -H.top().first;
H.pop();
if (d > dist[u]) continue;
else if (u == 1) break;
for (i = B[u]+P[u],w = 1; i < N; i += P[u],w++) {
for (j = 0; j < doge[i].size(); j++) {
int v = doge[i][j];
if ((P[u] != P[v]) && ((dist[v] == -1) || ((dist[u]+w) < dist[v]))) {
dist[v] = dist[u]+w;
H.push(make_pair(-dist[v],v));
}
}
}
for (i = B[u]-P[u],w = 1; i >= 0; i -= P[u],w++) {
for (j = 0; j < doge[i].size(); j++) {
int v = doge[i][j];
if ((P[u] != P[v]) && ((dist[v] == -1) || ((dist[u]+w) < dist[v]))) {
dist[v] = dist[u]+w;
H.push(make_pair(-dist[v],v));
}
}
}
}
printf("%d\n",dist[1]);
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:73:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < doge[B[0]].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
skyscraper.cpp:86:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < doge[i].size(); j++) {
~~^~~~~~~~~~~~~~~~
skyscraper.cpp:95:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < doge[i].size(); j++) {
~~^~~~~~~~~~~~~~~~
skyscraper.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&M);
~~~~~^~~~~~~~~~~~~~~
skyscraper.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&B[i],&P[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1016 KB |
Output is correct |
2 |
Correct |
2 ms |
1016 KB |
Output is correct |
3 |
Correct |
2 ms |
1184 KB |
Output is correct |
4 |
Correct |
2 ms |
1184 KB |
Output is correct |
5 |
Correct |
2 ms |
1196 KB |
Output is correct |
6 |
Correct |
2 ms |
1196 KB |
Output is correct |
7 |
Correct |
3 ms |
1308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1308 KB |
Output is correct |
2 |
Correct |
2 ms |
1464 KB |
Output is correct |
3 |
Correct |
3 ms |
1464 KB |
Output is correct |
4 |
Correct |
3 ms |
1464 KB |
Output is correct |
5 |
Correct |
3 ms |
1464 KB |
Output is correct |
6 |
Correct |
3 ms |
1464 KB |
Output is correct |
7 |
Correct |
3 ms |
1536 KB |
Output is correct |
8 |
Correct |
3 ms |
1536 KB |
Output is correct |
9 |
Correct |
3 ms |
1536 KB |
Output is correct |
10 |
Correct |
3 ms |
1536 KB |
Output is correct |
11 |
Correct |
3 ms |
1568 KB |
Output is correct |
12 |
Correct |
3 ms |
1568 KB |
Output is correct |
13 |
Correct |
3 ms |
1568 KB |
Output is correct |
14 |
Correct |
4 ms |
1644 KB |
Output is correct |
15 |
Correct |
3 ms |
1672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1672 KB |
Output is correct |
2 |
Correct |
2 ms |
1672 KB |
Output is correct |
3 |
Correct |
2 ms |
1672 KB |
Output is correct |
4 |
Correct |
2 ms |
1672 KB |
Output is correct |
5 |
Correct |
3 ms |
1672 KB |
Output is correct |
6 |
Correct |
4 ms |
1672 KB |
Output is correct |
7 |
Correct |
3 ms |
1672 KB |
Output is correct |
8 |
Correct |
4 ms |
1672 KB |
Output is correct |
9 |
Correct |
3 ms |
1672 KB |
Output is correct |
10 |
Correct |
3 ms |
1672 KB |
Output is correct |
11 |
Correct |
4 ms |
1704 KB |
Output is correct |
12 |
Correct |
4 ms |
1704 KB |
Output is correct |
13 |
Correct |
4 ms |
1728 KB |
Output is correct |
14 |
Correct |
4 ms |
1756 KB |
Output is correct |
15 |
Correct |
4 ms |
1768 KB |
Output is correct |
16 |
Correct |
3 ms |
1768 KB |
Output is correct |
17 |
Correct |
4 ms |
1804 KB |
Output is correct |
18 |
Correct |
3 ms |
1804 KB |
Output is correct |
19 |
Correct |
3 ms |
1804 KB |
Output is correct |
20 |
Correct |
3 ms |
1840 KB |
Output is correct |
21 |
Correct |
3 ms |
1840 KB |
Output is correct |
22 |
Correct |
3 ms |
1840 KB |
Output is correct |
23 |
Correct |
4 ms |
1840 KB |
Output is correct |
24 |
Correct |
4 ms |
1888 KB |
Output is correct |
25 |
Correct |
4 ms |
1908 KB |
Output is correct |
26 |
Correct |
4 ms |
1908 KB |
Output is correct |
27 |
Correct |
3 ms |
1908 KB |
Output is correct |
28 |
Correct |
4 ms |
1960 KB |
Output is correct |
29 |
Correct |
3 ms |
1960 KB |
Output is correct |
30 |
Correct |
2 ms |
1960 KB |
Output is correct |
31 |
Correct |
3 ms |
1960 KB |
Output is correct |
32 |
Correct |
3 ms |
1960 KB |
Output is correct |
33 |
Correct |
4 ms |
2036 KB |
Output is correct |
34 |
Correct |
4 ms |
2036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2036 KB |
Output is correct |
2 |
Correct |
3 ms |
2036 KB |
Output is correct |
3 |
Correct |
2 ms |
2036 KB |
Output is correct |
4 |
Correct |
2 ms |
2036 KB |
Output is correct |
5 |
Correct |
2 ms |
2036 KB |
Output is correct |
6 |
Correct |
3 ms |
2036 KB |
Output is correct |
7 |
Correct |
2 ms |
2036 KB |
Output is correct |
8 |
Correct |
3 ms |
2036 KB |
Output is correct |
9 |
Correct |
2 ms |
2036 KB |
Output is correct |
10 |
Correct |
3 ms |
2036 KB |
Output is correct |
11 |
Correct |
4 ms |
2068 KB |
Output is correct |
12 |
Correct |
3 ms |
2068 KB |
Output is correct |
13 |
Correct |
3 ms |
2068 KB |
Output is correct |
14 |
Correct |
5 ms |
2104 KB |
Output is correct |
15 |
Correct |
5 ms |
2104 KB |
Output is correct |
16 |
Correct |
3 ms |
2104 KB |
Output is correct |
17 |
Correct |
5 ms |
2136 KB |
Output is correct |
18 |
Correct |
4 ms |
2136 KB |
Output is correct |
19 |
Correct |
3 ms |
2136 KB |
Output is correct |
20 |
Correct |
4 ms |
2136 KB |
Output is correct |
21 |
Correct |
3 ms |
2136 KB |
Output is correct |
22 |
Correct |
3 ms |
2136 KB |
Output is correct |
23 |
Correct |
3 ms |
2240 KB |
Output is correct |
24 |
Correct |
4 ms |
2300 KB |
Output is correct |
25 |
Correct |
3 ms |
2320 KB |
Output is correct |
26 |
Correct |
3 ms |
2320 KB |
Output is correct |
27 |
Correct |
4 ms |
2320 KB |
Output is correct |
28 |
Correct |
5 ms |
2336 KB |
Output is correct |
29 |
Correct |
3 ms |
2336 KB |
Output is correct |
30 |
Correct |
3 ms |
2452 KB |
Output is correct |
31 |
Correct |
3 ms |
2452 KB |
Output is correct |
32 |
Correct |
3 ms |
2452 KB |
Output is correct |
33 |
Correct |
5 ms |
2452 KB |
Output is correct |
34 |
Correct |
4 ms |
2452 KB |
Output is correct |
35 |
Correct |
34 ms |
3672 KB |
Output is correct |
36 |
Correct |
5 ms |
3672 KB |
Output is correct |
37 |
Correct |
37 ms |
3776 KB |
Output is correct |
38 |
Correct |
48 ms |
4728 KB |
Output is correct |
39 |
Correct |
14 ms |
4728 KB |
Output is correct |
40 |
Correct |
24 ms |
5256 KB |
Output is correct |
41 |
Correct |
45 ms |
5552 KB |
Output is correct |
42 |
Correct |
15 ms |
5552 KB |
Output is correct |
43 |
Correct |
11 ms |
5552 KB |
Output is correct |
44 |
Correct |
10 ms |
5552 KB |
Output is correct |
45 |
Correct |
37 ms |
5716 KB |
Output is correct |
46 |
Correct |
35 ms |
6008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6008 KB |
Output is correct |
2 |
Correct |
3 ms |
6008 KB |
Output is correct |
3 |
Correct |
2 ms |
6008 KB |
Output is correct |
4 |
Correct |
3 ms |
6008 KB |
Output is correct |
5 |
Correct |
2 ms |
6008 KB |
Output is correct |
6 |
Correct |
2 ms |
6008 KB |
Output is correct |
7 |
Correct |
3 ms |
6008 KB |
Output is correct |
8 |
Correct |
3 ms |
6008 KB |
Output is correct |
9 |
Correct |
3 ms |
6008 KB |
Output is correct |
10 |
Correct |
2 ms |
6008 KB |
Output is correct |
11 |
Correct |
5 ms |
6008 KB |
Output is correct |
12 |
Correct |
3 ms |
6008 KB |
Output is correct |
13 |
Correct |
3 ms |
6008 KB |
Output is correct |
14 |
Correct |
5 ms |
6008 KB |
Output is correct |
15 |
Correct |
4 ms |
6008 KB |
Output is correct |
16 |
Correct |
3 ms |
6008 KB |
Output is correct |
17 |
Correct |
4 ms |
6008 KB |
Output is correct |
18 |
Correct |
4 ms |
6008 KB |
Output is correct |
19 |
Correct |
3 ms |
6008 KB |
Output is correct |
20 |
Correct |
4 ms |
6008 KB |
Output is correct |
21 |
Correct |
3 ms |
6008 KB |
Output is correct |
22 |
Correct |
3 ms |
6008 KB |
Output is correct |
23 |
Correct |
4 ms |
6008 KB |
Output is correct |
24 |
Correct |
4 ms |
6008 KB |
Output is correct |
25 |
Correct |
4 ms |
6008 KB |
Output is correct |
26 |
Correct |
3 ms |
6008 KB |
Output is correct |
27 |
Correct |
4 ms |
6008 KB |
Output is correct |
28 |
Correct |
3 ms |
6008 KB |
Output is correct |
29 |
Correct |
3 ms |
6008 KB |
Output is correct |
30 |
Correct |
3 ms |
6008 KB |
Output is correct |
31 |
Correct |
3 ms |
6008 KB |
Output is correct |
32 |
Correct |
3 ms |
6008 KB |
Output is correct |
33 |
Correct |
4 ms |
6008 KB |
Output is correct |
34 |
Correct |
4 ms |
6008 KB |
Output is correct |
35 |
Correct |
37 ms |
6740 KB |
Output is correct |
36 |
Correct |
5 ms |
6740 KB |
Output is correct |
37 |
Correct |
28 ms |
6744 KB |
Output is correct |
38 |
Correct |
46 ms |
7672 KB |
Output is correct |
39 |
Correct |
18 ms |
7672 KB |
Output is correct |
40 |
Correct |
20 ms |
8164 KB |
Output is correct |
41 |
Correct |
46 ms |
8396 KB |
Output is correct |
42 |
Correct |
10 ms |
8396 KB |
Output is correct |
43 |
Correct |
11 ms |
8396 KB |
Output is correct |
44 |
Correct |
9 ms |
8396 KB |
Output is correct |
45 |
Correct |
36 ms |
8744 KB |
Output is correct |
46 |
Correct |
34 ms |
8924 KB |
Output is correct |
47 |
Correct |
30 ms |
9776 KB |
Output is correct |
48 |
Correct |
12 ms |
9776 KB |
Output is correct |
49 |
Correct |
12 ms |
9776 KB |
Output is correct |
50 |
Correct |
9 ms |
9776 KB |
Output is correct |
51 |
Correct |
30 ms |
11032 KB |
Output is correct |
52 |
Correct |
32 ms |
11032 KB |
Output is correct |
53 |
Correct |
17 ms |
11032 KB |
Output is correct |
54 |
Correct |
3 ms |
11032 KB |
Output is correct |
55 |
Correct |
2 ms |
11032 KB |
Output is correct |
56 |
Correct |
13 ms |
11156 KB |
Output is correct |
57 |
Correct |
4 ms |
11156 KB |
Output is correct |
58 |
Correct |
9 ms |
11156 KB |
Output is correct |
59 |
Correct |
11 ms |
11156 KB |
Output is correct |
60 |
Correct |
10 ms |
11156 KB |
Output is correct |
61 |
Correct |
10 ms |
11344 KB |
Output is correct |
62 |
Correct |
19 ms |
12360 KB |
Output is correct |
63 |
Correct |
28 ms |
12360 KB |
Output is correct |
64 |
Correct |
33 ms |
12404 KB |
Output is correct |
65 |
Correct |
35 ms |
12616 KB |
Output is correct |
66 |
Correct |
66 ms |
13108 KB |
Output is correct |
67 |
Correct |
58 ms |
13320 KB |
Output is correct |