#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include <set>
using namespace std;
int N, M;
vector<set<int>> jump;
vector<int> discovered;
vector<unordered_map<int,int>> adjList;
int firstDogeLoc = 0, firstJump = 0;
int secondDogeLoc= 0, secondJump = 0;
void input()
{
cin >> N >> M;
jump = vector<set<int>>(N,set<int>());
cin >> firstDogeLoc >> firstJump;
jump[firstDogeLoc].emplace(firstJump);
cin >> secondDogeLoc >> secondJump;
jump[secondDogeLoc].emplace(secondJump);
for (int i=2; i<M; ++i) {
int start, j;
cin >> start >> j;
jump[start].emplace(j);
}
adjList = vector<unordered_map<int, int>>(N, unordered_map<int,int>());
}
#define INF (int)2e9
vector<int> shortTime;
int dijkstra2()
{
shortTime = vector<int>(N, INF);
priority_queue<pair<int, int>> q;
q.emplace(0, firstDogeLoc);
shortTime[firstDogeLoc] = 0;
while (!q.empty()) {
auto [soFarDist, here] = q.top();
soFarDist = -soFarDist;
q.pop();
if (shortTime[here] < soFarDist)
continue;
if (here == secondDogeLoc)
return shortTime[here];
for (int j : jump[here]) {
// goto right
for (int time=1, there=here+j*time; there < N; ++time, there=here+j*time ) {
int destDist = soFarDist + time;
if (shortTime[there] > destDist) {
shortTime[there] = destDist;
q.emplace(-destDist, there);
}
}
//goto left
for (int time=1, there=here-j*time; there >= 0; ++time, there=here-j*time ) {
int destDist = soFarDist + time;
if (shortTime[there] > destDist) {
shortTime[there] = destDist;
q.emplace(-destDist, there);
}
}
}
}
return -1;
}
int dijkstraV2()
{
shortTime = vector<int>(N, (int)2e9);
vector<bool> visited(N, false);
shortTime[firstDogeLoc] = 0;
while (true) {
int here = INF;
int distHere = INF;
for (int i=0; i<N; ++i) {
if (shortTime[i] < distHere && !visited[i]) {
here = i;
distHere = shortTime[i];
}
}
if (here == secondDogeLoc)
return shortTime[here];
if (distHere == INF)
break;
visited[here] = true;
for (auto [there, dist] : adjList[here]) {
int distThere = distHere + dist;
if (!visited[there] && shortTime[there] > distThere) {
shortTime[there] = distThere;
}
}
}
return -1;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
//freopen("input.txt","r",stdin);
input();
cout << dijkstra2() << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
452 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
640 KB |
Output is correct |
20 |
Correct |
5 ms |
640 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
640 KB |
Output is correct |
25 |
Correct |
1 ms |
640 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
640 KB |
Output is correct |
28 |
Correct |
1 ms |
640 KB |
Output is correct |
29 |
Correct |
2 ms |
768 KB |
Output is correct |
30 |
Correct |
1 ms |
640 KB |
Output is correct |
31 |
Correct |
2 ms |
640 KB |
Output is correct |
32 |
Correct |
1 ms |
660 KB |
Output is correct |
33 |
Correct |
3 ms |
896 KB |
Output is correct |
34 |
Correct |
2 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
640 KB |
Output is correct |
20 |
Correct |
5 ms |
640 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
640 KB |
Output is correct |
25 |
Correct |
2 ms |
640 KB |
Output is correct |
26 |
Correct |
1 ms |
640 KB |
Output is correct |
27 |
Correct |
1 ms |
640 KB |
Output is correct |
28 |
Correct |
1 ms |
640 KB |
Output is correct |
29 |
Correct |
2 ms |
768 KB |
Output is correct |
30 |
Correct |
1 ms |
640 KB |
Output is correct |
31 |
Correct |
2 ms |
640 KB |
Output is correct |
32 |
Correct |
1 ms |
640 KB |
Output is correct |
33 |
Correct |
3 ms |
896 KB |
Output is correct |
34 |
Correct |
2 ms |
896 KB |
Output is correct |
35 |
Correct |
11 ms |
1920 KB |
Output is correct |
36 |
Correct |
2 ms |
768 KB |
Output is correct |
37 |
Correct |
8 ms |
1792 KB |
Output is correct |
38 |
Correct |
13 ms |
2304 KB |
Output is correct |
39 |
Correct |
12 ms |
2304 KB |
Output is correct |
40 |
Correct |
13 ms |
2304 KB |
Output is correct |
41 |
Correct |
14 ms |
2304 KB |
Output is correct |
42 |
Correct |
7 ms |
768 KB |
Output is correct |
43 |
Correct |
7 ms |
768 KB |
Output is correct |
44 |
Correct |
10 ms |
896 KB |
Output is correct |
45 |
Correct |
16 ms |
2432 KB |
Output is correct |
46 |
Correct |
16 ms |
2560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
640 KB |
Output is correct |
20 |
Correct |
6 ms |
640 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
616 KB |
Output is correct |
25 |
Correct |
1 ms |
640 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
640 KB |
Output is correct |
28 |
Correct |
1 ms |
640 KB |
Output is correct |
29 |
Correct |
2 ms |
768 KB |
Output is correct |
30 |
Correct |
1 ms |
640 KB |
Output is correct |
31 |
Correct |
2 ms |
640 KB |
Output is correct |
32 |
Correct |
2 ms |
640 KB |
Output is correct |
33 |
Correct |
3 ms |
896 KB |
Output is correct |
34 |
Correct |
2 ms |
896 KB |
Output is correct |
35 |
Correct |
11 ms |
1920 KB |
Output is correct |
36 |
Correct |
2 ms |
768 KB |
Output is correct |
37 |
Correct |
8 ms |
1664 KB |
Output is correct |
38 |
Correct |
14 ms |
2304 KB |
Output is correct |
39 |
Correct |
12 ms |
2304 KB |
Output is correct |
40 |
Correct |
13 ms |
2304 KB |
Output is correct |
41 |
Correct |
13 ms |
2304 KB |
Output is correct |
42 |
Correct |
7 ms |
768 KB |
Output is correct |
43 |
Correct |
8 ms |
768 KB |
Output is correct |
44 |
Correct |
11 ms |
896 KB |
Output is correct |
45 |
Correct |
16 ms |
2432 KB |
Output is correct |
46 |
Correct |
15 ms |
2432 KB |
Output is correct |
47 |
Correct |
12 ms |
3708 KB |
Output is correct |
48 |
Correct |
10 ms |
4096 KB |
Output is correct |
49 |
Correct |
10 ms |
4224 KB |
Output is correct |
50 |
Correct |
7 ms |
4096 KB |
Output is correct |
51 |
Correct |
21 ms |
6392 KB |
Output is correct |
52 |
Correct |
22 ms |
5884 KB |
Output is correct |
53 |
Correct |
13 ms |
5504 KB |
Output is correct |
54 |
Correct |
4 ms |
3712 KB |
Output is correct |
55 |
Correct |
5 ms |
3712 KB |
Output is correct |
56 |
Correct |
930 ms |
5504 KB |
Output is correct |
57 |
Correct |
6 ms |
4856 KB |
Output is correct |
58 |
Correct |
11 ms |
3840 KB |
Output is correct |
59 |
Correct |
11 ms |
3968 KB |
Output is correct |
60 |
Correct |
11 ms |
3968 KB |
Output is correct |
61 |
Correct |
12 ms |
4224 KB |
Output is correct |
62 |
Correct |
14 ms |
5248 KB |
Output is correct |
63 |
Correct |
37 ms |
6264 KB |
Output is correct |
64 |
Correct |
35 ms |
6264 KB |
Output is correct |
65 |
Correct |
40 ms |
6264 KB |
Output is correct |
66 |
Correct |
70 ms |
7284 KB |
Output is correct |
67 |
Correct |
45 ms |
7284 KB |
Output is correct |