博客
关于我
【代码超详解】POJ 2502 / ZOJ 1891 Subway(Dijkstra 算法 · 堆优化求最短路)
阅读量:709 次
发布时间:2019-03-21

本文共 2790 字,大约阅读时间需要 9 分钟。

###Algorithm Analysis and Code Writing Guide

This problem can be effectively solved using the Dijkstra algorithm modified for road networks where time is the edge weight. The differences in travel speeds between metro lines and walking between stations need to be carefully handled. Additionally, the graph needs to be constructed correctly to reflect both road and metro connections.

####Graph Construction

  • cities and stations:Use a map to assign unique IDs to each city and station. This helps in managing the dynamic addition of stations during input parsing.

  • Railway Lines:For each metro line defined by its start and end stations, add bidirectional edges between consecutive stations. The edges should be weighted based on the distance between the stations and the metro speed (40 km/h).

  • Walking Connections:After the metro lines are added, connect each station to all other stations (including other metro stations and the start/end points) with walking edges. Walking speed is 10 km/h.

  • Edge Weights

    • Metro Edges: Time is calculated as distance / 40 (converted to hours).
    • Walking Edges: Time is calculated as distance / 10 (converted to hours).
  • ####Implementation Steps

  • Graph Initialization

    • Use a vector of edge structures to store the graph. Each edge contains the destination node and the travel time.
    • Use a bitset to track visited nodes and a priority queue for Dijkstra's algorithm.
  • Input Parsing

    • Parse the home and school coordinates.
    • Read each metro line's stations and add edges for each consecutive pair.
    • Add walking edges between all pairs of nodes.
  • Dijkstra's Algorithm

    • Initialize the travel times from home to all other nodes as infinity.
    • Use a priority queue to always expand the node with the smallest current travel time.
    • For each node, update the travel times of its neighbors and push them into the priority queue if a shorter path is found.
  • Output

    • The time taken to reach the school node multiplied by 60 gives the total travel time in minutes.
  • ####Code Structure

    The provided code implements the above steps with optimizations for both POJ and ZOJ platforms. Key features include:

    • Graph Construction:Efficiently adds metro and walking edges using vectors and maps.
    • Priority Queue: Uses a min-heap to always process the shortest known path.
    • Time Calculation: Converts distances to travel times based on the given speeds.

    ####Notes

    • Edge Cases: Handle scenarios where the home or school coordinates are not provided.
    • Graph Connectivity: Ensure all necessary stations are included and edges are properly bidirectional.
    • Precision: Use double precision to avoid floating-point errors in time calculations.

    The algorithm efficiently handles dynamic graphs and ensures that the shortest path is found by leveraging the priority queue and Dijkstra's algorithm properties.

    转载地址:http://oqtez.baihongyu.com/

    你可能感兴趣的文章
    MySQL 备份 Xtrabackup
    查看>>
    mysql 多个表关联查询查询时间长的问题
    查看>>
    mySQL 多个表求多个count
    查看>>
    mysql 多字段删除重复数据,保留最小id数据
    查看>>
    MySQL 多表联合查询:UNION 和 JOIN 分析
    查看>>
    MySQL 大数据量快速插入方法和语句优化
    查看>>
    mysql 如何给SQL添加索引
    查看>>
    mysql 字段区分大小写
    查看>>
    mysql 字段合并问题(group_concat)
    查看>>
    mysql 字段类型类型
    查看>>
    MySQL 字符串截取函数,字段截取,字符串截取
    查看>>
    MySQL 存储引擎
    查看>>
    mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
    查看>>
    MySQL 存储过程参数:in、out、inout
    查看>>
    mysql 存储过程每隔一段时间执行一次
    查看>>
    mysql 存在update不存在insert
    查看>>
    Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
    查看>>
    Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
    查看>>
    Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
    查看>>
    Mysql 学习总结(89)—— Mysql 库表容量统计
    查看>>