[Python](PAT)1030 Travel Plan (30 分)

43 篇文章 0 订阅

A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤500) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input:

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Sample Output:

0 2 3 3 40

题目大意

给定N,M,S,D即城市数量,道路数量,起点和终点。之后给定道路信息,要求找到从起点到终点的最短路径,同时费用最低。优先级是最短路径>最低费用。

分析

使用Dijkstra算法求最短路径和最小点权。

使用path数组存储从起点到该点的最优路径

使用weight数组存储从起点到该点最短路径长度

使用point数组存储从起点到该点最少花费

使用visted数组存储该点是否已经最优化。

每一次从挑选出来的node节点出发,向周围还没有最优化的点进行试探,如果从node节点到周边的点的路径长度小于该点当前的路径长度,就更新该点的路径长度、花费和路径。如果从node节点到周边的点的路径长度等于该点当前的路径长度,就检查从node节点出发到当前点的花费是否更低,如果更低则更新路径和花费。全部可接触点试探结束后,从所有已经被试探(即最短路径不为无穷大)的点中找出还不是最优的点,找出其中路径最短的点,将它设为下一个node,且在visted数组中设置其已经为最优,无法再优化。进入下一次循环,如果没有可以继续的点,则把node设为-1,结束循环。

最后输出路径、最短路径长度以及最小花费即可。

python实现

def main():
    line = input().split(" ")
    n,m,s,d = [int(x) for x in line]
    route = [ [[-1,-1] for x in range(n)] for x in range(n)]
    for x in range(m):
        line = input().split(" ")
        a, b, dis, cost = [int(x) for x in line]
        route[a][b], route[b][a] = [dis,cost], [dis,cost]
    path = [[] for x in range(n)]
    weight = [float("inf") for x in range(n)]
    point = [float("inf") for x in range(n)]
    visted = [0 for x in range(n)]
    path[s] = [s]
    visted[s] = 1
    point[s], weight[s] = 0, 0
    node = s
    while(node != -1):
        nodes = [x for x in range(n) if route[node][x] != [-1,-1] and visted[x] == 0]
        for x in nodes:
            if weight[node] + route[node][x][0] < weight[x]:
                weight[x] = weight[node] + route[node][x][0]
                point[x] = point[node] + route[node][x][1]
                path[x] = path[node] + [x]
            elif weight[node] + route[node][x][0] == weight[x]:
                if point[node] + route[node][x][1] < point[x]:
                    path[x] = path[node] + [x]
                    point[x] = point[node] + route[node][x][1]
        avai = [x for x in range(n) if weight[x] != float("inf") and visted[x]==0]
        if len(avai) == 0:
            node = -1
        else:
            node = sorted(avai,key = lambda x : weight[x])[0]
            visted[node] = 1
    print(*path[d], weight[d], point[d])
 
if __name__ == "__main__":
    main()

 python写PAT,答案都在这了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值