Raging River
Time limit2sMemory limit1024 MB
Find the minimum total time to get all P people across when each plank can be crossed only once, or the fewest people who must stay behind.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path
- Solved
- No attempts yet
Problem
Sally and her friends are trying to cross safely from one bank of a raging river to another. Boulders are scattered within the river. Log planks connect the banks to some of the boulders, and some pairs of boulders to each other.
Sally crosses the river first. She starts at the left bank and crosses one plank at a time, aiming to reach the right bank. Walking across a plank takes one second. Each time she crosses a plank, that plank collapses into the river, so neither Sally nor her friends can use it again. After Sally reaches the right bank safely, another friend tries to cross, and so on, until everyone who can make it has crossed.
Given the graph of banks, boulders, and planks, and the number of people who need to cross, find the smallest total time (in seconds) required for everyone to cross safely. If not everyone can cross, find the minimum number of people who must be left behind.
Input
The first line contains three integers , , and : the number of people who must cross, the number of boulders in the river, and the number of logs that span boulders and river banks. These satisfy , , and .
Each of the next lines contains two integers and , the endpoints of one log. Each value is in the range . The value is the left river bank, is the right river bank, and every other value is a boulder.
Every log has two distinct endpoints, and no two logs span the same pair of boulders or banks. The right bank or some boulders may not be reachable from the left bank.
Output
If all people can reach the right bank, print one integer: the minimum total time (in seconds) for all of them to cross.
Otherwise, print n people left behind, where is the least number of people who must stay on the left bank.