from collections import defaultdict def calculate_distances(adj_list, n): # This function calculates the shortest distances between all pairs of nodes # in the graph using Floyd-Warshall algorithm. # The input `adj_list` is an adjacency list representation of the graph, # and `n` is the number of nodes in the graph. dist = [[float('inf')]*n for _ in range(n)] for u in range(n): dist[u][u] = 0 for v, w in adj_list[u]: dist[u][v] = w for k in range(n): for i in range(n): for j in range(n): dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) return dist def solve_case(case_num, w, e, c, west, east, options): # Create an adjacency list representation of the graph. # The nodes are numbered from 0 to w+e-1, where the first w nodes # represent the western stations and the next e nodes represent # the eastern stations. adj_list = defaultdict(list) for i in range(w-1): adj_list[i].append((i+1, 1)) adj_list[i+1].append((i, 1)) for i in range(e-1): adj_list[w+i].append((w+i+1, 1)) adj_list[w+i+1].append((w+i, 1)) for a, b in options: # Add the overpass connection to the graph. adj_list[a-1].append((w+b-1, 2)) adj_list[w+b-1].append((a-1, 2)) # Calculate the shortest distances between all pairs of nodes. dist = calculate_distances(adj_list, w+e) # Calculate the average distance of the map resulting from each option. avg_distances = [] for a, b in options: total_dist = 0 for i in range(w): for j in range(w, w+e): total_dist += dist[i][j] avg_dist = total_dist / (w*e) avg_distances.append(avg_dist) # Print the output. print("Case #{}: {}".format(case_num, ' '.join("{:.6f}".format(d) for d in avg_distances))) # Read the input and solve each test case. t = int(input()) for i in range(t): w, e, c = map(int, input().split()) west = list(map(int, input().split())) east = list(map(int, input().split())) options = [tuple(map(int, input().split())) for j in range(c)] solve_case(i+1, w, e, c, west, east, options)