Hi All,
I was trying to solve this using Dijkstra.
I'm getting TLE in the last few test cases. Can someone suggest me any further optimizations I can make?
thanks! have a good day!
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | djm03178 | 152 |
Hi All,
I was trying to solve this using Dijkstra.
I'm getting TLE in the last few test cases. Can someone suggest me any further optimizations I can make?
thanks! have a good day!
Название |
---|
[DELETED]
No proper question will use this optimization as a solution.
i am just a beginner,i was just trying to help with the relatively little information i had that's all.
Ah, I see. However, this trick should only be used as a crutch when your solution barely doesn't pass the TL.
For practice sessions however, I would recommend avoiding this command, as it usually doesn't help you learn how to properly optimize your code
i understand, i only started CP a few weeks ago. so it's a pretty good opportunity to learn optimization techniques.
Your code actually runs in $$$O(\sum deg_i \cdot K_i)$$$. This is due to the
addWaitingCost(node, time)
code having $$$O(K_{node})$$$ complexity, and you calculate this value $$$O(deg_i)$$$ times for each vertex.An alternative idea will be to calculate
addWaitingCost(node, time)
when you reach a node and add the value to the current distance. This will be optimal as the faster you reach a node, the faster you can exit out of that node.Whilst this might seem slow at first, this will have an amortized complexity of $$$O(\sum K_i)$$$, since you compute the value only once for each vertex.
I would recommend reading the tutorial of the contest if you are stuck.
Even this approcah TLE'd on same testcase :(
Problem of modified code next: let's we can come to vertex $$$v$$$ at times $$$t_1 < t_2 < t_3 < \cdots < t_d$$$. Right code would process only pair ($$$v$$$, $$$t_1$$$) and skip all others. For that reason you have next line of code:
And it is correct line of code. Correct, but not in your case. That condition assumes that $$$dist[curNode] == t_1$$$, but later in dijkstra you update $$$dist[curNode]$$$:
And it breaks all! There could be test where $$$t_1 < t_2 < t_3 < \cdots < t_d < t_1 + waitingCost$$$. Because of that you'll actually process all pairs ($$$v$$$, $$$t_1$$$), ($$$v$$$, $$$t_2$$$), ($$$v$$$, $$$t_3$$$), ..., ($$$v$$$, $$$t_d$$$) — and it would be TL.
It could be easily fixed by updating $$$curDist$$$, not $$$dist[curNode]$$$:
Full AC version of code
P.S.
You had very strange output:
I think that this version a bit more convenient:
I solved this problem a few months ago, and used two binary searches.
Haven't read the editorial for this problem, but this is how I solved it, at least.
Thanks for your help!
I'll try this too! X)
Just use Dijkstra's algorithm to find shortest path from node 1 to any node and while on a node just wait till the current node is "OK" to go. You can simply find the first time after you reach it which is "OK" to go, just this can be done just by simply traversing the arrival list at that node.
See my implementation for more details:
Solution
PS:By "OK" I mean you can go from that node after all consecutive arrivals gone.
Sorry for my English.