Problem one: Trains

本文介绍了一道关于图的笔试题,涉及图的存储、深度优先遍历、最短路径等知识点。作者使用邻接表变种解决前5题,通过Dijkstra算法解决第8、9题,而第10题尚未找到解决方案。
摘要由CSDN通过智能技术生成

分享一道TW的笔试题吧,该题目考查的是图的存储、深度优先遍历、最短路径等知识点。
图共有两种存储方式,第一种是邻接矩阵(二维数组),另外一种是邻接表(map+链表),
我采用的是邻接表的一种变种表示法,将链表用map替代了,为了方便通过顶点的值查找到
另一个顶点的路径。该题共有10个测试用例,目前只有9个测试用例是通过的,第10道题我
目前还没有找到解决方案,前5道题都是用同一种方式来遍历的,用前一个顶点的值为key在
邻接表查找下一个顶点的距离,如果找到就把距离累加起来,只要有一个顶点找不到就返回-1
代表路径不存在。第6、7题是一类题,要求找出符合不超过n站的路径数量,我先用深度优先
遍历出起始顶点到结束顶点所有可能的路径,再选出符合站数要求的路径数。第8,9题是计算
最短路径的,我用了Dijkstra算法计算出所有节点距离起始顶点最近的距离以及最近的顶点,
如果起始顶点和终止顶点不想听则直接返回距离,否则就要找出所有最近的顶点是原始起始顶点的
顶点,分别求得dist=当前节点离原始起始顶点的距离+shortestDist(当前顶点,原始起始顶点)
选择最小的dist即可。第10题我目前没有想到一种方法使得已经到达终止顶点还继续查找再次到达
终止顶点的路径,我目前仅仅求出了C-C有三种路径,题目中给出的那7种感觉是有规律的,像是那
三种路径的排列组合。

Problem one: Trains

 

The local commuter railroad services a number of towns in Kiwiland.  Because of monetary concerns, all of the tracks are 'one-way.'  That is, a route from Kaitaia to Invercargill does not imply the existence of a route from Invercargill to Kaitaia.  In fact, even if both of these routes do happen to exist, they are distinct and are not necessarily the same distance!

 

The purpose of this problem is to help the railroad provide its customers with information about the routes.  In particular, you will compute the distance along a certain route, the number of different routes between two towns, and the shortest route between two towns.

 

Input:  A directed graph where a node represents a town and an edge represents a route between two towns.  The weighting of the edge represents the distance between the two towns.  A given route will never appear more than once, and for a given route, the starting and ending town will not be the same town.

 

Output: For test input 1 through 5, if no such route exists, output 'NO SUCH ROUTE'.  Otherwise, follow the route as given; do not make any extra stops!  For example, the first problem means to start at city A, then travel directly to city B (a distance of 5), then directly to city C (a distance of 4).

  1. The distance of the route A-B-C.
  2. The distance of the route A-D.
  3. The distance of the route A-D-C.
  4. The distance of the route A-E-B-C-D.
  5. The distance of the route A-E-D.
  6. The number of trips starting at C and ending at C with a maximum of 3 stops.  In the sample data below, there are two such trips: C-D-C (2 stops). and C-E-B-C (3 stops).
  7. The number of trips starting at A and ending at C with exactly 4 stops.  In the sample data below, there are three such trips: A to C (via B,C,D); A to C (via D,C,D); and A to C (via D,E,B).
  8. The length of the shortest route (in terms of distance to travel) from A to C.
  9. The length of the shortest route (in terms of distance to travel) from B to B.
  10. The number of different routes from C to C with a distance of less than 30.  In the sample data, the trips are: CDC, CEBC, CEBCDC, CDCEBC, CDEBC, CEBCEBC, CEBCEBCEBC.

 

Test Input:

For the test input, the

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值