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)