帮同学参加数学建模做的求点集间最短距离,时间复杂度300*300

要求:求每一个会员点分别到任务点集的最短距离

表格如下:

我的做法是:

1先把会员经度纬度保存为 X.txt ,把任务经度纬度保存为Y.txt(直接从表格复制)

源代码如下:

import math  
class Point:  
    def __init__(self,x=0,y=0):  
        self.x=x  
        self.y=y  

listx=[]
listy=[]


with open("X.txt", 'r') as f:  
    data = f.readlines()  
        
    for line in data:  
         odom = line.split()
         a=float(odom[0])
         b=float(odom[1])
         p=Point(a,b)
         listx.append(p)
with open("Y.txt", 'r') as f:  
    data = f.readlines()  
        
    for line in data:  
         odom = line.split()
         a=float(odom[0])
         b=float(odom[1])
         p=Point(a,b)
         listy.append(p)
                


class Line(Point):  
    def getLen(self,p1,p2):  
        r=math.sqrt(((p1.x-p2.x)**2)+(p1.y-p2.y)**2)  
        return r


l=Line()
listhaha=[]
b=open("shuchu.txt","a+") 
for i in listx:
    listl=[]
    for j in listy: 
        juli=l.getLen(i,j)
        listl.append(juli)
    listhaha.append(min(listl))
    print("点(",i.x,",",i.y,")到会员点集的最短距离为",min(listl))
    b.write("点(")
    b.write(str(i.x))
    b.write(",")
    b.write(str(i.y))
    b.write(")到会员点集的最短距离为")
    b.write(str(min(listl)))
    b.write("\n")

b.close()

  

 

posted on 2017-09-16 14:08  我是蒟蒻  阅读(454)  评论(0编辑  收藏  举报

导航