首页 > 其他分享 >第六周总结

第六周总结

时间:2023-06-10 20:45:01浏览次数:40  
标签:总结 distance info graph current 第六周 station id

本次我基本完成了地铁查询系统的web端内容,但成品仍有很多瑕疵,只满足了最基本的内容功能,设计代码如下:

def get_station_info(station_name):

    sql = "SELECT * FROM stations WHERE name='%s'" % station_name

    result = execute_sql(sql)

    if len(result) == 0:

        return None

    else:

        return result[0]

 

 

import heapq

 

def dijkstra(graph, start, end):

    # 记录每个节点的距离和前驱节点

    distances = {start: 0}

    path = {}

 

    # 将起点加入队列

    heap = []

    heapq.heappush(heap, (0, start))

 

    # 遍历所有节点

    while heap:

        (current_distance, current_node) = heapq.heappop(heap)

 

        # 如果当前节点已经访问过,则跳过

        if current_node in path: continue

 

        # 将当前节点标记为已访问

        path[current_node] = current_distance

 

        # 如果到达终点,则返回最短路径长度

        if current_node == end:

            return current_distance

 

        # 遍历当前节点的邻居节点

        for neighbor, distance in graph[current_node].items():

            new_distance = current_distance + distance

 

            # 如果通过当前节点到邻居节点的距离更短,则更新距离和前驱节点

            if neighbor not in distances or new_distance < distances[neighbor]:

                distances[neighbor] = new_distance

                heapq.heappush(heap, (new_distance, neighbor))

 

    # 如果无法

达终点,则返回无穷大

    return float('inf')

 

def calculate_distance(from_station, to_station):

    from_station_info = get_station_info(from_station)

    to_station_info = get_station_info(to_station)

    

    if not from_station_info or not to_station_info:

        return None

 

    graph = build_graph()

    distance = dijkstra(graph, from_station_info["id"], to_station_info["id"])

    return distance

 

def build_graph():

    graph = {}

    sql = "SELECT * FROM graph"

    result = execute_sql(sql)

    for row in result:

        from_station_id, to_station_id, distance = row["from_station_id"], row["to_station_id"], row["distance"]

 

        # 构建起点到终点的连接关系和权重

        if from_station_id not in graph:

            graph[from_station_id] = {}

        graph[from_station_id][to_station_id] = distance

 

        # 构建终点到起点的连接关系和权重

        if to_station_id not in graph:

            graph[to_station_id] = {}

        graph[to_station_id][from_station_id] = distance

 

    return graph

标签:总结,distance,info,graph,current,第六周,station,id
From: https://www.cnblogs.com/srz123/p/17471914.html

相关文章

  • 第四周总结
    本周我继续进行个人作业,这次我写出了要求的1/3的内容,具体思路如下: 用户身份注册与登录 用户登录:deflogin(username,password):  #判断用户名和密码是否正确  ifcheck_username(username)andcheck_password(password):    #登录成功,返回用户信息......
  • 第三周总结
    本周老师向我们布置了个人作业,在一周内我完成了登录注册的功能,代码如下:<EditText   android:id="@+id/username_edittext"   android:layout_width="match_parent"   android:layout_height="wrap_content"   android:hint="用户名"   android:inp......
  • 常用调度算法 总结
    常用调度算法总结 常用调度算法总结 1常见的批处理作业调度算法 1.1先来先服务调度算法(FCFS): 就是按照各个作业进入系统的自然次序来调度作业。这种调度算法的优点是实现简单,公平。其缺点是没有考虑到系统中各种资源的综合使用情况,往往使短作业的用户不满......
  • 2023.6.10集训总结
    2023.6.10集训总结在5月中旬到现在,我们经历了几周的停课集训,期间我还前往NJU参加学科营活动,感受到自己与全国大佬的差距时,也学到了一些大赛策略和经验。现对停课期间的收获与反思进行总结。讲课这几天之内,Meatherm、yny和tqx分别来讲了2、2、4天的课。讲课主要以做例题为主,图论......
  • T5L使用总结1
    迪文屏GUI控制,T5L类型,实用接口汇总。一、代码控制1.1RTC时间获取接口地址:0x0010,4个字空间,读取即可获取年月日。D7~D0,  D7年,D6月,D5日,D4星期,D3小时,D2分,D1秒。示例代码:u16buf[4];sys_read_vp(0x0010,(u8*)buf,4);解析: buf......
  • 今日总结3.13
    今天我们进行了地铁查询系统的大体设计数据库设计:建了一个表,表中数据有线路号、车站id(起始车站记为0,之后按线路顺序逐个递增)、站名。思路:线路查询和站点查询:简单的数据库查询,只需遍历一遍数据即可。起点终点查询:我们想的是将一个 表遍历两遍,查出起始线路的换乘站和终点站的......
  • 前端Node环境下模块的导入与导出总结
    //1、一个模块可以有多个下方这种非默认导出//2、外部模块要想单独使用a,b其中之一时,就必须要用解构符{}的方式去导入//3、外部导入方式1:import{aasAAA,basBBB}from"./utils";此处的AAA与BBB都是在外部模块导入的时候起的别名,可以是任意有效的标识符//4、外......
  • 5.26日学习总结之网络编程socket
    Pythonsocket编程在网络上的教程较少,菜鸟中也只是给出了一小段描述,在此我推荐白羽黑夜socket编程|白月黑羽(byhy.net)其中的描写非常详细,本人也是在此学习的。socket又被称作套接字,可以简单的看作是两个地址(ip,port地址)之间通过socket的函数来进行传递数据。socket在客户端......
  • 4.30学习总结之初见tkinter
     Tkinter是Python的标准GUI库。Python使用Tkinter可以快速的创建GUI应用程序。由于Tkinter是内置到python的安装包中、只要安装好Python之后就能importTkinter库,对简单图形界面的实现十分简单。在引入"importtkinter"后即可使用,以下两行即可运行出窗口,l......
  • 6.7日学习总结之iframe
    官方解释<iframe>标签是规定一个内联框架。一个内联框架被用来在当前HTML文档中嵌入另一个文档。简单来说就是在web页面中可以插入一个页面框来展示其它jsp/html等页面的内容,生成一个子页面。通常定义为:<iframesrc="iframename.jsp"name="myframe"></iframe>的形式,其......