1003 Emergency (Python实现)

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.

Input Specification:

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, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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.

Sample Input:

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Sample Output:

2 4

Python实现:

n, m, c1, c2 = map(int, input().split())
rescue_num = list(map(int, input().split()))  # 每个城市中救援队的数量
distance = [[float('inf') for i in range(n)] for j in range(n)]  # 城市之间的距离
for i in range(m):
    x, y, z = map(int, input().split())
    distance[x][y] = distance[y][x] = z

min_distance_num = [0 for i in range(n)]  # 最短路径数量
max_rescue_num = [0 for i in range(n)]  # 最大救援队数量
max_rescue_num[c1] = rescue_num[c1]

# 使用Dijkstra算法求解最短路径
distance_table = [float('inf') for i in range(n)]  # 距离表
distance_table[c1] = 0
is_traversed = [False for i in range(n)]  # 是否已遍历
is_traversed[c1] = True
x = c1  # 设定遍历起点
min_distance_num[c1] = 1
while is_traversed[c2] is False:  # 直到c2被遍历,结束循环
    for i in range(n):  # 遍历x的邻接顶点
        if is_traversed[i] is False and distance[x][i] != float('inf'):  # 顶点可达且邻接顶点未被遍历的情况
            # 将已知直接到i的距离与先到x,再从x到i的距离进行比较
            if distance_table[i] > distance_table[x] + distance[x][i]:  # 如果先到x,再从x到i的距离更近
                min_distance_num[i] = min_distance_num[x]  # 更新最短路径数量
                max_rescue_num[i] = max_rescue_num[x] + rescue_num[i]  # 更新最大救援队数量
                distance_table[i] = distance_table[x] + distance[x][i]  # 更新距离表
            elif distance_table[i] == distance_table[x] + distance[x][i]:  # 如果先到x,再从x到i的距离与已知直接到i的距离相同
                min_distance_num[i] = min_distance_num[i] + min_distance_num[x]  # 更新最短路径数量
                if max_rescue_num[i] < max_rescue_num[x] + rescue_num[i]:  # 如果最大救援队数量小于实际最大救援队数量
                    max_rescue_num[i] = max_rescue_num[x] + rescue_num[i]  # 更新最大救援队数量
    next_data = sorted([distance_table[j] for j in range(n) if is_traversed[j] is False])[0]  # 从距离表中找到出发距离最短的点
    next_index = [j for j in range(n) if distance_table[j] == next_data and is_traversed[j] is False][0]  # 找到该距离最短的点的编号
    is_traversed[next_index] = True  # 标记该点已访问,避免重复访问
    x = next_index  # 更新x点,进入下一轮循环

print(min_distance_num[c2], max_rescue_num[c2])
发布了79 篇原创文章 · 获赞 100 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/lee1hong/article/details/103341858