PAT 图最短路径问题

1018 Public Bike Management(30分)

There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.

The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.

When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.

The above figure illustrates an example. The stations are represented by vertices and the roads correspond to the edges. The number on an edge is the time taken to reach one end station from another. The number written inside a vertex S is the current number of bikes stored at S. Given that the maximum capacity of each station is 10. To solve the problem at S3​, we have 2 different shortest paths:

  1. PBMC -> S1​ -> S3​. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S1​ and then take 5 bikes to S3​, so that both stations will be in perfect conditions.

  2. PBMC -> S2​ -> S3​. This path requires the same time as path 1, but only 3 bikes sent from PBMC and hence is the one that will be chosen.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 numbers: Cmax​ (≤100), always an even number, is the maximum capacity of each station; N (≤500), the total number of stations; Sp​, the index of the problem station (the stations are numbered from 1 to N, and PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers Ci​ (i=1,⋯,N) where each Ci​ is the current number of bikes at Si​ respectively. Then M lines follow, each contains 3 numbers: Si​, Sj​, and Tij​ which describe the time Tij​ taken to move betwen stations Si​ and Sj​. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print your results in one line. First output the number of bikes that PBMC must send. Then after one space, output the path in the format: 0−>S1​−>⋯−>Sp​. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of Sp​ is adjusted to perfect.

Note that if such a path is not unique, output the one that requires minimum number of bikes that we must take back to PBMC. The judge's data guarantee that such a path is unique.

Sample Input:

10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1

Sample Output:

3 0->2->3 0

 题目描述:

        该题可描述为给定一个图,求从起点到终点的最短路径中,所需自行车最少的路径,若需要自行车一样多,则选择带回自行车最少的。开始做该题目时陷入一个误区以为该题和1004题类似,都是求带权最短路径中最大的值所在的路径。其实不然,对于有路径的调度问题,是不能返回的,即从PBMC出来带有一定数目的车,遇到多余的则一起带上,遇到少的,则将手里有的填充,可能出现,刚出PBMC的几个站点都是缺少的,而之后的都是多余车的,那只能从PBMC带足够多的缺少的车填充前面的几个站点,再将后面站点多余车一起带回。所以解决该问题,在找到最短路径后,需要遍历一遍数组,得到需要带出门的车和带回去的车数,再选择其中带出门最少情况下带回去最少对应的路径。

思路一:通过bfs搜索最短路径,再通过遍历每条最短路径得到其带出门和带回来车。时间复杂度O(n!*n)

代码:

def dfs(pat, p, mid):
    sin, sout = 0, 0
    for i in pat:
        if sout+p[i]<mid:
            sin+=mid-sout-p[i]
            sout=0
        else:
            sout = sout+p[i]-mid
    return sin,sout

def emergency(dr, p, s, e, mid):
    res=[]
    visit = [0]*len(p)
    def back1(cur, line):
        if cur == e:
            cost = sum([cp[0] for cp in line])
            patt = [cp[1] for cp in line]
            sin,sout = dfs(patt, p, mid)
            res.append((cost, patt, sin, sout))
            return 
        visit[cur]=1
        for l, c in dr[cur]:
            if not visit[l]:
                back1(l, line + [(c, l)])
 
                visit[l]=0
    back1(s, [])
    res.sort(key=lambda x: (x[0], x[-2], x[-1]))
    return res[0]


if __name__=="__main__":
    c, n, end, m = list(map(int, input().split()))
    vi = [0] + list(map(int, input().split()))
    dr = {}
    for _ in range(m):
        r1, r2, t = list(map(int, input().split()))
        dr[r1] = dr.get(r1,[]) +[(r2, t)]
        dr[r2] = dr.get(r2,[]) +[(r1, t)]
    ans = emergency(dr, vi, 0, end, c//2)
    _, line, send, rec = ans
    print(str(send)+ " " + "->".join(list(map(str, [0]+line))) + " "+ str(rec))

1030 Travel Plan

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

题目描述:

        该题找给定带权无向图起点到终点的最短路径,若路径同长,选择花费最少的。

思路:

        Dijkstra算法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值