#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX 30005
int numHouse, numDoge;
set<int> G[MAX];
int pos[MAX], power[MAX], minDist[MAX];
void process(void){
int from, to;
cin >> numHouse >> numDoge;
for (int i = 1; i <= numDoge; ++i){
cin >> pos[i] >> power[i];
++pos[i];
G[pos[i]].insert(power[i]);
if(i == 1){
from = pos[i];
}
if(i == 2){
to = pos[i];
}
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
memset(minDist, 0x3f, sizeof minDist);
q.emplace(0, from);
minDist[from] = 0;
while(q.size()){
int du, u; tie(du, u) = q.top(); q.pop();
if(du > minDist[u]) continue;
for (auto&v : G[u]){
for (int i = 1; u + v * i <= numHouse; ++i){
if(minimize(minDist[u + v * i], minDist[u] + i)){
q.emplace(minDist[u + v * i], u + v * i);
}
}
for (int i = -1; u + v * i >= 1; --i){
if(minimize(minDist[u + v * i], minDist[u] - i)){
q.emplace(minDist[u + v * i], u + v * i);
}
}
}
}
cout << (minDist[to] >= LINF ? -1 : minDist[to]);
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
Compilation message
skyscraper.cpp: In function 'void process()':
skyscraper.cpp:62:19: warning: 'from' may be used uninitialized in this function [-Wmaybe-uninitialized]
62 | minDist[from] = 0;
| ~~~~~~~~~~~~~~^~~
skyscraper.cpp:80:24: warning: 'to' may be used uninitialized in this function [-Wmaybe-uninitialized]
80 | cout << (minDist[to] >= LINF ? -1 : minDist[to]);
| ~~~~~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
2040 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
2100 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
2132 KB |
Output is correct |
11 |
Correct |
2 ms |
2132 KB |
Output is correct |
12 |
Correct |
2 ms |
2132 KB |
Output is correct |
13 |
Correct |
2 ms |
2132 KB |
Output is correct |
14 |
Correct |
2 ms |
2132 KB |
Output is correct |
15 |
Correct |
3 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
2044 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
2300 KB |
Output is correct |
9 |
Correct |
2 ms |
1884 KB |
Output is correct |
10 |
Correct |
2 ms |
2132 KB |
Output is correct |
11 |
Correct |
2 ms |
2132 KB |
Output is correct |
12 |
Correct |
2 ms |
2132 KB |
Output is correct |
13 |
Correct |
3 ms |
2132 KB |
Output is correct |
14 |
Correct |
2 ms |
2132 KB |
Output is correct |
15 |
Correct |
2 ms |
2132 KB |
Output is correct |
16 |
Correct |
2 ms |
2132 KB |
Output is correct |
17 |
Correct |
2 ms |
2132 KB |
Output is correct |
18 |
Correct |
2 ms |
2132 KB |
Output is correct |
19 |
Correct |
2 ms |
2132 KB |
Output is correct |
20 |
Correct |
6 ms |
2132 KB |
Output is correct |
21 |
Correct |
2 ms |
2132 KB |
Output is correct |
22 |
Correct |
2 ms |
2132 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
2 ms |
2296 KB |
Output is correct |
25 |
Correct |
2 ms |
2132 KB |
Output is correct |
26 |
Correct |
2 ms |
2132 KB |
Output is correct |
27 |
Correct |
2 ms |
2132 KB |
Output is correct |
28 |
Correct |
2 ms |
2132 KB |
Output is correct |
29 |
Correct |
4 ms |
2552 KB |
Output is correct |
30 |
Correct |
2 ms |
2132 KB |
Output is correct |
31 |
Correct |
2 ms |
2388 KB |
Output is correct |
32 |
Correct |
2 ms |
2132 KB |
Output is correct |
33 |
Correct |
5 ms |
2768 KB |
Output is correct |
34 |
Correct |
5 ms |
2768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
2132 KB |
Output is correct |
11 |
Correct |
2 ms |
2132 KB |
Output is correct |
12 |
Correct |
2 ms |
2132 KB |
Output is correct |
13 |
Correct |
2 ms |
2132 KB |
Output is correct |
14 |
Correct |
2 ms |
2220 KB |
Output is correct |
15 |
Correct |
2 ms |
2132 KB |
Output is correct |
16 |
Correct |
2 ms |
2132 KB |
Output is correct |
17 |
Correct |
2 ms |
2132 KB |
Output is correct |
18 |
Correct |
2 ms |
2132 KB |
Output is correct |
19 |
Correct |
2 ms |
2132 KB |
Output is correct |
20 |
Correct |
5 ms |
2132 KB |
Output is correct |
21 |
Correct |
2 ms |
2132 KB |
Output is correct |
22 |
Correct |
2 ms |
2132 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
3 ms |
2132 KB |
Output is correct |
25 |
Correct |
2 ms |
2132 KB |
Output is correct |
26 |
Correct |
2 ms |
2132 KB |
Output is correct |
27 |
Correct |
2 ms |
2132 KB |
Output is correct |
28 |
Correct |
2 ms |
2132 KB |
Output is correct |
29 |
Correct |
4 ms |
2388 KB |
Output is correct |
30 |
Correct |
2 ms |
2132 KB |
Output is correct |
31 |
Correct |
3 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2140 KB |
Output is correct |
33 |
Correct |
5 ms |
3032 KB |
Output is correct |
34 |
Correct |
5 ms |
2768 KB |
Output is correct |
35 |
Correct |
10 ms |
3924 KB |
Output is correct |
36 |
Correct |
3 ms |
2388 KB |
Output is correct |
37 |
Correct |
7 ms |
3668 KB |
Output is correct |
38 |
Correct |
11 ms |
4436 KB |
Output is correct |
39 |
Correct |
10 ms |
4440 KB |
Output is correct |
40 |
Correct |
10 ms |
4436 KB |
Output is correct |
41 |
Correct |
10 ms |
4436 KB |
Output is correct |
42 |
Correct |
4 ms |
2656 KB |
Output is correct |
43 |
Correct |
5 ms |
2656 KB |
Output is correct |
44 |
Correct |
8 ms |
2912 KB |
Output is correct |
45 |
Correct |
13 ms |
4828 KB |
Output is correct |
46 |
Correct |
13 ms |
5056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1888 KB |
Output is correct |
2 |
Correct |
2 ms |
1888 KB |
Output is correct |
3 |
Correct |
3 ms |
1888 KB |
Output is correct |
4 |
Correct |
2 ms |
1888 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
2132 KB |
Output is correct |
11 |
Correct |
2 ms |
2132 KB |
Output is correct |
12 |
Correct |
2 ms |
2296 KB |
Output is correct |
13 |
Correct |
2 ms |
2132 KB |
Output is correct |
14 |
Correct |
2 ms |
2132 KB |
Output is correct |
15 |
Correct |
2 ms |
2132 KB |
Output is correct |
16 |
Correct |
2 ms |
2132 KB |
Output is correct |
17 |
Correct |
3 ms |
2132 KB |
Output is correct |
18 |
Correct |
2 ms |
2132 KB |
Output is correct |
19 |
Correct |
2 ms |
2132 KB |
Output is correct |
20 |
Correct |
6 ms |
2132 KB |
Output is correct |
21 |
Correct |
2 ms |
2132 KB |
Output is correct |
22 |
Correct |
2 ms |
2132 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
2 ms |
2148 KB |
Output is correct |
25 |
Correct |
2 ms |
2132 KB |
Output is correct |
26 |
Correct |
2 ms |
2184 KB |
Output is correct |
27 |
Correct |
2 ms |
2132 KB |
Output is correct |
28 |
Correct |
2 ms |
2132 KB |
Output is correct |
29 |
Correct |
3 ms |
2388 KB |
Output is correct |
30 |
Correct |
2 ms |
2132 KB |
Output is correct |
31 |
Correct |
2 ms |
2388 KB |
Output is correct |
32 |
Correct |
2 ms |
2132 KB |
Output is correct |
33 |
Correct |
5 ms |
2768 KB |
Output is correct |
34 |
Correct |
4 ms |
2828 KB |
Output is correct |
35 |
Correct |
9 ms |
4084 KB |
Output is correct |
36 |
Correct |
4 ms |
2388 KB |
Output is correct |
37 |
Correct |
7 ms |
3696 KB |
Output is correct |
38 |
Correct |
10 ms |
4436 KB |
Output is correct |
39 |
Correct |
10 ms |
4436 KB |
Output is correct |
40 |
Correct |
10 ms |
4436 KB |
Output is correct |
41 |
Correct |
10 ms |
4436 KB |
Output is correct |
42 |
Correct |
5 ms |
2644 KB |
Output is correct |
43 |
Correct |
5 ms |
2820 KB |
Output is correct |
44 |
Correct |
7 ms |
2920 KB |
Output is correct |
45 |
Correct |
13 ms |
4816 KB |
Output is correct |
46 |
Correct |
13 ms |
4980 KB |
Output is correct |
47 |
Correct |
16 ms |
4812 KB |
Output is correct |
48 |
Correct |
6 ms |
3668 KB |
Output is correct |
49 |
Correct |
6 ms |
3452 KB |
Output is correct |
50 |
Correct |
4 ms |
3156 KB |
Output is correct |
51 |
Correct |
22 ms |
7528 KB |
Output is correct |
52 |
Correct |
21 ms |
5388 KB |
Output is correct |
53 |
Correct |
18 ms |
5492 KB |
Output is correct |
54 |
Correct |
3 ms |
2636 KB |
Output is correct |
55 |
Correct |
3 ms |
2388 KB |
Output is correct |
56 |
Correct |
708 ms |
4828 KB |
Output is correct |
57 |
Correct |
35 ms |
8404 KB |
Output is correct |
58 |
Correct |
6 ms |
3040 KB |
Output is correct |
59 |
Correct |
6 ms |
3080 KB |
Output is correct |
60 |
Correct |
7 ms |
3324 KB |
Output is correct |
61 |
Correct |
8 ms |
3452 KB |
Output is correct |
62 |
Correct |
8 ms |
4180 KB |
Output is correct |
63 |
Correct |
31 ms |
10600 KB |
Output is correct |
64 |
Correct |
32 ms |
10600 KB |
Output is correct |
65 |
Correct |
46 ms |
10436 KB |
Output is correct |
66 |
Correct |
73 ms |
14388 KB |
Output is correct |
67 |
Correct |
84 ms |
14528 KB |
Output is correct |