博客
关于我
【代码超详解】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 数据库操作指南:学习如何使用 Python 进行增删改查操作
    查看>>
    MySQL 数据库的高可用性分析
    查看>>
    MySQL 数据库设计总结
    查看>>
    Mysql 数据库重置ID排序
    查看>>
    Mysql 数据类型一日期
    查看>>
    MySQL 数据类型和属性
    查看>>
    mysql 敲错命令 想取消怎么办?
    查看>>
    Mysql 整形列的字节与存储范围
    查看>>
    mysql 断电数据损坏,无法启动
    查看>>
    MySQL 日期时间类型的选择
    查看>>
    Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
    查看>>
    MySQL 是如何加锁的?
    查看>>
    MySQL 是怎样运行的 - InnoDB数据页结构
    查看>>
    mysql 更新子表_mysql 在update中实现子查询的方式
    查看>>
    MySQL 有什么优点?
    查看>>
    mysql 权限整理记录
    查看>>
    mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
    查看>>
    MYSQL 查看最大连接数和修改最大连接数
    查看>>
    MySQL 查看有哪些表
    查看>>
    mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
    查看>>