编程介的小学生 2017-03-31 12:51 采纳率: 20.5%
浏览 772
已采纳

Prester John

Where was the Kingdom of Prester John? There are many maps showing the way to it, but unfortunately it seems hard to establish a sensible agreement. India? Ethiopia? Mongolia? Syria? Puzzling��Well, what would one expect from the descendant of the Three Wise Men?

An idea is to follow the directions in two maps at the same time, and if the directions lead to the same place, we can find Prester. Well, "information technology" may help here. Write a program that, given two medieval maps of the world indicating the location of the Mythical Kingdom, finds the length of the common shortest path to the location where the Prester was seen.

Given two maps, compute the minimum length of a common path to the Prester location.

Input

A positive integer P in a single line followed by a sequence of P pairs of maps. Each map is given by a positive integer L, not greater than 50,000, in a single line, indicating the number of locations in the map. Next, there is an integer in the range [0, L-1] indicating the location, in a single line, where the Prester was seen. Next, there is a positive integer P, not greater than 100,000, in a single line, indicating the number of paths in the description of the map. Then, the description of the map follows. Each path in the map is listed in a separate line, and has the form

L1 description L2

where L1 and L2 are integers in the range [0, L-1] indicating a location, and description is a string with no more than 8 characters, indicating the name of a unidirectional path from location L1 to location L2. It is known that location 0 represents the same place in all maps. The descriptions of paths in each map will be distinct.

Output

An integer in a single line indicating the length of the shortest sequence of path descriptions that is common to both maps, and that, in both maps, lead to a location of the Prester. If there is no common path leading to the Prester location, your program should write -1 as result.

Sample Input

1
2
1
2
0 tunnel 1
1 bridge 1
3
2
3
0 tunnel 1
1 bridge 2
2 river 2
Sample Output

2

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-04-10 01:55
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能