利用无权图的单源最短路算法实现地铁换乘图

//Metro.php
$MetroVertex = array(
    1 => '体育中心',
    2 => '体育西路',
    3 => '杨箕',
    4 => '东山口',
    5 => '烈士陵园',
    6 => '农讲所',
    7 => '公园前',
    8 => '西门口',
    9 => '陈家祠',
    10 => '长寿路',
    11 => '黄沙',
    12 => '芳村',
    13 => '花地湾',
    14 => '坑口',
    15 => '西朗',
    16 => '嘉禾望岗',
    17 => '黄边',
    18 => '江夏',
    19 => '萧岗',
    20 => '白云文化广场',
    21 => '白云公园',
    22 => '飞翔公园',
    23 => '三元里',
    24 => '广州火车站',
    25 => '越秀公园',
    26 => '纪念堂',
    27 => '海珠广场',
    28 => '市二宫',
    29 => '江南西',
    30 => '昌岗',
    31 => '江泰路',
    32 => '东晓南',
    33 => '南州',
    34 => '洛溪',
    35 => '南浦',
    36 => '会江',
    37 => '石壁',
    38 => '广州南站',
    39 => '天河客运站',
    40 => '五山',
    41 => '华师',
    42 => '岗顶',
    43 => '石牌桥',
    44 => '珠江新城',
    45 => '广州塔',
    46 => '客村',
    47 => '大塘',
    48 => '沥滘',
    49 => '厦滘',
    50 => '大石',
    51 => '汉溪长隆',
    52 => '市桥',
    53 => '番禺广场',
    54 => '机场南',
    55 => '人和',
    56 => '龙归',
    57 => '白云大道北',
    58 => '永泰',
    59 => '同和',
    60 => '京溪南方医院',
    61 => '梅花园',
    62 => '燕塘',
    63 => '广州东',
    64 => '林和西',
    65 => '黄村',
    66 => '车陂',
    67 => '车陂南',
    68 => '万胜围',
    69 => '官洲',
    70 => '大学城北',
    71 => '大学城南',
    72 => '新造',
    73 => '石碁',
    74 => '海傍',
    75 => '低涌',
    76 => '东涌',
    77 => '黄阁汽车城',
    78 => '黄阁',
    79 => '蕉门',
    80 => '金州',
    81 => '滘口',
    82 => '坦尾',
    83 => '中山八',
    84 => '西场',
    85 => '西村',
    86 => '小北',
    87 => '淘金',
    88 => '区庄',
    89 => '动物园',
    90 => '五羊邨',
    91 => '猎德',
    92 => '潭村',
    93 => '员村',
    94 => '科韵路',
    95 => '东圃',
    96 => '三溪',
    97 => '鱼珠',
    98 => '大沙地',
    99 => '大沙东',
    100 => '文冲',
    101 => '浔峰岗',
    102 => '横沙',
    103 => '沙贝',
    104 => '河沙',
    106 => '如意坊',
    108 => '文化公园',
    109 => '一德路',
    111 => '北京路',
    112 => '团一大广场',
    113 => '东湖',
    115 => '黄花岗',
    116 => '沙河顶',
    117 => '沙河',
    118 => '天平架',
    119 => '长湴',
    120 => '凤凰新村',
    121 => '沙园',
    122 => '宝岗大道',
    123 => '晓港',
    124 => '中大',
    125 => '鹭江',
    126 => '赤岗',
    127 => '磨碟沙',
    128 => '新港东',
    129 => '琶洲',
    130 => '体育中心南',
    131 => '天河南',
    132 => '黄埔大道',
    133 => '妇儿中心',
    134 => '花城大道',
    135 => '大剧院',
    136 => '海心沙'
);

