import sys # function to calculate the average path length before and after connecting a western and an eastern station def calculate_average_path_length(W, E): # calculate average path length before connection avg_length_before = ((W-1)*(W-2)//2 + (E-1)*(E-2)//2) / (W+E-2) # create adjacency matrix for complete map after connection graph = [[sys.maxsize for j in range(W+E)] for i in range(W+E)] for i in range(W): for j in range(i+1, W): graph[i][j] = graph[j][i] = 1 for i in range(E): for j in range(i+1, E): graph[i+W][j+W] = graph[j+W][i+W] = 1 for i in range(W): for j in range(E): graph[i][j+W] = graph[j+W][i] = 2 # use Floyd-Warshall algorithm to find shortest paths between all pairs of stations for k in range(W+E): for i in range(W+E): for j in range(W+E): graph[i][j] = min(graph[i][j], graph[i][k]+graph[k][j]) # calculate average path length after connection total_length_after = sum([sum(graph[i]) for i in range(W+E)]) avg_length_after = total_length_after / (W*E) # return the difference between the average path length after and before connection return avg_length_after - avg_length_before