#P7197. [CTSC2002] 购房计划
[CTSC2002] 购房计划
Description
and are business rivals. They both plan to buy a house in the city of , but they want their homes to be as far away from each other as possible. Since 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 form a rectangular grid, containing east-west streets labeled in order by capital letters , and north-south streets labeled in order by numbers . 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, and collected the following information:
- There are east-west streets and north-south streets in the city.
- House is located at intersection .
- The post office is located at intersection .
- The school is blocks away from house .
- House is blocks away from the post office.
- The school is blocks away from the post office.
- House is blocks away from the post office.
.jpg)
Based on the above information, we can obtain two possible layouts of the city . We can see that the positions of house , the post office, and the school are all determined, while house can be located at or , and house can be located at or . Therefore, there always exist two houses in the city that are blocks apart (in map , and ; in map , and ). However, for a fixed pair of houses, the longest distance we can guarantee is only blocks (the distance between and is always blocks). Thus, we should tell and that although there always exist two houses that are blocks apart, the safest suggestion is: one person buys house , and the other buys house .
Below is the strict definition of the required values:
-
The diameter of a feasible city layout is defined as the distance between the two farthest houses in that layout.
-
For a fixed pair of houses , their safety factor is defined as the minimum distance between houses over all feasible city layouts.
and want you to write a program that, based on the information they collected, computes and , where and . You also need to output the safest house-buying suggestion, i.e. all houses such that .
For the example above, in the first layout , and in the second layout . The safety factors between each pair of houses are: . 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 and house .
Input Format
The first line contains two positive integers , 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 . 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, and are strings containing only digits and lowercase letters, with length at most , representing building names. is a capital letter between and , and is a digit, indicating the intersection where the building is located. is a positive integer, indicating the distance between the two buildings. If the first five characters of a building name are the lowercase letters , then it is a purchasable house; otherwise, it is a non-residential building. If the information is of the second form, then must have appeared in the earlier information.
This set of information contains at least two purchasable houses, and at most different buildings in total.
Output Format
The first line contains two integers, representing and , 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
京公网安备 11011102002149号