Military Recruit
InterviewTime limit1sMemory limit128 MB
Given a directed weighted graph, find the smallest D so that at least p percent of ordered pairs of distinct sites have a shortest-path distance of at most D.
- Level
Medium6 of 10
- Topics
- Shortest path, Graph, Sorting, Binary search
- Solved
- No attempts yet
Problem
Tom is a recruit for an elite army unit. As the final part of his exams, he will be dropped into unknown terrain and must march to a designated exit point while carrying all of his heavy gear.
Because he is a little nervous, Tom has studied satellite images of the training area. He has identified every possible entry and exit site, together with the possible connections between sites and their estimated lengths. Because of differences in terrain height these connections are one-directional: the length from one site to another need not equal the length in the opposite direction, so the length from site to may differ from the length from to .
Tom does not know in advance which site will be his start and which will be his exit. He realises that, no matter how hard he trains, he cannot possibly succeed in the worst case — the pair of sites that are farthest apart.
His instructors always choose two distinct sites as the entry and exit, and Tom assumes that every ordered pair of distinct sites is equally likely. For a given pair, the distance he must march is the length of the shortest path from the entry site to the exit site.
Given the sites, the direct connections and their lengths, and Tom's desired success probability , compute the minimum distance that he must be comfortable with so that he passes with probability at least . In other words, is the smallest value for which the fraction of ordered pairs whose shortest-path length is at most is at least .
Input
The first line contains the number of scenarios.
Each scenario is given as follows. The first line contains an integer (), Tom's minimum success probability as a percentage. The next line contains the number of sites (). Each of the following lines contains integers separated by spaces: the -th integer on the -th line is the length of the direct connection from site to site .
Every distance is a non-negative integer less than . The distance from a site to itself is always . A value of means there is no direct connection in that direction. You may assume that every site is reachable from every other site.
Output
For each scenario, first print a line Scenario #i:, where is the scenario number starting at . On the next line, print the minimum distance that Tom must be prepared for. Separate consecutive scenarios with a single blank line.