$Line = array();
$Line['63']['1'] = 1;
$Line['63']['62'] = 1;
$Line['63']['64'] = 1;
$Line['1']['63'] = 1;
$Line['1']['2'] = 1;
$Line['2']['1'] = 1;
$Line['2']['43'] = 1;
$Line['2']['3'] = 1;
$Line['2']['44'] = 1;
$Line['3']['2'] = 1;
$Line['3']['2'] = 1;
$Line['3']['89'] = 1;
$Line['3']['4'] = 1;
$Line['4']['3'] = 1;
$Line['4']['5'] = 1;
$Line['4']['113'] = 1;
$Line['4']['88'] = 1;
$Line['5']['4'] = 1;
$Line['5']['6'] = 1;
$Line['6']['5'] = 1;
$Line['6']['7'] = 1;
$Line['7']['8'] = 1;
$Line['7']['6'] = 1;
$Line['7']['26'] = 1;
$Line['7']['27'] = 1;
$Line['8']['7'] = 1;
$Line['8']['9'] = 1;
$Line['9']['8'] = 1;
$Line['9']['10'] = 1;
$Line['10']['9'] = 1;
$Line['10']['11'] = 1;
$Line['11']['108'] = 1;
$Line['11']['106'] = 1;
$Line['11']['12'] = 1;
$Line['12']['11'] = 1;
$Line['12']['13'] = 1;
$Line['13']['12'] = 1;
$Line['13']['14'] = 1;
$Line['14']['13'] = 1;
$Line['14']['15'] = 1;
$Line['16']['56'] = 1;
$Line['16']['57'] = 1;
$Line['16']['17'] = 1;
$Line['17']['16'] = 1;
$Line['17']['18'] = 1;
$Line['18']['17'] = 1;
$Line['18']['19'] = 1;
$Line['19']['18'] = 1;
$Line['19']['20'] = 1;
$Line['20']['19'] = 1;
$Line['20']['21'] = 1;
$Line['21']['20'] = 1;
$Line['21']['22'] = 1;
$Line['22']['21'] = 1;
$Line['22']['23'] = 1;
$Line['23']['22'] = 1;
$Line['23']['24'] = 1;
$Line['24']['23'] = 1;
$Line['24']['25'] = 1;
$Line['24']['85'] = 1;
$Line['24']['86'] = 1;
$Line['25']['24'] = 1;
$Line['25']['26'] = 1;
$Line['26']['25'] = 1;
$Line['26']['27'] = 1;
$Line['27']['26'] = 1;
$Line['27']['28'] = 1;
$Line['27']['109'] = 1;
$Line['27']['111'] = 1;
$Line['28']['27'] = 1;
$Line['28']['29'] = 1;
$Line['29']['28'] = 1;
$Line['29']['30'] = 1;
$Line['30']['29'] = 1;
$Line['30']['31'] = 1;
$Line['30']['122'] = 1;
$Line['30']['123'] = 1;
$Line['31']['30'] = 1;
$Line['31']['32'] = 1;
$Line['32']['31'] = 1;
$Line['32']['33'] = 1;
$Line['33']['32'] = 1;
$Line['33']['34'] = 1;
$Line['34']['35'] = 1;
$Line['34']['33'] = 1;
$Line['35']['34'] = 1;
$Line['35']['36'] = 1;
$Line['36']['35'] = 1;
$Line['36']['37'] = 1;
$Line['37']['36'] = 1;
$Line['37']['38'] = 1;
$Line['39']['119'] = 1;
$Line['39']['62'] = 1;
$Line['39']['40'] = 1;
$Line['40']['39'] = 1;
$Line['40']['41'] = 1;
$Line['41']['40'] = 1;
$Line['41']['42'] = 1;
$Line['42']['41'] = 1;
$Line['42']['43'] = 1;
$Line['43']['42'] = 1;
$Line['43']['2'] = 1;
$Line['44']['2'] = 1;
$Line['44']['45'] = 1;
$Line['44']['45'] = 1;
$Line['44']['90'] = 1;
$Line['44']['91'] = 1;
$Line['45']['44'] = 1;
$Line['45']['46'] = 1;
$Line['45']['136'] = 1;
$Line['46']['45'] = 1;
$Line['46']['47'] = 1;
$Line['46']['125'] = 1;
$Line['46']['126'] = 1;
$Line['47']['46'] = 1;
$Line['47']['48'] = 1;
$Line['48']['47'] = 1;
$Line['48']['49'] = 1;
$Line['49']['48'] = 1;
$Line['49']['50'] = 1;
$Line['50']['49'] = 1;
$Line['50']['51'] = 1;
$Line['51']['50'] = 1;
$Line['51']['52'] = 1;
$Line['52']['51'] = 1;
$Line['52']['53'] = 1;
$Line['53']['52'] = 1;
$Line['54']['55'] = 1;
$Line['55']['54'] = 1;
$Line['55']['56'] = 1;
$Line['56']['16'] = 1;
$Line['57']['16'] = 1;
$Line['57']['58'] = 1;
$Line['58']['57'] = 1;
$Line['58']['59'] = 1;
$Line['59']['58'] = 1;
$Line['59']['60'] = 1;
$Line['60']['59'] = 1;
$Line['60']['61'] = 1;
$Line['61']['60'] = 1;
$Line['61']['62'] = 1;
$Line['62']['61'] = 1;
$Line['62']['63'] = 1;
$Line['62']['118'] = 1;
$Line['62']['39'] = 1;
$Line['64']['63'] = 1;
$Line['64']['130'] = 1;
$Line['64']['2'] = 1;
$Line['65']['66'] = 1;
$Line['66']['65'] = 1;
$Line['66']['67'] = 1;
$Line['67']['66'] = 1;
$Line['67']['68'] = 1;
$Line['67']['94'] = 1;
$Line['67']['95'] = 1;
$Line['68']['67'] = 1;
$Line['68']['69'] = 1;
$Line['68']['129'] = 1;
$Line['69']['68'] = 1;
$Line['69']['70'] = 1;
$Line['70']['69'] = 1;
$Line['70']['71'] = 1;
$Line['71']['70'] = 1;
$Line['71']['72'] = 1;
$Line['72']['71'] = 1;
$Line['72']['73'] = 1;
$Line['73']['72'] = 1;
$Line['73']['74'] = 1;
$Line['74']['73'] = 1;
$Line['74']['75'] = 1;
$Line['75']['74'] = 1;
$Line['75']['76'] = 1;
$Line['76']['75'] = 1;
$Line['76']['77'] = 1;
$Line['76']['77'] = 1;
$Line['77']['76'] = 1;
$Line['77']['78'] = 1;
$Line['78']['77'] = 1;
$Line['78']['79'] = 1;
$Line['79']['78'] = 1;
$Line['79']['80'] = 1;
$Line['80']['79'] = 1;
$Line['81']['82'] = 1;
$Line['82']['81'] = 1;
$Line['82']['83'] = 1;
$Line['82']['104'] = 1;
$Line['82']['106'] = 1;
$Line['83']['82'] = 1;
$Line['83']['84'] = 1;
$Line['84']['83'] = 1;
$Line['84']['85'] = 1;
$Line['85']['84'] = 1;
$Line['85']['24'] = 1;
$Line['86']['24'] = 1;
$Line['86']['87'] = 1;
$Line['87']['86'] = 1;
$Line['87']['88'] = 1;
$Line['88']['87'] = 1;
$Line['88']['89'] = 1;
$Line['88']['4'] = 1;
$Line['88']['115'] = 1;
$Line['89']['88'] = 1;
$Line['89']['3'] = 1;
$Line['90']['3'] = 1;
$Line['90']['44'] = 1;
$Line['91']['44'] = 1;
$Line['91']['92'] = 1;
$Line['92']['91'] = 1;
$Line['92']['93'] = 1;
$Line['93']['92'] = 1;
$Line['93']['94'] = 1;
$Line['94']['93'] = 1;
$Line['94']['67'] = 1;
$Line['95']['67'] = 1;
$Line['95']['96'] = 1;
$Line['96']['95'] = 1;
$Line['96']['97'] = 1;
$Line['97']['96'] = 1;
$Line['97']['98'] = 1;
$Line['98']['97'] = 1;
$Line['98']['99'] = 1;
$Line['99']['98'] = 1;
$Line['99']['100'] = 1;
$Line['100']['99'] = 1;
$Line['101']['102'] = 1;
$Line['102']['101'] = 1;
$Line['102']['103'] = 1;
$Line['103']['102'] = 1;
$Line['103']['104'] = 1;
$Line['104']['103'] = 1;
$Line['104']['82'] = 1;
$Line['106']['82'] = 1;
$Line['106']['11'] = 1;
$Line['108']['11'] = 1;
$Line['108']['109'] = 1;
$Line['109']['108'] = 1;
$Line['109']['27'] = 1;
$Line['111']['27'] = 1;
$Line['111']['112'] = 1;
$Line['112']['111'] = 1;
$Line['112']['113'] = 1;
$Line['113']['4'] = 1;
$Line['115']['88'] = 1;
$Line['115']['116'] = 1;
$Line['116']['115'] = 1;
$Line['116']['117'] = 1;
$Line['117']['116'] = 1;
$Line['117']['118'] = 1;
$Line['118']['117'] = 1;
$Line['118']['62'] = 1;
$Line['119']['39'] = 1;
$Line['120']['121'] = 1;
$Line['121']['120'] = 1;
$Line['121']['122'] = 1;
$Line['122']['121'] = 1;
$Line['122']['30'] = 1;
$Line['123']['30'] = 1;
$Line['123']['124'] = 1;
$Line['124']['123'] = 1;
$Line['124']['125'] = 1;
$Line['125']['124'] = 1;
$Line['125']['46'] = 1;
$Line['126']['46'] = 1;
$Line['126']['127'] = 1;
$Line['127']['126'] = 1;
$Line['127']['128'] = 1;
$Line['128']['127'] = 1;
$Line['128']['129'] = 1;
$Line['129']['128'] = 1;
$Line['129']['68'] = 1;
$Line['130']['64'] = 1;
$Line['130']['131'] = 1;
$Line['131']['130'] = 1;
$Line['131']['132'] = 1;
$Line['132']['131'] = 1;
$Line['132']['133'] = 1;
$Line['133']['132'] = 1;
$Line['133']['134'] = 1;
$Line['134']['133'] = 1;
$Line['134']['135'] = 1;
$Line['135']['134'] = 1;
$Line['135']['136'] = 1;
$Line['136']['135'] = 1;
$Line['136']['45'] = 1;
<?php
require_once(dirname(__FILE__) . '\Metro.php');
//print_r($Line);
//初始化$dist,$path数组
foreach($MetroVertex as $k=>$v){
    $dist[$k] = -1;
    $path[$k] = -1;
}
function Unweighted($start,$end,$dist,$path,$Line){
    $Queue = array();
    $Queue[] = $start;
    $dist[$start] = 0;

    while(!empty($Queue)){
        $v = array_shift($Queue);
        $temp = $Line[(string)$v];
        foreach($temp as $k=>$value){
            if($dist[(int)$k] == -1){
                $dist[(int)$k] = $dist[$v] +1;
                $path[(int)$k] = $v;
                $Queue[] = $k;
            }
            if((int)$k == $end){
                $find_arr = array();
                $find = $end;
                $find_arr[] = $find;
                while($find != $start){
                    $find = $path[$find];
                    array_unshift($find_arr,$find);
                }
                return $find_arr;
                exit;
            }
        }
    }
}
$find_arr = Unweighted(50,87,$dist,$path,$Line);
foreach($find_arr as $k=>$v){
    $find_arr[$k] = $MetroVertex[$v];
}
echo implode('->',$find_arr);

