博客
关于我
【代码超详解】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 常见的 9 种优化方法
    查看>>
    MySQL 常见的开放性问题
    查看>>
    Mysql 常见错误
    查看>>
    MYSQL 幻读(Phantom Problem)不可重复读
    查看>>
    mysql 往字段后面加字符串
    查看>>
    mysql 快速自增假数据, 新增假数据,mysql自增假数据
    查看>>
    Mysql 报错 Field 'id' doesn't have a default value
    查看>>
    MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
    查看>>
    Mysql 拼接多个字段作为查询条件查询方法
    查看>>
    mysql 排序id_mysql如何按特定id排序
    查看>>
    Mysql 提示:Communication link failure
    查看>>
    mysql 插入是否成功_PDO mysql:如何知道插入是否成功
    查看>>
    Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
    查看>>
    mysql 数据库备份及ibdata1的瘦身
    查看>>
    MySQL 数据库备份种类以及常用备份工具汇总
    查看>>
    mysql 数据库存储引擎怎么选择?快来看看性能测试吧
    查看>>
    MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
    查看>>
    MySQL 数据库的高可用性分析
    查看>>
    MySQL 数据库设计总结
    查看>>
    Mysql 数据库重置ID排序
    查看>>