首页 > 其他分享 >6.5(学号:3025)

6.5(学号:3025)

时间:2024-10-27 21:36:34浏览次数:4  
标签:distances school 3025 6.5 connected np inf hospital

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")

标签:distances,school,3025,6.5,connected,np,inf,hospital
From: https://www.cnblogs.com/tjs200461/p/18509034

相关文章

  • 6.6(学号:3025)
    importnumpyasnpmatches=np.array([[0,1,0,1,1,1],#1队[0,0,0,1,1,1],#2队[1,1,0,1,0,0],#3队[0,0,0,0,1,1],#4队[0,0,1,0,0,1],#5队[0,0,1,0,0,0]#6队],dtype=int)n=matches.shape[0]closure=matches.copy......
  • 2.13(学号:3025)
    importnumpyasnpdeff(x):return(abs(x+1)-abs(x-1))/2+np.sin(x)defg(x):return(abs(x+3)-abs(x-3))/2+np.cos(x)假设我们有一些初始猜测值(这里只是随机选择的)x1_guess=0.5x2_guess=1.0y1_guess=0.2y2_guess=0.3定义方程组矩阵A和向......
  • 4.3(学号:3025)
    importmatplotlib.pyplotaspltimportnumpyasnpimportcvxpyascpx=cp.Variable(6,pos=True)obj=cp.Minimize(x[5])a1=np.array([0.025,0.015,0.055,0.026])a2=np.array([0.05,0.27,0.19,0.185,0.185])a3=np.array([1,1.01,1.02,1.045,1.065])k=0.05......
  • 4.4(学号:3025)
    MAX_A=15MAX_B=24MAX_DEBUG=5products=[{"name":"Ⅰ","A_hours":1,"B_hours":6,"debug_hours":1,"profit":2},#假设产品Ⅰ至少使用1小时设备A{"name":"Ⅱ","A_hours"......
  • 2.8(学号:3025)
    importnumpyasnp初始化系数矩阵A和常数项向量bn=1000A=np.zeros((n,n))b=np.arange(1,n+1)填充系数矩阵Aforiinrange(n):A[i,i]=4#对角线元素为4ifi<n-1:A[i,i+1]=1#每一行的下一个元素为1ifi>0:A[i,i-1]=1#每一行的上一个元素......
  • 2.9(学号:3025)
    importsympyassp定义变量x,y=sp.symbols('xy')定义方程组equation1=sp.Eq(x**2-y-x,3)equation2=sp.Eq(x+3*y,2)解方程组solutions=sp.solve((equation1,equation2),(x,y),dict=True)print("符号解:")forsolinsolutions:print(sol)......
  • 2.10(学号:3025)
    fromscipy.integrateimportquadimportnumpyasnp第一部分:抛物线旋转体(修正后)defV1_quad(y):returnnp.pi*(4*y-y**2)V1_corrected,_=quad(V1_quad,1,3)第二部分保持不变V2=0.5*(4/3)*np.pi*23-(1/3)*np.pi*22*1计算总体积total_volume_co......
  • 2.12(学号:3025)
    importnumpyasnpfromscipy.linalgimporteig定义矩阵A=np.array([[-1,1,0],[-4,3,0],[1,0,2]])计算特征值和特征向量eigenvalues,eigenvectors=eig(A)打印特征值print("特征值:")print(eigenvalues)打印特征向量print("特征向量:")foriinrange(ei......
  • 2.11(学号:3025)
    importnumpyasnpdeff(x):return(abs(x+1)-abs(x-1))/2+np.sin(x)defg(x):return(abs(x+3)-abs(x-3))/2+np.cos(x)fromscipy.optimizeimportfsolvedefequation_system(vars):x1,x2,y1,y2=varseq1=2x1-3f(y1)-4g(y2)+1eq2......
  • 2.4(学号:3025)
    importnumpyasnpimportmatplotlib.pyplotasplt定义x的范围x=np.linspace(-10,10,400)创建一个2行3列的子图布局fig,axs=plt.subplots(2,3,figsize=(12,8))遍历每个子图fork,axinenumerate(axs.flat,start=1):y=k*x**2+2*kax.plot(x,y,label......