South African Computer Olympiad 2008 Day 1 Problem 3 - Visiting Grandma

View as PDF

Points: 17 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem type

Bruce 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 :

1. Bruce goes from Town (purchasing cookies at )
2. Bruce goes from Town (purchasing cookies at )
3. Bruce goes from Town (purchasing cookies at )
4. 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

• Kirito  commented on April 4, 2020, 8:22 p.m.

What do we output if it is impossible to satisfy the given conditions?

• 4fecta  commented on April 4, 2020, 8:27 p.m.

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.

• pblpbl  commented on April 4, 2020, 9:12 p.m.

Wait if N = 1, the upper bound for M is N-1 = 0. This may mean that N can't be 1.

• yeerk16  commented on April 19, 2020, 6:28 p.m.

@pblpbl -- I just changed the minimum number of towns to be 2. Thank you for your comment!

• pblpbl  commented on April 4, 2020, 4:17 p.m.

Can paths visit more than one cookie store?