Drunkard's Path
Time limit0.5sMemory limit1024 MB
Given bar qualities and distances, find the bar where Marcos is most likely to be after infinitely many moves, starting from bar b.
- Level
Medium5 of 10
- Topics
- Probability, Matrix, Simulation
- Solved
- No attempts yet
Problem
Since he left prison, Marcos cannot stop drinking. He is a former marathon runner who was wrongly arrested on charges of stealing contest problems and selling them to Russian and Chinese competitors. After five years in prison, he was declared innocent for lack of evidence. The long wrongful imprisonment left him depressed, and since then he spends every night walking from bar to bar, corner to corner.
As his best friend, you are tired of searching the city for him after each night of drinking. So you decide to write a program that predicts where Marcos will be at the end of his nights.
Every night, before he goes out, Marcos tells you which bar he will visit first, so you always know where his drinking starts. After each drink, there is a chance that Marcos moves to another bar. The probability of moving to another bar is proportional to the quality of that bar divided by the distance between his current bar and the destination. The probability of staying at the current bar is proportional to the square root of that bar's quality.
In general, the probability that Marcos goes from bar to bar is
The probability that Marcos stays at the same bar is
These formulas guarantee that for every ,
Marcos drinks far more than the average person. One of his favorite stories when drunk is how he won a Russian vodka drinking contest. You know he can drink an arbitrarily large number of shots. For practical purposes, assume Marcos drinks an infinite number of shots in one night.
Given the bars, their qualities, and the distances between them, write a program that finds the bar where Marcos is most likely to be after his night of drinking.
Hint: the probabilities of Marcos being at each bar always sum to 1.
Input
The input has several test cases. Each test case begins with a line containing two integers and , , the number of bars in the city and the bar where Marcos starts his night.
Then lines follow, each containing one integer. The integer on the -th line is the quality of bar .
Then more lines follow, each containing integers. The -th integer on the -th line is the distance between bar and bar .
It is given that for all and .
The input ends with a line “0 0”, which is not processed.
Output
For each test case, print one line containing a single integer: the index of the bar where Marcos is most likely to be. If several bars tie, print the smallest index.