PAT 甲级 1003

1003 Emergency (25)

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

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.

Output

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.

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

# 思路
# 首先使用迪杰斯特拉算法寻找出src到每一个城市的最短路径大小
# 再使用深度搜索算法,寻找出到dst的路径数量和最大救护车的数量


INFINITY = 10000000
class City():
    def __init__(self, id, citynum):
        self.id = id
        self.team = 0
        self.distance = INFINITY
        self.visit = 0
        self.to = [INFINITY for i in range(citynum)]

def Min(City):
    min = 1000000
    index = -1
    for i in range(len(City)):
        if City[i].distance < min and City[i].visit == 0:
            min = City[i].distance
            index = i
    return index

# ----------------------------------------
# 读取数据内容
N,M,C1,C2 = map(int, input().split())
City = [City(i,N) for i in range(N)]
cityteam = input().split()
for i in range(N):
    City[i].team = int(cityteam[i])

for i in range(M):
    source,dest,distance = map(int, input().split())
    City[source].to[dest] = distance
    City[dest].to[source] = distance

# 初始化
start = C1
nomark = N
rescue_option = 0 # optional route
City[start].visit = 1
City[start].routenum = 1
City[start].distance = 0
nomark -= 1

# 迪杰斯特拉算法
while(nomark >= 0):
    for i in range(N):
        if City[start].to[i] != INFINITY and City[i].visit == 0:
            if City[start].distance + City[start].to[i] < City[i].distance:
                City[i].distance = City[start].distance + City[start].to[i]
                City[i].routenum = City[start].routenum

    start = Min([City[i] for i in range(N)])
    City[start].visit = 1
    nomark -= 1


visit = [0 for i in range(N)]
maxteam = -1
routenum = -1

# 深度搜索
def dfs(src, dst, curdst, cur_rescueteam):
    global maxteam # python特性,声明全局变量
    global routenum
    visit[src] = 1
    if src == dst:
        routenum += 1
        if curdst == City[C2].distance:
            if cur_rescueteam > maxteam:
                maxteam = cur_rescueteam
                return

    if curdst > City[C2].distance: # 提前终止
        return

    for i in range(N):
        if visit[i] == 0 and City[src].to[i] != INFINITY:
            dfs(i,dst,curdst+City[src].to[i],cur_rescueteam+City[i].team)
            visit[i] = 0

    return

dfs(C1,C2,0,City[C1].team) # 深度搜索寻找最短路径数量和最大救护车数量

print(routenum,maxteam)
### 关于 PAT 甲级 1024 题目 PAT (Programming Ability Test) 是一项编程能力测试,其中甲级考试面向有一定编程基础的学生。对于 PAT 甲级 1024 题目,虽然具体题目描述未直接给出,但从相似类型的题目分析来看,这类题目通常涉及较为复杂的算法设计。 #### 数据结构的选择与实现 针对此类问题,常用的数据结构包括但不限于二叉树节点定义: ```cpp struct Node { int val; Node* lchild, *rchild; }; ``` 此数据结构用于表示二叉树中的节点[^1]。通过这种方式构建的二叉树能够支持多种遍历操作,如前序、中序和后序遍历等。 #### 算法思路 当处理涉及到图论的问题时,深度优先搜索(DFS)是一种常见的解题策略。特别是当需要寻找最优路径或访问尽可能多的节点时,结合贪心算法可以在某些情况下提供有效的解决方案[^2]。 #### 输入输出格式说明 根据以往的经验,在解决 PAT 类型的问题时,输入部分往往遵循特定模式。例如,给定 N 行输入来描述每个节点的信息,每行按照如下格式:“Address Data Next”,这有助于理解如何解析输入并建立相应的数据模型[^4]。 #### 数学运算示例 有时也会遇到基本算术表达式的求值问题,比如分数之间的加减乘除运算。下面是一些简单的例子展示不同情况下的计算结果: - \( \frac{2}{3} + (-2) = -\frac{7}{3}\) -2) = -\frac{4}{3}\) - \( \frac{2}{3} ÷ (-2) = -\frac{1}{3}\) 这些运算是基于样例提供的信息得出的结果[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值