编程介的小学生 2017-03-31 12:51 采纳率: 20.3%
浏览 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
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False