# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Name |
---|
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.