№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Название |
---|
This is a tricky problem. You won't get Accepted with a
priority_queue
. Actually, you won't get Accepted with a normal BFS either, because edge weights aren't all equal, you have edges with weight 0 and edges with weight 1. This means that many nodes will be pushed to the queue and processed multiple times, resulting in TLE.In order for BFS to work fast, nodes should be pushed to the queue in non-descending distance. To achieve this, there are two ways that I know of:
1) Everytime you add a node to the queue, perform a flood-fill to all nodes connected to current node through edges with cost 0 and push them to the queue as well.
2) Use two queues, namely q1 and q2. When you get to a node using a 0-cost edge, push the new node to q1. When you get to a node using an edge with cost 1, push the new node to q2. As long as q1 is not empty, pop from q1, otherwise pop from q2. This way, you will process nodes in order of non-descending distance.
Both approaches get Accepted.
2nd method suggested by tenshi_kanade is actually 0-1 BFS. You can find a really nice tutorial about this technique here.