import heapq def dijkstra(n, adj, s): dist = [float('inf')] * n dist[s] = 0 pq = [(0, s)] while pq: d, u = heapq.heappop(pq) if d > dist[u]: continue for v, w in adj[u]: if dist[u] + w < dist[v]: dist[v] = dist[u] + w heapq.heappush(pq, (dist[v], v)) return dist def solve_case(case_num, w, e, c, wx, ex, options): # Build adjacency lists for western and eastern stations w_adj = [[] for _ in range(w)] for i in range(w-1): w_adj[i].append((i+1, wx[i])) w_adj[i+1].append((i, wx[i])) e_adj = [[] for _ in range(e)] for i in range(e-1): e_adj[i].append((i+1, ex[i])) e_adj[i+1].append((i, ex[i])) # Calculate shortest paths between all pairs of stations dists = [] for opt in options: # Add the overpass connection u, v = opt w_adj[u-1].append((w+v-2, 1)) w_adj[w+v-2].append((u-1, 1)) e_adj[v-1].append((w+u-2, 1)) e_adj[w+u-2].append((v-1, 1)) # Run Dijkstra's algorithm for each station dist = [] for i in range(w+e-1): if i < w: dist.append(dijkstra(w, w_adj, i)) else: dist.append(dijkstra(e, e_adj, i-w)) # Calculate the average distance avg_dist = sum(dist[i][j] for i in range(w+e-1) for j in range(i+1, w+e-1)) / (w*(e-1) + e*(w-1)) dists.append(avg_dist) # Remove the overpass connection w_adj[u-1].pop() w_adj[w+v-2].pop() e_adj[v-1].pop() e_adj[w+u-2].pop() # Print the results print(f"Case #{case_num}: {' '.join(f'{d:.6f}' for d in dists)}") # Read the input t = int(input()) for case_num in range(1, t+1): w, e, c = map(int, input().split()) wx = list(map(int, input().split())) ex = list(map(int, input().split())) options = [tuple(map(int, input().split())) for _ in range(c)] # Solve the case solve_case(case_num, w, e,