6.5(学号:3025)
import numpy as np
distances = np.array([
[0, 2, 7, np.inf, np.inf, np.inf],
[2, 0, 4, 6, 8, np.inf],
[7, 4, 0, 1, 3, np.inf],
[np.inf, 6, 1, 0, 1, 6],
[np.inf, 8, 3, 1, 0, 3],
[np.inf, np.inf, np.inf, 6, 3, 0]
], dtype=float)
students = np.array([50, 40, 60, 20, 70, 90])
hospital_distances_sum = np.zeros(6)
for i in range(6):
connected_distances = distances[i, :i+1].copy()
connected_distances = connected_distances[connected_distances != np.inf]
hospital_distances_sum[i] = np.sum(connected_distances)
hospital_location = np.argmin(hospital_distances_sum)
print(f"医院应该建在村庄 {chr(65 + hospital_location)} 处,使得最远村庄的人到医院看病所走的路最短。")
school_total_distances = np.zeros(6)
for i in range(6):
weighted_distances = 0
for j in range(6):
if distances[j, i] != np.inf:
weighted_distances += students[j] * distances[j, i]
school_total_distances[i] = weighted_distances
school_location = np.argmin(school_total_distances)
print(f"小学应该建在村庄 {chr(65 + school_location)} 处,使得所有学生上学走的总路程最短。")
print("学号:3025")