Bancopia
Time limit1sMemory limit128 MB
Place up to m police posts, each halving one road's robbery probability, so that the safest a-to-b route has the smallest possible robbery probability.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Dynamic programming, Brute force
- Solved
- No attempts yet
Problem
Gold transports in Bancopia are risky because of frequent robberies, and some roads are more dangerous than others. To move gold safely between two cities, the Bancopians want to place police posts on some roads to make them safer.
You are given the cities of Bancopia, the roads between them, and the two cities the gold transport travels between. Each road has a robbery probability: given that a gold transport uses that road, this is the probability that a robbery happens on that road. Robberies on different roads are independent.
You are also given the maximum number of police posts you may place. At most one police post can be placed on each road, and a police post exactly halves the robbery probability of that road.
The transport always takes the safest route between the two cities: the route with the smallest overall robbery probability. If a route uses roads with (possibly halved) robbery probabilities , its overall robbery probability is .
Place the police posts so that the robbery probability of the safest route is as small as possible, and report that minimum probability.
Input
The first line contains a single integer : the number of test cases. Each test case has the following format:
- One line with five integers , , , , separated by spaces, where is the number of cities, is the number of roads, is the maximum number of police posts, and and are the two cities between which the transport travels. Cities are numbered with distinct integers from .
- lines, each describing one road with three values , , separated by spaces, where and are the cities the road connects (, ) and is the robbery probability of that road.
No two roads connect the same pair of cities. Every road is bidirectional, and its robbery probability is the same in both directions. It is guaranteed that at least one route from to exists.
Output
For each test case, print a single line containing one number: the robbery probability of the safest route from to when at most police posts are placed to minimize this probability, rounded to four digits after the decimal point. Rounding is done the usual way (round half up): becomes and becomes .

Figure 1: The maps illustrate one scenario. The left map shows the safest route without police posts; the right map shows the safest route after the posts are placed. Cities are numbered and each road is labelled with its robbery probability. Next to the destination city, the robbery probability of the entire route is shown.