South African Computer Olympiad 2008 Day 1 Problem 3 - Visiting Grandma
View as PDFBruce plans to visit his beloved grandmother whom he has not seen in ages. The petrol price is high and the roads are dangerous, so he wants to travel the shortest possible distance. He also wants to know how much choice he has for the route, as he wants to visit his grandmother more often and being Bruce, he loves change.
Bruce wants to surprise his grandmother with a box of cookies every time he visits. Bruce does not know how to make real cookies (digital ones are easy, however!), so he will have to go past a cookie store along the way, possibly increasing the length of the shortest distance.
Task
Bruce will start off in his home town, numbered , and his grandmother lives in town 
. Given the distance between each pair of towns and the location of the cookie stores, your task is to determine the length of the shortest route and the number of routes having that length. All routes must visit a town with a cookie store. Bruce is only interested in the last six digits of the number of routes, i.e. the remainder after division by 
.
Input Specification
The first line contains a single integer , representing the number of towns (numbered 
 to 
). The next 
 lines each contain 
 space-separated integers. The 
 integer on the 
 line, 
, is the distance between town 
 and town 
. Following this is a line containing a single integer 
, the number of cookie stores. The last line contains 
 space-separated integers, each representing a town 
 that has a cookie store.
Output Specification
Output two space-separated integers, the length of the shortest route, followed by the remainder when the number of routes having this length is divided by .
Constraints
- For 
of tests,
.
 for all
(i.e. distance from a town to itself is zero)
(i.e. distance from town
to town
is the same as the distance from town
to town
)
Sample Input
5
0 2 2 1 1
2 0 1 2 1
2 1 0 2 1
1 2 2 0 2
1 1 1 2 0
2
2 4
Sample Output
3 4
Explanation
In the sample input there are  towns, with Towns 
 and 
 containing cookie stores. Bruce, who lives in Town 
, wants to visit his grandmother in Town 
. There are four routes having a distance of 
:
- Bruce goes from Town 
(purchasing cookies at
)
 - Bruce goes from Town 
(purchasing cookies at
)
 - Bruce goes from Town 
(purchasing cookies at
)
 - Bruce goes from Town 
(purchasing cookies at
)
 
Notice that travelling from Town  directly has a distance of only 
. However, Bruce would then arrive at his grandmother's without any cookies!
Comments
If a shortest path traverses two city with a store each, this counts as a single route or two? i.e. is it a route identified also by which particular city we use for the store?
What do we output if it is impossible to satisfy the given conditions?
The input format implies that the graph is complete (therefore connected). Also, the lower bound on
 is 1, so there is always at least 1 cookie shop Bruce can visit. Thus, there will always be at least one path from town 1 to town 
 that visits a cookie shop.
Wait if
, the upper bound for 
 is 
. This may mean that 
 can't be 
.
pblpbl -- I just changed the minimum number of towns to be 2. Thank you for your comment!
Can paths visit more than one cookie store?
Yes.