Problem:
Problem H: Winter Bells The best night ever in the world has come! It's 8 p.m. of December 24th, yes, the night of Cristmas Eve. Santa Clause comes to a silent city with ringing bells. Overtaking north wind, from a sleigh a reindeer pulls she shoot presents to soxes hanged near windows for children. The sleigh she is on departs from a big christmas tree on the fringe of the city. Miracle power that the christmas tree spread over the sky like a web and form a paths that the sleigh can go on. Since the paths with thicker miracle power is easier to go, these paths can be expressed as undirected weighted graph. Derivering the present is very strict about time. When it begins dawning the miracle power rapidly weakens and Santa Clause can not continue derivering any more. Her pride as a Santa Clause never excuses such a thing, so she have to finish derivering before dawning. The sleigh a reindeer pulls departs from christmas tree (which corresponds to 0th node), and go across the city to the opposite fringe ( n -1 th node) along the shortest path. Santa Clause create presents from the miracle power and shoot them from the sleigh the reindeer pulls at his full power. If there are two or more shortest paths, the reindeer selects one of all shortest paths with equal probability and go along it. By the way, in this city there are p children that wish to see Santa Clause and are looking up the starlit sky from their home. Above the i -th child's home there is a cross point of the miracle power that corresponds to c [ i ]-th node of the graph. The child can see Santa Clause if (and only if) Santa Clause go through the node. Please find the probability that each child can see Santa Clause. Input Input consists of several datasets. The first line of each dataset contains three integers n , m , p , which means the number of nodes and edges of the graph, and the number of the children. Each node are numbered 0 to n -1. Following m lines contains information about edges. Each line has three integers. The first two integers means nodes on two endpoints of the edge. The third one is weight of the edge. Next p lines represent c [ i ] respectively. Input terminates when n = m = p = 0. Output For each dataset, output p decimal values in same order as input. Write a blank line after that. You may output any number of digit and may contain an error less than 1.0e-6. Constraints  3 ≤ n ≤ 100  There are no parallel edges and a edge whose end points are identical.  0 < weight of the edge < 10000  Sample Input 3 2 1
0 1 2
1 2 3
1
4 5 2
0 1 1
0 2 1
1 2 1
1 3 1
2 3 1
1
2
0 0 0 Output for the Sample Input 1.00000000

0.50000000
0.50000000

Solution (Python):



import heapq
INF = 0x7fffffff

def dijkstra(V, to, start):
	dist, path = [INF]*V, [0]*V
	Q = []
	dist[start], path[start] = 0, 1
	heapq.heappush(Q, (0, start))
	while Q:
		t, s = heapq.heappop(Q)
		if dist[s] < t: continue
		for e, cost in to[s]:
			nt = t + cost
			if dist[e] < nt: continue
			if dist[e] > nt:
				path[e] = 0
				dist[e] = nt
				heapq.heappush(Q, (nt, e))
			path[e] += path[s]
	return [dist, path]
	
while True:
	n, m, p = map(int, input().split())
	if n == 0: break
	to = [[] for i in range(n)]
	for i in range(m):
		a, b, c = map(int, input().split())
		to[a].append((b, c))
		to[b].append((a, c))
	dist1, path1 = dijkstra(n, to, 0)
	dist2, path2 = dijkstra(n, to, n-1)
	for i in range(p):
		c = int(input())
		if dist1[c]+dist2[c] == dist2[0]:
			print((path1[c]*path2[c])/path2[0])
		else: print(0)
	print()

Solution (Python):
INF=int(1e9)

while 1:
  n,m,p=map(int,input().split())
  if (n,m,p)==(0,0,0): break
  es=[]
  for i in range(m):
    u,v,w=map(int,input().split())
    es.extend([(u,v,w),(v,u,w)])
  
  d=[[INF]*n for _ in range(n)]
  for i in range(n): d[i][i]=0
  for e in es: d[e[0]][e[1]]=e[2]
  for k in range(n):
    for i in range(n):
      for j in range(n):
        d[i][j]=min(d[i][j],d[i][k]+d[k][j])
  
  dp=[[0]*n for _ in range(n)]
  for e in es:
    if d[0][e[0]]+e[2]+d[e[1]][-1]==d[0][-1]:
      dp[e[0]][e[1]]=1
  for k in range(n):
    for i in range(n):
      for j in range(n):
        dp[i][j]+=dp[i][k]*dp[k][j];
  
  for _ in range(p):
    c=int(input())
    print("{:.9f}".format(dp[0][c]*dp[c][-1]/dp[0][-1]))

Solution (Python):
while 1:
  n,m,p=map(int,input().split())
  if (n,m,p)==(0,0,0): break
  es=[]
  for i in range(m):
    u,v,w=map(int,input().split())
    es.extend([(u,v,w),(v,u,w)])
  
  d=[[float("inf")]*n for _ in range(n)]
  for i in range(n): d[i][i]=0
  for e in es: d[e[0]][e[1]]=e[2]
  for k in range(n):
    d=[[min(d[i][j],d[i][k]+d[k][j]) for j in range(n)] for i in range(n)]
  
  dp=[[0]*n for _ in range(n)]
  for e in es:
    if d[0][e[0]]+e[2]+d[e[1]][-1]==d[0][-1]:
      dp[e[0]][e[1]]=1
  for k in range(n):
    dp=[[dp[i][j]+dp[i][k]*dp[k][j] for j in range(n)] for i in range(n)]
  
  for _ in range(p):
    c=int(input())
    print(dp[0][c]*dp[c][-1]/dp[0][-1])

Solution (Python):
from heapq import heappush, heappop
INF = 10 ** 20
while True:
  n, m, p = map(int, input().split())
  if n == 0:
    break
  edges = [[] for _ in range(n)]
  for _ in range(m):
    x, y, w = map(int, input().split())
    edges[x].append((y, w))
    edges[y].append((x, w))
  que = []
  heappush(que, (0, 0))
  shortest_counter = [[0] * n for _ in range(n)]
  shortest_counter[0][0] = 1
  dist = [INF] * n
  dist[0] = 0
  path_counter = [0] * n
  path_counter[0] = 1
  while que:
    total, node = heappop(que)
    count = shortest_counter[node]
    for to, w in edges[node]:
      new_total = total + w
      new_count = [i for i in count]
      new_count[to] += path_counter[node]
      if dist[to] > new_total:
        dist[to] = new_total
        shortest_counter[to] = [i for i in new_count]
        path_counter[to] = path_counter[node]
        heappush(que, (new_total, to))
      elif dist[to] == new_total:
        shortest_counter[to] = [i + j for i, j in zip(shortest_counter[to], new_count)]
        path_counter[to] += path_counter[node]
  for _ in range(p):
    print(shortest_counter[n - 1][int(input())] / path_counter[n - 1])
  print()