博客
关于我
【代码超详解】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中的 IFNULL 函数的详解
    查看>>
    mysql中的collate关键字是什么意思?
    查看>>
    MySql中的concat()相关函数
    查看>>
    mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
    查看>>
    MySQL中的count函数
    查看>>
    MySQL中的DB、DBMS、SQL
    查看>>
    MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
    查看>>
    MySQL中的GROUP_CONCAT()函数详解与实战应用
    查看>>
    MySQL中的IO问题分析与优化
    查看>>
    MySQL中的ON DUPLICATE KEY UPDATE详解与应用
    查看>>
    mysql中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
    查看>>
    mysql中的undo log、redo log 、binlog大致概要
    查看>>
    Mysql中的using
    查看>>
    MySQL中的关键字深入比较:UNION vs UNION ALL
    查看>>
    mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
    查看>>
    mysql中的字段如何选择合适的数据类型呢?
    查看>>
    MySQL中的字符集陷阱:为何避免使用UTF-8
    查看>>
    mysql中的数据导入与导出
    查看>>
    MySQL中的时间函数
    查看>>
    mysql中的约束
    查看>>