博客
关于我
【代码超详解】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 group by
    查看>>
    MySQL I 有福啦,窗口函数大大提高了取数的效率!
    查看>>
    mysql id自动增长 初始值 Mysql重置auto_increment初始值
    查看>>
    MySQL in 太多过慢的 3 种解决方案
    查看>>
    MySQL InnoDB 三大文件日志,看完秒懂
    查看>>
    Mysql InnoDB 数据更新导致锁表
    查看>>
    Mysql Innodb 锁机制
    查看>>
    MySQL InnoDB中意向锁的作用及原理探
    查看>>
    MySQL InnoDB事务隔离级别与锁机制深入解析
    查看>>
    Mysql InnoDB存储引擎 —— 数据页
    查看>>
    Mysql InnoDB存储引擎中的checkpoint技术
    查看>>
    Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
    查看>>
    MySQL InnoDB引擎的锁机制详解
    查看>>
    Mysql INNODB引擎行锁的3种算法 Record Lock Next-Key Lock Grap Lock
    查看>>
    mysql InnoDB数据存储引擎 的B+树索引原理
    查看>>
    mysql innodb通过使用mvcc来实现可重复读
    查看>>
    mysql insert update 同时执行_MySQL进阶三板斧(三)看清“触发器 (Trigger)”的真实面目...
    查看>>
    mysql interval显示条件值_MySQL INTERVAL关键字可以使用哪些不同的单位值?
    查看>>
    Mysql join原理
    查看>>
    MySQL Join算法与调优白皮书(二)
    查看>>