编程介的小学生 2018-12-25 00:59 采纳率: 20.5%
浏览 596
已采纳

一个类似于旅行商的问题,但是又有不同的地方,用C语言实现?

Problem Description
Jimmy lives in a huge kingdom which contains lots of beautiful cities. He also loves traveling very much, and even would like to visit each city in the country. Jaddy, his secretary, is now helping him to plan the routes, however, Jaddy suddenly find that is quite a tough task because it is possible for Jimmy to ask route’s information toward any city. What was worth? Jaddy has to response for queries about the distance information nearly between any pair of cities due to the undeterminable starting city which Jimmy is living in when he raises a query. Because of the large scale of the whole country, Jaddy feel hopeless to archive such an impossible job, however, in order to gratify his manager, Jaddy is now looking forward to your assistance.
There might be good news about Jaddy’s work: since Jimmy is very lazy and would not like to travel to a destination whose distance between the original city is larger than TWO. That means only one intermediate city among the route is acceptable (Apparently, all the connecting paths between any two cities, if exists, have the same length as ONE). But don’t be fooled: Jimmy also needs to know that how many alternative different routes are available so that he can have more options. In particular two routes were named as different if and only if there is at least one path in the two routes is distinguishable, moreover, if more than one paths exist between a particular pair of cities, they are considered as distinct.

Input
Input has multiple test cases. The first line of the input has a single integer T indication the number of test cases, then each test case following. For each test case, the first line contains two integers N and M indication the number of cities and paths in the country. Then M lines are following, each line contains a pair of integers A and B, separated by space, denoting an undirected path between city A and city B, all the cities are numbered from 1 to N. Then a new line contains a single integer Q, which means there are Q queries following. Each query contains a couple of integers A and B which means querying the distance and number of shortest routes between city A and B, each query occupy a single line separately.
All the test cases are separated by a single blank line.
You can assume that N, Q <= 100000, M <= 200000.

Output
For each test case, firstly output a single line contains the case number, then Q lines for the response to queries with the same order in the input. For each query, if there exists at least one routes with length no longer than TWO, then output two integer separated by a single space, the former is the distance (shortest) of routes and the later means how many different shortest routes Jimmy can choose; otherwise, output a single line contains “The pair of cities are not connected or too far away.” (quotes for clarifying). See the sample data carefully for further details.

Sample Input
2
5 7
1 2
2 3
3 4
4 5
2 5
2 4
1 2
4
1 4
1 2
5 3
5 4

2 0
2
1 1
1 2

Sample Output
Case #1:
2 2
1 2
2 2
1 1
Case #2:
0 1
The pair of cities are not connected or too far away.

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-08-28 22:27
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装