Spontaneous Trip
Time limit3sMemory limit256 MB
Given flight counts between airports, find the most likely airport reached after exactly K random flights starting from ICN.
- Level
Medium5 of 10
- Topics
- Probability, Dynamic programming, Graph
- Solved
- No attempts yet
Problem
Sangil travels on impulse. One journey goes like this.
- He goes to the airport of the city he is in.
- He buys a ticket for one of the flights leaving that airport, picked at random. Every flight leaving the airport is equally likely.
- He boards that flight and lands in another city.
One trip consists of exactly journeys, and it always starts at the airport ICN. Some airports have no departing flight at all, so a trip that reaches one of them cannot continue.
A course is the sequence of airports Sangil passes through during the journeys. The probability of a course is the product of the probabilities of the flights picked in each journey. Among the courses that finish all journeys, find the last airport of the course with the highest probability. Such a course always exists.
Input
The first line contains the number of test cases ().
The first line of each test case contains the number of airports and the number of journeys in one trip (, ).
Each of the next lines contains the IATA code of one airport. A code consists of three uppercase letters, the codes are pairwise different, and one of them is ICN.
Each of the next lines contains integers. The -th integer on the -th line, , is the number of flights that leave airport and land at airport (, ). Airport has departing flights in total, so the probability that Sangil flies from airport to airport is divided by that sum.
Output
For each test case, print the IATA code of the last airport of the most likely course on its own line. If several most likely courses end at different airports, print the code that comes first in alphabetical order.