首页 > 试题广场 >

Emergency (25)

[编程题]Emergency (25)
  • 热度指数:5179 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

输入描述:
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively.  The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city.  Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively.  
It is guaranteed that there exists at least one path from C1 to C2.


输出描述:
For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.
示例1

输入

5 6 0 2<br/>1 2 1 5 3<br/>0 1 1<br/>0 2 2<br/>0 3 1<br/>1 2 1<br/>2 4 1<br/>3 4 1

输出

2 4
a = list(map(int,input().split()))
weight = list(map(int,input().split()))             # 点权
G = [[1000000000] * a[0] for j in range(a[0])]      # 邻接矩阵
for i in range(a[1]):
    c = list(map(int,input().split()))
    G[c[0]][c[1]],G[c[1]][c[0]] = c[2],c[2]
d = [1000000000] * a[0]                             # 最短距离
num = [0] * a[0]                                    # 最短路径条数
w = [0] * a[0]                                      # 最大点权和
vis = [1] * a[0]                                    # 是否访问过
d[a[2]] = 0
w[a[2]] = weight[a[2]]
num[a[2]] = 1

while True:
    u,MIN = -1,1000000000
    for j in range(a[0]):
        if vis[j] and d[j] < MIN:
            u,MIN = j,d[j]
    if u == -1:                                     # 全访问完或不可达
        break
    vis[u] = 0
    
    for v in range(a[0]):
        if(vis[v] and G[u][v] != 1000000000):       # 未曾访问且可达
            if d[u] + G[u][v] < d[v]:               # 如果距离更短
                d[v] = d[u] + G[u][v]
                w[v] = w[u] + weight[v]
                num[v] = num[u]
            elif d[u] + G[u][v] == d[v]:            # 如果权值更大
                if w[u] + weight[v] > w[v]:
                    w[v] = w[u] + weight[v]
                num[v] += num[u]

print(num[a[3]],w[a[3]])


核心就是Dijkstra算法了,用对了就很快。
编辑于 2020-07-13 11:09:46 回复(0)

问题信息

难度:
1条回答 7607浏览

热门推荐

通过挑战的用户