#include <bits/stdc++.h>
#define MT make_tuple
using namespace std;
typedef tuple<int,int,int> trinca;
typedef pair<trinca,int> Ttrinca;
const int MAXN = 30010;
const int BUCKET = 175;
vector<trinca> grafo[MAXN];
queue<trinca> filas[BUCKET];
priority_queue<Ttrinca, vector<Ttrinca> , greater<Ttrinca> > Dijkstra;
int processado[MAXN][BUCKET],N,M,origem,destino;
trinca top(){
return Dijkstra.top().first;
}
void pop(){
int peso = Dijkstra.top().second;
Dijkstra.pop();
filas[peso].pop();
if(!filas[peso].empty()) Dijkstra.push(make_pair(filas[peso].front(),peso));
}
void push(int peso,trinca davez){
if(filas[peso].empty()) Dijkstra.push(make_pair(davez,peso));
filas[peso].push(davez);
}
bool empty(){
return Dijkstra.empty();
}
int main(){
scanf("%d %d",&N,&M);
for(int i = 0;i<M;i++){
int B,P;
scanf("%d %d",&B,&P);
if(i == 0) origem = B;
else if(i == 1) destino = B;
if(P < BUCKET){
grafo[B].push_back(MT(B,P,0));
}
else{
for(int j = 1;B - j*P >= 0;j++){
grafo[B].push_back(MT(B - j*P,0,j));
}
for(int j = 1;B + j*P < N;j++){
grafo[B].push_back(MT(B + j*P,0,j));
}
}
}
priority_queue<trinca, vector<trinca>, greater<trinca> > Dijkstra;
push(0,MT(0,origem,0));
while(!empty()){
trinca davez = top();
pop();
int dist = get<0>(davez), v = get<1>(davez), power = get<2>(davez);
if(processado[v][power] == 1) continue;
processado[v][power] = 1;
if(!processado[v][0]){
push(0,MT(dist,v,0));
}
if(power < BUCKET && v - power >= 0){
if(!processado[v - power][power]) push(1,MT(dist+1,v - power,power));
}
if(power < BUCKET && v + power < N){
if(!processado[v + power][power]) push(1,MT(dist+1,v + power,power));
}
if(v == destino){
printf("%d\n",dist);
return 0;
}
if(power != 0) continue;
for(trinca nova : grafo[v]){
int nv = get<0>(nova), npower = get<1>(nova), nadd = get<2>(nova);
if(v == nv){
if(processado[v][npower]) continue;
processado[v][npower] = 1;
if(npower < BUCKET && v - npower >= 0){
if(!processado[v - npower][npower]) push(1,MT(dist+1,v - npower,npower));
}
if(npower < BUCKET && v + npower < N){
if(!processado[v + npower][npower]) push(1,MT(dist+1,v + npower,npower));
}
continue;
}
if(!processado[nv][npower]) push(nadd,MT(dist+nadd,nv,npower));
}
}
printf("-1\n");
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:29:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&M);
^
skyscraper.cpp:32:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&B,&P);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1144 KB |
Output is correct |
2 |
Correct |
2 ms |
1144 KB |
Output is correct |
3 |
Correct |
2 ms |
1196 KB |
Output is correct |
4 |
Correct |
2 ms |
1268 KB |
Output is correct |
5 |
Correct |
3 ms |
1392 KB |
Output is correct |
6 |
Correct |
2 ms |
1392 KB |
Output is correct |
7 |
Correct |
2 ms |
1392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1392 KB |
Output is correct |
2 |
Correct |
2 ms |
1392 KB |
Output is correct |
3 |
Correct |
2 ms |
1392 KB |
Output is correct |
4 |
Correct |
2 ms |
1392 KB |
Output is correct |
5 |
Correct |
2 ms |
1392 KB |
Output is correct |
6 |
Correct |
2 ms |
1392 KB |
Output is correct |
7 |
Correct |
2 ms |
1392 KB |
Output is correct |
8 |
Correct |
2 ms |
1392 KB |
Output is correct |
9 |
Correct |
2 ms |
1392 KB |
Output is correct |
10 |
Correct |
2 ms |
1392 KB |
Output is correct |
11 |
Correct |
4 ms |
1392 KB |
Output is correct |
12 |
Correct |
2 ms |
1516 KB |
Output is correct |
13 |
Correct |
3 ms |
1516 KB |
Output is correct |
14 |
Correct |
3 ms |
1516 KB |
Output is correct |
15 |
Correct |
3 ms |
1516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1516 KB |
Output is correct |
2 |
Correct |
2 ms |
1516 KB |
Output is correct |
3 |
Correct |
2 ms |
1516 KB |
Output is correct |
4 |
Correct |
2 ms |
1516 KB |
Output is correct |
5 |
Correct |
2 ms |
1516 KB |
Output is correct |
6 |
Correct |
3 ms |
1516 KB |
Output is correct |
7 |
Correct |
2 ms |
1516 KB |
Output is correct |
8 |
Correct |
2 ms |
1516 KB |
Output is correct |
9 |
Correct |
2 ms |
1516 KB |
Output is correct |
10 |
Correct |
2 ms |
1516 KB |
Output is correct |
11 |
Correct |
3 ms |
1516 KB |
Output is correct |
12 |
Correct |
3 ms |
1516 KB |
Output is correct |
13 |
Correct |
3 ms |
1516 KB |
Output is correct |
14 |
Correct |
3 ms |
1516 KB |
Output is correct |
15 |
Correct |
3 ms |
1516 KB |
Output is correct |
16 |
Correct |
3 ms |
1516 KB |
Output is correct |
17 |
Correct |
4 ms |
1900 KB |
Output is correct |
18 |
Correct |
3 ms |
1900 KB |
Output is correct |
19 |
Correct |
3 ms |
1900 KB |
Output is correct |
20 |
Correct |
5 ms |
2796 KB |
Output is correct |
21 |
Correct |
3 ms |
2796 KB |
Output is correct |
22 |
Correct |
2 ms |
2796 KB |
Output is correct |
23 |
Correct |
4 ms |
2796 KB |
Output is correct |
24 |
Correct |
6 ms |
2796 KB |
Output is correct |
25 |
Correct |
4 ms |
2796 KB |
Output is correct |
26 |
Correct |
4 ms |
2796 KB |
Output is correct |
27 |
Correct |
4 ms |
2796 KB |
Output is correct |
28 |
Correct |
5 ms |
2796 KB |
Output is correct |
29 |
Correct |
7 ms |
2796 KB |
Output is correct |
30 |
Correct |
4 ms |
2796 KB |
Output is correct |
31 |
Correct |
5 ms |
2892 KB |
Output is correct |
32 |
Correct |
5 ms |
2892 KB |
Output is correct |
33 |
Correct |
10 ms |
2892 KB |
Output is correct |
34 |
Correct |
8 ms |
2892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2892 KB |
Output is correct |
2 |
Correct |
2 ms |
2892 KB |
Output is correct |
3 |
Correct |
2 ms |
2892 KB |
Output is correct |
4 |
Correct |
2 ms |
2892 KB |
Output is correct |
5 |
Correct |
2 ms |
2892 KB |
Output is correct |
6 |
Correct |
2 ms |
2892 KB |
Output is correct |
7 |
Correct |
2 ms |
2892 KB |
Output is correct |
8 |
Correct |
3 ms |
2892 KB |
Output is correct |
9 |
Correct |
2 ms |
2892 KB |
Output is correct |
10 |
Correct |
2 ms |
2892 KB |
Output is correct |
11 |
Correct |
3 ms |
2892 KB |
Output is correct |
12 |
Correct |
3 ms |
2892 KB |
Output is correct |
13 |
Correct |
3 ms |
2892 KB |
Output is correct |
14 |
Correct |
3 ms |
2892 KB |
Output is correct |
15 |
Correct |
3 ms |
2892 KB |
Output is correct |
16 |
Correct |
2 ms |
2892 KB |
Output is correct |
17 |
Correct |
3 ms |
2892 KB |
Output is correct |
18 |
Correct |
3 ms |
2892 KB |
Output is correct |
19 |
Correct |
2 ms |
2892 KB |
Output is correct |
20 |
Correct |
5 ms |
2892 KB |
Output is correct |
21 |
Correct |
3 ms |
2892 KB |
Output is correct |
22 |
Correct |
3 ms |
2892 KB |
Output is correct |
23 |
Correct |
5 ms |
2892 KB |
Output is correct |
24 |
Correct |
5 ms |
2892 KB |
Output is correct |
25 |
Correct |
4 ms |
2892 KB |
Output is correct |
26 |
Correct |
5 ms |
2892 KB |
Output is correct |
27 |
Correct |
5 ms |
2892 KB |
Output is correct |
28 |
Correct |
5 ms |
2892 KB |
Output is correct |
29 |
Correct |
7 ms |
2892 KB |
Output is correct |
30 |
Correct |
4 ms |
2892 KB |
Output is correct |
31 |
Correct |
5 ms |
2892 KB |
Output is correct |
32 |
Correct |
7 ms |
2892 KB |
Output is correct |
33 |
Correct |
11 ms |
2892 KB |
Output is correct |
34 |
Correct |
8 ms |
2892 KB |
Output is correct |
35 |
Correct |
11 ms |
3052 KB |
Output is correct |
36 |
Correct |
4 ms |
3052 KB |
Output is correct |
37 |
Correct |
12 ms |
4076 KB |
Output is correct |
38 |
Correct |
15 ms |
4076 KB |
Output is correct |
39 |
Correct |
12 ms |
4076 KB |
Output is correct |
40 |
Correct |
13 ms |
4076 KB |
Output is correct |
41 |
Correct |
18 ms |
4076 KB |
Output is correct |
42 |
Correct |
10 ms |
4076 KB |
Output is correct |
43 |
Correct |
10 ms |
4076 KB |
Output is correct |
44 |
Correct |
10 ms |
4076 KB |
Output is correct |
45 |
Correct |
44 ms |
6892 KB |
Output is correct |
46 |
Correct |
33 ms |
7020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7020 KB |
Output is correct |
2 |
Correct |
2 ms |
7020 KB |
Output is correct |
3 |
Correct |
2 ms |
7020 KB |
Output is correct |
4 |
Correct |
2 ms |
7020 KB |
Output is correct |
5 |
Correct |
2 ms |
7020 KB |
Output is correct |
6 |
Correct |
3 ms |
7020 KB |
Output is correct |
7 |
Correct |
2 ms |
7020 KB |
Output is correct |
8 |
Correct |
2 ms |
7020 KB |
Output is correct |
9 |
Correct |
2 ms |
7020 KB |
Output is correct |
10 |
Correct |
2 ms |
7020 KB |
Output is correct |
11 |
Correct |
3 ms |
7020 KB |
Output is correct |
12 |
Correct |
3 ms |
7020 KB |
Output is correct |
13 |
Correct |
3 ms |
7020 KB |
Output is correct |
14 |
Correct |
3 ms |
7020 KB |
Output is correct |
15 |
Correct |
3 ms |
7020 KB |
Output is correct |
16 |
Correct |
2 ms |
7020 KB |
Output is correct |
17 |
Correct |
3 ms |
7020 KB |
Output is correct |
18 |
Correct |
2 ms |
7020 KB |
Output is correct |
19 |
Correct |
2 ms |
7020 KB |
Output is correct |
20 |
Correct |
4 ms |
7020 KB |
Output is correct |
21 |
Correct |
2 ms |
7020 KB |
Output is correct |
22 |
Correct |
2 ms |
7020 KB |
Output is correct |
23 |
Correct |
4 ms |
7020 KB |
Output is correct |
24 |
Correct |
5 ms |
7020 KB |
Output is correct |
25 |
Correct |
3 ms |
7020 KB |
Output is correct |
26 |
Correct |
4 ms |
7020 KB |
Output is correct |
27 |
Correct |
4 ms |
7020 KB |
Output is correct |
28 |
Correct |
5 ms |
7020 KB |
Output is correct |
29 |
Correct |
6 ms |
7020 KB |
Output is correct |
30 |
Correct |
5 ms |
7020 KB |
Output is correct |
31 |
Correct |
5 ms |
7020 KB |
Output is correct |
32 |
Correct |
5 ms |
7020 KB |
Output is correct |
33 |
Correct |
11 ms |
7020 KB |
Output is correct |
34 |
Correct |
8 ms |
7020 KB |
Output is correct |
35 |
Correct |
12 ms |
7020 KB |
Output is correct |
36 |
Correct |
4 ms |
7020 KB |
Output is correct |
37 |
Correct |
12 ms |
7020 KB |
Output is correct |
38 |
Correct |
15 ms |
7020 KB |
Output is correct |
39 |
Correct |
13 ms |
7020 KB |
Output is correct |
40 |
Correct |
13 ms |
7020 KB |
Output is correct |
41 |
Correct |
15 ms |
7020 KB |
Output is correct |
42 |
Correct |
10 ms |
7020 KB |
Output is correct |
43 |
Correct |
10 ms |
7020 KB |
Output is correct |
44 |
Correct |
11 ms |
7020 KB |
Output is correct |
45 |
Correct |
68 ms |
7020 KB |
Output is correct |
46 |
Correct |
35 ms |
7020 KB |
Output is correct |
47 |
Correct |
31 ms |
14060 KB |
Output is correct |
48 |
Correct |
13 ms |
14060 KB |
Output is correct |
49 |
Correct |
12 ms |
14060 KB |
Output is correct |
50 |
Correct |
15 ms |
14060 KB |
Output is correct |
51 |
Correct |
48 ms |
24528 KB |
Output is correct |
52 |
Correct |
48 ms |
24556 KB |
Output is correct |
53 |
Correct |
15 ms |
24556 KB |
Output is correct |
54 |
Correct |
22 ms |
24556 KB |
Output is correct |
55 |
Correct |
24 ms |
24556 KB |
Output is correct |
56 |
Correct |
37 ms |
24556 KB |
Output is correct |
57 |
Correct |
4 ms |
24556 KB |
Output is correct |
58 |
Correct |
37 ms |
24556 KB |
Output is correct |
59 |
Correct |
40 ms |
24556 KB |
Output is correct |
60 |
Correct |
37 ms |
24556 KB |
Output is correct |
61 |
Correct |
36 ms |
24556 KB |
Output is correct |
62 |
Correct |
70 ms |
25336 KB |
Output is correct |
63 |
Correct |
236 ms |
64812 KB |
Output is correct |
64 |
Correct |
291 ms |
73524 KB |
Output is correct |
65 |
Correct |
579 ms |
82880 KB |
Output is correct |
66 |
Correct |
835 ms |
82880 KB |
Output is correct |
67 |
Correct |
535 ms |
82880 KB |
Output is correct |