#P7197. [CTSC2002] 购房计划

[CTSC2002] 购房计划

Description

Bill\text{Bill} and Scott\text{Scott} are business rivals. They both plan to buy a house in the city of Javaville\text{Javaville}, but they want their homes to be as far away from each other as possible. Since Javaville\text{Javaville} is a newly developed city, there is no map yet. Therefore, they can only collect information from local residents. This information includes the locations of buildings and the distances between buildings. Although the information is incomplete, it is all correct.

The streets of Javaville\text{Javaville} form a rectangular grid, containing mm east-west streets labeled in order by capital letters A,B,C,\text{A},\text{B},\text{C},\cdots, and nn north-south streets labeled in order by numbers 0,1,2,0,1,2,\cdots. We call the intersection of two streets an intersection, and a segment of street between two intersections a block. All buildings are located at intersections, and each intersection has at most one building. The distance between two buildings is defined as the minimum number of blocks that must be passed to get from one building to the other.

For example, Bill\text{Bill} and Scott\text{Scott} collected the following information:

  • There are 55 east-west streets and 55 north-south streets in the city.
  • House 11 is located at intersection A0A0.
  • The post office is located at intersection A4A4.
  • The school is 44 blocks away from house 11.
  • House 22 is 66 blocks away from the post office.
  • The school is 66 blocks away from the post office.
  • House 33 is 66 blocks away from the post office.

Based on the above information, we can obtain two possible layouts of the city Javaville\text{Javaville}. We can see that the positions of house 11, the post office, and the school are all determined, while house 22 can be located at C0C0 or E2E2, and house 33 can be located at C0C0 or E2E2. Therefore, there always exist two houses in the city that are 66 blocks apart (in map 11, h1h1 and h3h3; in map 22, h1h1 and h2h2). However, for a fixed pair of houses, the longest distance we can guarantee is only 44 blocks (the distance between h2h2 and h3h3 is always 44 blocks). Thus, we should tell Bill\text{Bill} and Scott\text{Scott} that although there always exist two houses that are 66 blocks apart, the safest suggestion is: one person buys house 22, and the other buys house 33.

Below is the strict definition of the required values:

  • The diameter d(S)d(S) of a feasible city layout SS is defined as the distance between the two farthest houses in that layout.

  • For a fixed pair of houses i,ji, j, their safety factor e[i,j]e[i, j] is defined as the minimum distance between houses i,ji, j over all feasible city layouts.

Bill\text{Bill} and Scott\text{Scott} want you to write a program that, based on the information they collected, computes DD and EE, where D=min{d(S)}D=\min\{d(S)\} and E=max{e[i,j]}E=\max\{e[i, j]\}. You also need to output the safest house-buying suggestion, i.e. all houses i,ji, j such that e[i,j]=Ee[i, j]=E.

For the example above, in the first layout d(S1)=6d(S_1)=6, and in the second layout d(S2)=6d(S_2)=6. The safety factors between each pair of houses are: e[1,2]=2e[1,3]=2e[2,3]=4e[1,2]=2,e[1,3]=2,e[2,3]=4. Therefore, $D=min{d(S1),d(S2)}=6,E= max{e[1,2],e[1,3],e[2,3]}=4$, and the safest house-buying suggestion is: house 22 and house 33.

Input Format

The first line contains two positive integers m,n1m,n10m, n(1\le m, n\le 10), representing the number of east-west streets and north-south streets, respectively. The second line contains an integer, representing the number of collected pieces of information t(1t50)t(1\le t\le 50). Starting from the third line, each line describes one piece of collected information, and each piece is in one of the following two forms:

  • name LOCATION r c

or

  • name DISTANCE d name2

These two forms describe the location of a building and the distance between two buildings, respectively.

Here, name\text{name} and name2\text{name2} are strings containing only digits and lowercase letters, with length at most 2020, representing building names. rr is a capital letter between A\text{A} and J\text{J}, and cc is a digit, indicating the intersection where the building is located. dd is a positive integer, indicating the distance between the two buildings. If the first five characters of a building name are the lowercase letters “house”\text{“house”}, then it is a purchasable house; otherwise, it is a non-residential building. If the information is of the second form, then name2\text{name2} must have appeared in the earlier information.

This set of information contains at least two purchasable houses, and at most 2525 different buildings in total.

Output Format

The first line contains two integers, representing DD and EE, separated by one space.

5 5
6
house1 LOCATION A 0
postoffice LOCATION A 4
school DISTANCE 4 house1
house2 DISTANCE 6 postoffice
school DISTANCE 6 postoffice
house3 DISTANCE 6 postoffice
6 4

Hint

All Constraints are mentioned in the input format.

Translated by ChatGPT 5