2017-10-25 03:46

# A Walk Through the Forest

Problem Description

Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer, his office is on one side of a forest, and his house is on the other. A nice walk through the forest, seeing the birds and chipmunks is quite enjoyable.
The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house. He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take.

Input

Input contains several test cases followed by a line containing 0. Jimmy has numbered each intersection or joining of paths starting with 1. His office is numbered 1, and his house is numbered 2. The first line of each test case gives the number of intersections N, 1 < N ≤ 1000, and the number of paths M. The following M lines each contain a pair of intersections a b and an integer distance 1 ≤ d ≤ 1000000 indicating a path of length d between intersection a and a different intersection b. Jimmy may walk a path any direction he chooses. There is at most one path between any pair of intersections.

Output

For each test case, output a single integer indicating the number of different routes through the forest. You may assume that this number does not exceed 2147483647

Sample Input

5 6 1 3 2 1 4 2 3 4 3 1 5 12 4 2 34 5 2 24 7 8 1 3 1 1 4 1 3 7 1 7 4 1 7 5 1 6 7 1 5 2 1 6 2 1 0

Sample Output

2 4

• 点赞
• 写回答
• 关注问题
• 收藏
• 邀请回答

#### 3条回答默认 最新

• qq824827715 2017-10-25 06:23

问题描述
吉米最近工作压力很大，特别是因为他的事故使工作变得困难。劳累了一天之后，他喜欢走路回家。为了使事情变得更美好，他的办公室在森林的一边，他的房子在另一边。在森林里漫步，看到鸟儿和花栗鼠是很享受的。
森林很美，吉米想每天走一条不同的路。他也想在天黑前回家，所以他总是走一条路，朝他的房子走去。他考虑从a到B的路径，如果有一条从B到他家的路线，这条路线比任何可能的路线都要短。计算一下，吉米可能会走多少条不同的路线。
输入
输入包含几个测试用例，后跟一个包含0的行。从1开始，吉米已经数出了每一个交点或路径。他的办公室是1号，他的房子是2号。每个测试用例的第一行了十字路口的数量N,1 < N≤1000,和M以下路径数量的M线每个包含一对路口b和整数距离1≤d≤1000000指示的道路长度d之间的交叉和不同的十字路口b。吉米可能走任何方向他选择的道路。在任意一对交集之间，最多有一条路径。
输出
对于每个测试用例，输出一个整数，表示穿过森林的不同路径的数量。您可以假设这个数字不超过2147483647
样例输入
5 6 1 3 2 3 2 34 34 34 2 34 34 34 34 3 1 5 12 4 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 34 3 1 3 1 3 1 3 1 3 1 34 1 3 1 3 1 3 1 3 1 3 7 4 1 7 4 1 7 4 5 1 7 4 5 1 7 5 1 5 1 5 1 5 2 1 5 2 1 5 2 1 5 2 1 5 1
试样输出
2 4

点赞 评论
• qq824827715 2017-10-25 06:26

You can calculate the shortest route and location navigation after the height map service, and then the algorithm that deviates from the route is very similar to the algorithm of the problem

点赞 评论