//伪代码,无权图的单源最短路算法

    /* 邻接表存储 - 无权图的单源最短路算法 */

    /* dist[]和path[]全部初始化为-1 */
    void Unweighted ( LGraph Graph, int dist[], int path[], Vertex S )
    {
        Queue Q;
        Vertex V;
        PtrToAdjVNode W;

        Q = CreateQueue( Graph->Nv ); /* 创建空队列, MaxSize为外部定义的常数 */
        dist[S] = 0; /* 初始化源点 */
        AddQ (Q, S);

        while( !IsEmpty(Q) ){
            V = DeleteQ(Q);
            for ( W=Graph->G[V].FirstEdge; W; W=W->Next ) /* 对V的每个邻接点W->AdjV */
                if ( dist[W->AdjV]==-1 ) { /* 若W->AdjV未被访问过 */
                    dist[W->AdjV] = dist[V]+1; /* W->AdjV到S的距离更新 */
                    path[W->AdjV] = V; /* 将V记录在S到W->AdjV的路径上 */
                    AddQ(Q, W->AdjV);
                }
        } /* while结束*/
    }

时间: 2024-10-06 00:16:24

利用无权图的单源最短路算法实现地铁换乘图的相关文章

Dijkstra(单源最短路算法)

typedef struct graph{ int val; int weight;}graph; graph g[1005][1005],dist[1005];int visit[1005]; void dijkstra(int start,int n) { int min,u; for(int i=1;i<=n;i++) { dist[i].val = MAXVAL; visit[i]=0; } for(int i=1;i<=n;i++) { if(g[start][i].val <

Dijkstra算法 --- 单源最短路

Dijkstra算法适用于边权值为正的情况,可用于计算正权图上的单元最短路. 其伪代码如下: 设d[v0] = 0, 其他d[i] = INF 循环n次{ 在所有未标号的结点中,选取d值最小的结点x 给结点x加上永久标号 对于从x出发的所有边,执行松弛操作. } //松弛操作的伪代码如下: RELAX(u,v,w) if(u.d + w(u,v) < v.d){ v.d = w.d + w(u,v); pre[v] = u; } Dijkstra算法代码: /* Dijkstra 单源最短路算法

利用分支限界法求解单源最短路(Dijkstra)问题

分支限界法定义:采用BFS算法,并使用剪枝函数的算法称为分支界限法. 分支限界法解释:按广度优先的原则,有选择的在其child中进行扩展,从而舍弃不含有最优解的分支,不断重复这一过程,直到找到答案或者判定无解. 分支界限法常常用到优先队列来选择最佳扩展节点,有时也会用到普通队列,以先进先出为原则来进行筛选. 单源最短路问题定义:给定有向图和起点,寻找到达所有点的最短路径. 单源最短路的分支限界法概述:首先把节点加入优先队列,之后不断地从队列中取出最优扩展点,观察其可抵达的所有目标节点,若当前路径

模板C++ 03图论算法 1最短路之单源最短路(SPFA)

3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过的边上的权和小于0, 就说这条路是一个负权回路. 回归正题,SPFA是bellman-ford的一种改进算法,由1994年西安交通大学段凡丁提出.它无法处理带有负环的图,判断方法:如果某个点进入队列的次数超过N次则存在负环. SPFA的两种写法,bfs和dfs,bfs判别负环不稳定,相当于限深度搜索

UVA 658 It&#39;s not a Bug, it&#39;s a Feature! (单源最短路,dijkstra+优先队列,变形,经典)

题意:有n个bug,有m个补丁,每个补丁有一定的要求(比如某个bug必须存在,某个必须不存在,某些无所谓等等),打完出来后bug还可能变多了呢.但是打补丁是需要时间的,每个补丁耗时不同,那么问题来了:要打多久才能无bug?(同1补丁可重复打) 分析: n<=20,那么用位来表示bug的话有220=100万多一点.不用建图了,图实在太大了,用位图又不好玩.那么直接用隐式图搜索(在任意点,只要满足转移条件,任何状态都能转). 但是有没有可能每个状态都要搜1次啊?那可能是100万*100万啊,这样出题

再看最短路算法 1 —— 单源最短路

学了多年的算法,最短路问题相当之常见———— 好久没写过最短路的问题了,直到昨天闲的无聊来了一题——BZOJ3402(HansBug:额才发现我弱到只能刷水的地步了TT) 一看这不是明显的单源最短路么呵呵...于是直接上来来了个dijkstra,而且用的是邻接表存储图—— Submit之后,结果却是—— 我立刻被雷到了QAQ...于是立刻改写spfa,结果—— 4000ms+(估计还不止)和192ms究竟是怎样的差距啊QAQ,本人虽然早都听说过spfa的强大性,但是未曾想过差距会如此可怕,于是H

带负权图的单源最短路径算法:Bellman-Ford算法

算法简介 前面介绍过图的单源最短路径算法Dijkstra算法,然而Dijkstra算法无法判断含负权边的图的最短路.如果遇到负权,在没有负权回路存在时(负权回路的含义是,回路的权值和为负.)即便有负权的边,也可以采用Bellman-Ford算法正确求出最短路径. Bellman-Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数 w是 边集 E 的映射.对图G运行Bellman-Ford算法的结果是一个布尔值,表

用scheme语言实现SPFA算法(单源最短路)

最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学习了scheme并实现了SPFA算法,那天实现之后感觉很有成就感-在这里贴出来,以飨读者. 突然发现博客园不支持scheme语言,于是只能放弃高亮了.不得不说,scheme代码有没有高亮差别好大…… ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; 题目

图论-单源最短路-SPFA算法

有关概念: 最短路问题:若在图中的每一条边都有对应的权值,求从一点到另一点之间权值和最小的路径 SPFA算法的功能是求固定起点到图中其余各点的的最短路(单源最短路径) 约定:图中不存在负权环,用邻接表存储有向图,di存放从起点到结点i的最短路,q为队列,保存待处理节点 思路: 首先指定起点入队,取当前队头结点u,沿每一条与u相连的边向外扩展,对该边所指向的结点v松弛(比较当前dv与当前du加此边长,更新最短路值dv,以及最短路径prev)如果v不在队列中且更新了最短路值,v进队,直至队列中没有元