#P4206. [NOI2005] 聪聪与可可
[NOI2005] 聪聪与可可
Description
In a magical forest, there live a clever kitten Congcong and a cute little mouse Keke. Although Cinderella likes both of them very much, Congcong is still a cat and Keke is still a mouse, and one thing never changes: Congcong is always thinking about eating Keke.
One day, Congcong accidentally obtained a very useful device called a GPS, which can accurately locate Keke. With this device, it becomes easy for Congcong to catch Keke. So Congcong plans to set off immediately to find Keke. Poor Keke, unaware of the looming danger, is still playing in the forest without a care. Little Bunny "Guai Guai" hears about this and reports to Cinderella at once. Cinderella decides to stop Congcong as soon as possible to save Keke, but she does not know whether there is still enough time.
The entire forest can be regarded as an undirected graph with beautiful spots, numbered from to . The little animals only rest and play at the spots. There are some roads connecting pairs of spots.
When Congcong gets the GPS, Keke is at spot (). In each subsequent time unit, Keke will choose one of the adjacent spots (possibly multiple) or stay at the current spot. The probabilities of these choices are equal. Suppose there are spots adjacent to spot , namely spots , , …, . If at time Keke is at spot , then at time Keke has a chance to be at spot , a chance to be at spot , …, a chance to be at spot , and a chance to remain at spot .
We know Congcong is very smart, so when she is at spot , she will choose an adjacent spot that is closer to Keke. If there are multiple such spots, she chooses the one with the smallest index. Because Congcong is eager to eat Keke, if she still has not caught Keke after taking the first step, she can take one more step toward Keke within the same time unit. Assume the time spent walking is ignored.
In each time unit, Congcong moves first and Keke moves afterward. At any time, if Congcong and Keke are at the same spot, then poor Keke is eaten.
Cinderella wants to know, on average, how many time units it will take for Congcong to eat Keke. You need to help Cinderella find the answer as quickly as possible.
Input Format
The first line contains two integers and , separated by a space, representing the number of spots in the forest and the number of roads connecting adjacent spots.
The second line contains two integers and , separated by a space, representing the initial spots of Congcong and Keke, respectively.
The next lines each contain two integers. On line , the two integers and indicate that there is a road between spots and . All roads are undirected, i.e., if one can go from to , then one can also go from to .
The input guarantees that there is no more than one direct road between any pair of spots, and that there is a path (direct or indirect) between Congcong and Keke.
Output Format
Output one real number, rounded to three decimal places, representing the expected number of time units after which Congcong will eat Keke.
4 3
1 4
1 2
2 3
3 4
1.500
9 9
9 3
1 2
2 3
3 4
4 5
3 6
4 6
4 7
7 8
8 9
2.167
Hint
Sample Explanation 1:
Initially, Congcong and Keke are at spots 1 and 4, respectively.
At the first time unit, Congcong moves first. She moves toward Keke (spot 4), going to spot 2, then to spot 3; assume the time spent walking is ignored.
Keke then moves, with two possibilities: The first is moving to spot 3, in which case Congcong and Keke are at the same spot, Keke is eaten, the number of time units is , and the probability is . The second is staying at spot 4, not being eaten, with probability .
At the second time unit, Congcong moves toward Keke (spot 4) and needs only one step to reach the same spot as Keke. Therefore, in this case, Congcong will eat Keke in two time units. Hence the expected number of time units is .
Sample Explanation 2:
The forest is shown below:

For 50% of the testdata, . For all testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号