#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i=a; i<int(b); i++)
#define all(x) x.begin(),x.end()
int main() {
int n,m;
cin >> n >> m;
vector<pair<int,int> > doges(m);
vector<set<int> > scrapers(n); //For each scraper a set of all dogs that reside there from beginning, or rather their jumps
rep(i,0,m) {
int b,p;
cin >> b >> p;
doges[i]=make_pair(b,abs(p));
scrapers[b].insert(-p);
}
queue<vector<int> > order; //Each element is <numjumps,position, jumplen, 1 or 0 for left/right>
int dp[n];
rep (i,0,n) dp[i]=1000000000;
int start,stop;
start=doges[0].first;
stop=doges[1].first;
dp[start]=0;
for (auto p:scrapers[start]) {
order.push({0,start,-p,1});
order.push({0,start,-p,0});
}
while (!order.empty()) {
auto curr =order.front();
order.pop();
int num,pos,p;
num=-curr[0];
pos=curr[1];
if (pos==stop) break;
p=curr[2];
bool left = curr[3]==1;
int next;
if (left) next=pos+p;
else next=pos-p;
if (next>=0 && next<n) {
if (dp[next]>num+1) {
dp[next]=num+1;
for (auto pn:scrapers[next]){
if (-pn!=p) {
order.push({-num-1,next,-pn,1});
order.push({-num-1,next,-pn,0});
}
}
order.push({-num-1,next,p,curr[3]});
}
else if (scrapers[next].find(p)==scrapers[next].end()) {
order.push({-num-1,next,p,curr[3]});
}
}
}
if (dp[stop] == 1000000000) cout << -1 << endl;
else cout << dp[stop]<< endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
2 ms |
488 KB |
Output is correct |
6 |
Correct |
3 ms |
488 KB |
Output is correct |
7 |
Correct |
3 ms |
496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
572 KB |
Output is correct |
2 |
Correct |
3 ms |
572 KB |
Output is correct |
3 |
Correct |
2 ms |
572 KB |
Output is correct |
4 |
Correct |
3 ms |
572 KB |
Output is correct |
5 |
Correct |
3 ms |
572 KB |
Output is correct |
6 |
Correct |
3 ms |
572 KB |
Output is correct |
7 |
Correct |
3 ms |
572 KB |
Output is correct |
8 |
Correct |
2 ms |
572 KB |
Output is correct |
9 |
Correct |
2 ms |
572 KB |
Output is correct |
10 |
Correct |
4 ms |
620 KB |
Output is correct |
11 |
Correct |
4 ms |
748 KB |
Output is correct |
12 |
Correct |
3 ms |
748 KB |
Output is correct |
13 |
Correct |
4 ms |
748 KB |
Output is correct |
14 |
Correct |
6 ms |
892 KB |
Output is correct |
15 |
Correct |
5 ms |
892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
892 KB |
Output is correct |
2 |
Correct |
2 ms |
892 KB |
Output is correct |
3 |
Correct |
3 ms |
892 KB |
Output is correct |
4 |
Correct |
2 ms |
892 KB |
Output is correct |
5 |
Correct |
2 ms |
892 KB |
Output is correct |
6 |
Correct |
3 ms |
892 KB |
Output is correct |
7 |
Correct |
3 ms |
892 KB |
Output is correct |
8 |
Correct |
2 ms |
892 KB |
Output is correct |
9 |
Correct |
2 ms |
892 KB |
Output is correct |
10 |
Correct |
3 ms |
892 KB |
Output is correct |
11 |
Correct |
3 ms |
892 KB |
Output is correct |
12 |
Correct |
3 ms |
892 KB |
Output is correct |
13 |
Correct |
4 ms |
892 KB |
Output is correct |
14 |
Correct |
6 ms |
892 KB |
Output is correct |
15 |
Correct |
5 ms |
892 KB |
Output is correct |
16 |
Correct |
3 ms |
892 KB |
Output is correct |
17 |
Correct |
5 ms |
896 KB |
Output is correct |
18 |
Correct |
3 ms |
896 KB |
Output is correct |
19 |
Correct |
4 ms |
896 KB |
Output is correct |
20 |
Correct |
3 ms |
896 KB |
Output is correct |
21 |
Correct |
3 ms |
896 KB |
Output is correct |
22 |
Correct |
4 ms |
896 KB |
Output is correct |
23 |
Correct |
3 ms |
896 KB |
Output is correct |
24 |
Correct |
5 ms |
896 KB |
Output is correct |
25 |
Correct |
5 ms |
896 KB |
Output is correct |
26 |
Correct |
4 ms |
896 KB |
Output is correct |
27 |
Correct |
4 ms |
896 KB |
Output is correct |
28 |
Correct |
6 ms |
896 KB |
Output is correct |
29 |
Correct |
11 ms |
896 KB |
Output is correct |
30 |
Correct |
4 ms |
896 KB |
Output is correct |
31 |
Correct |
7 ms |
896 KB |
Output is correct |
32 |
Correct |
6 ms |
896 KB |
Output is correct |
33 |
Correct |
23 ms |
1148 KB |
Output is correct |
34 |
Correct |
11 ms |
1148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1148 KB |
Output is correct |
2 |
Correct |
3 ms |
1148 KB |
Output is correct |
3 |
Correct |
2 ms |
1148 KB |
Output is correct |
4 |
Correct |
3 ms |
1148 KB |
Output is correct |
5 |
Correct |
2 ms |
1148 KB |
Output is correct |
6 |
Correct |
2 ms |
1148 KB |
Output is correct |
7 |
Correct |
3 ms |
1148 KB |
Output is correct |
8 |
Correct |
2 ms |
1148 KB |
Output is correct |
9 |
Correct |
3 ms |
1148 KB |
Output is correct |
10 |
Correct |
3 ms |
1148 KB |
Output is correct |
11 |
Correct |
6 ms |
1148 KB |
Output is correct |
12 |
Correct |
5 ms |
1148 KB |
Output is correct |
13 |
Correct |
3 ms |
1148 KB |
Output is correct |
14 |
Correct |
5 ms |
1148 KB |
Output is correct |
15 |
Correct |
5 ms |
1148 KB |
Output is correct |
16 |
Correct |
4 ms |
1148 KB |
Output is correct |
17 |
Correct |
6 ms |
1148 KB |
Output is correct |
18 |
Correct |
3 ms |
1148 KB |
Output is correct |
19 |
Correct |
3 ms |
1148 KB |
Output is correct |
20 |
Correct |
4 ms |
1148 KB |
Output is correct |
21 |
Correct |
3 ms |
1148 KB |
Output is correct |
22 |
Correct |
3 ms |
1148 KB |
Output is correct |
23 |
Correct |
4 ms |
1148 KB |
Output is correct |
24 |
Correct |
5 ms |
1148 KB |
Output is correct |
25 |
Correct |
4 ms |
1148 KB |
Output is correct |
26 |
Correct |
5 ms |
1148 KB |
Output is correct |
27 |
Correct |
5 ms |
1148 KB |
Output is correct |
28 |
Correct |
12 ms |
1148 KB |
Output is correct |
29 |
Correct |
10 ms |
1148 KB |
Output is correct |
30 |
Correct |
5 ms |
1148 KB |
Output is correct |
31 |
Correct |
8 ms |
1148 KB |
Output is correct |
32 |
Correct |
6 ms |
1148 KB |
Output is correct |
33 |
Correct |
26 ms |
1148 KB |
Output is correct |
34 |
Correct |
18 ms |
1148 KB |
Output is correct |
35 |
Correct |
56 ms |
4500 KB |
Output is correct |
36 |
Correct |
11 ms |
4500 KB |
Output is correct |
37 |
Correct |
40 ms |
4500 KB |
Output is correct |
38 |
Correct |
66 ms |
5120 KB |
Output is correct |
39 |
Correct |
48 ms |
5120 KB |
Output is correct |
40 |
Correct |
47 ms |
5120 KB |
Output is correct |
41 |
Correct |
65 ms |
5244 KB |
Output is correct |
42 |
Correct |
21 ms |
5244 KB |
Output is correct |
43 |
Correct |
26 ms |
5244 KB |
Output is correct |
44 |
Correct |
21 ms |
5244 KB |
Output is correct |
45 |
Correct |
115 ms |
5628 KB |
Output is correct |
46 |
Correct |
94 ms |
5760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5760 KB |
Output is correct |
2 |
Correct |
3 ms |
5760 KB |
Output is correct |
3 |
Correct |
2 ms |
5760 KB |
Output is correct |
4 |
Correct |
3 ms |
5760 KB |
Output is correct |
5 |
Correct |
2 ms |
5760 KB |
Output is correct |
6 |
Correct |
2 ms |
5760 KB |
Output is correct |
7 |
Correct |
2 ms |
5760 KB |
Output is correct |
8 |
Correct |
3 ms |
5760 KB |
Output is correct |
9 |
Correct |
2 ms |
5760 KB |
Output is correct |
10 |
Correct |
3 ms |
5760 KB |
Output is correct |
11 |
Correct |
5 ms |
5760 KB |
Output is correct |
12 |
Correct |
4 ms |
5760 KB |
Output is correct |
13 |
Correct |
3 ms |
5760 KB |
Output is correct |
14 |
Correct |
7 ms |
5760 KB |
Output is correct |
15 |
Correct |
6 ms |
5760 KB |
Output is correct |
16 |
Correct |
4 ms |
5760 KB |
Output is correct |
17 |
Correct |
4 ms |
5760 KB |
Output is correct |
18 |
Correct |
3 ms |
5760 KB |
Output is correct |
19 |
Correct |
3 ms |
5760 KB |
Output is correct |
20 |
Correct |
4 ms |
5760 KB |
Output is correct |
21 |
Correct |
4 ms |
5760 KB |
Output is correct |
22 |
Correct |
3 ms |
5760 KB |
Output is correct |
23 |
Correct |
5 ms |
5760 KB |
Output is correct |
24 |
Correct |
7 ms |
5760 KB |
Output is correct |
25 |
Correct |
5 ms |
5760 KB |
Output is correct |
26 |
Correct |
4 ms |
5760 KB |
Output is correct |
27 |
Correct |
6 ms |
5760 KB |
Output is correct |
28 |
Correct |
7 ms |
5760 KB |
Output is correct |
29 |
Correct |
11 ms |
5760 KB |
Output is correct |
30 |
Correct |
4 ms |
5760 KB |
Output is correct |
31 |
Correct |
7 ms |
5760 KB |
Output is correct |
32 |
Correct |
8 ms |
5760 KB |
Output is correct |
33 |
Correct |
19 ms |
5760 KB |
Output is correct |
34 |
Correct |
12 ms |
5760 KB |
Output is correct |
35 |
Correct |
48 ms |
5760 KB |
Output is correct |
36 |
Correct |
7 ms |
5760 KB |
Output is correct |
37 |
Correct |
49 ms |
5760 KB |
Output is correct |
38 |
Correct |
65 ms |
5760 KB |
Output is correct |
39 |
Correct |
32 ms |
5760 KB |
Output is correct |
40 |
Correct |
39 ms |
5760 KB |
Output is correct |
41 |
Correct |
68 ms |
5760 KB |
Output is correct |
42 |
Correct |
24 ms |
5760 KB |
Output is correct |
43 |
Correct |
24 ms |
5760 KB |
Output is correct |
44 |
Correct |
21 ms |
5760 KB |
Output is correct |
45 |
Correct |
106 ms |
5760 KB |
Output is correct |
46 |
Correct |
82 ms |
5760 KB |
Output is correct |
47 |
Correct |
45 ms |
5760 KB |
Output is correct |
48 |
Correct |
28 ms |
5760 KB |
Output is correct |
49 |
Correct |
30 ms |
5760 KB |
Output is correct |
50 |
Correct |
29 ms |
5760 KB |
Output is correct |
51 |
Correct |
72 ms |
5760 KB |
Output is correct |
52 |
Correct |
58 ms |
5760 KB |
Output is correct |
53 |
Correct |
45 ms |
5760 KB |
Output is correct |
54 |
Correct |
5 ms |
5760 KB |
Output is correct |
55 |
Correct |
7 ms |
5760 KB |
Output is correct |
56 |
Correct |
33 ms |
5760 KB |
Output is correct |
57 |
Correct |
5 ms |
5760 KB |
Output is correct |
58 |
Correct |
23 ms |
5760 KB |
Output is correct |
59 |
Correct |
33 ms |
5760 KB |
Output is correct |
60 |
Correct |
31 ms |
5760 KB |
Output is correct |
61 |
Correct |
31 ms |
5760 KB |
Output is correct |
62 |
Correct |
154 ms |
5760 KB |
Output is correct |
63 |
Correct |
434 ms |
5760 KB |
Output is correct |
64 |
Correct |
647 ms |
5760 KB |
Output is correct |
65 |
Correct |
596 ms |
5760 KB |
Output is correct |
66 |
Execution timed out |
1079 ms |
7036 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |