最短路是个老问题了,大神们留下很多文档但是很多都是针对算法使用一些固定大小的数组进行数据存储在实际应用中受到限制,这里自己练习一下,主要用了一些c++的stl,减少了固定长度数组的依赖,换一种写法试图提高可读性。有兴趣的同学可以试着将map/set 换成 hash_set/hash_map 应该能获得更高的效率,对于稀疏表,内存还是能省不少的。

参考文献:  http://www.cnblogs.com/hxsyl/p/3270401.html

  1 /************************************************************************/
  2 /*     尽量用简单快速好理解的方法完成图的最短路径                          */
  3 /************************************************************************/
  4 #include <stdio.h>
  5 #include <string.h>
  6 #include <malloc.h>
  7 
  8 #include <iostream>
  9 #include <sstream>
 10 #include <string>
 11 
 12 #include <set>
 13 #include <map>
 14 #include <vector>
 15 #include <queue>
 16 
 17 #include <algorithm>
 18 
 19 using namespace std;
 20 
 21 typedef size_t Index;
 22 
 23 // 使用整数比string快
 24 map<string, Index> g_mapNode;       // string -> index      
 25 map<Index, string> g_mapNodeIndex;  // index -> string     
 26 map<Index, set<Index> > g_mapLink;       // 邻接表
 27 
 28 inline bool IsNullString(const char* pcName)
 29 {
 30     return (NULL == pcName) && ('\0' == pcName[]);
 31 }
 32 
 33 inline bool IsNodeExist(const string& strName)
 34 {
 35     return g_mapNode.end() != g_mapNode.find(strName);
 36 }
 37 
 38 bool FindPath1(Index uSource, Index uTarget, vector<Index>& vecRlst)
 39 {
 40     map<Index, size_t> mapDistence;
 41     map<Index, Index>  mapPath;
 42 
 43     // inistalize
 44     map<Index, set<Index> >::iterator iterInit = g_mapLink.begin();
 45     for (; iterInit != g_mapLink.end(); ++iterInit)
 46     {
 47         if (iterInit->second.count(uTarget) != )
 48         {
 49             mapDistence[iterInit->first] = ;
 50             mapPath[iterInit->first] = uTarget;
 51         }
 52     }
 53 
 54     // find
 55     size_t uNodeNum = g_mapNode.size();
 56     for (size_t i = ; i < uNodeNum; ++i)
 57     {
 58         for (size_t j = ; j < uNodeNum; ++j)
 59         {
 60             if (g_mapLink.count(i) !=  && g_mapLink[i].count(j) != ) // i - > j 是通的
 61             {
 62                 if (mapDistence.count(j) !=   // j -> uTarget是通的
 63                     && (mapDistence.count(i) ==   // i -> uTarget 不存在或者 比从 j走远
 64                         || (mapDistence[j] +  < mapDistence[i])))
 65                 {
 66                     mapDistence[i] = mapDistence[j] + ; // 更新距离 
 67                     mapPath[i] = j;                      // 更新下一跳地址
 68                 }
 69             }
 70         }    
 71     }
 72 
 73     // 不可到达
 74     if (mapDistence.count(uSource) == )
 75     {
 76         return false;
 77     }
 78 
 79     while (uSource != uTarget)
 80     {
 81         vecRlst.push_back(uSource);
 82         uSource = mapPath[uSource];
 83     }
 84     vecRlst.push_back(uSource);
 85 
 86     return true;
 87 }
 88 
 89 bool FindPath2(Index uSource, Index uTarget, vector<Index>& vecRlst)
 90 {
 91     map<Index, Index>  mapLastJump;
 92     queue<Index> queNodeQue;
 93 
 94     bool bIsFind = false;
 95     queNodeQue.push(uSource);
 96     while (!queNodeQue.empty() && !bIsFind)
 97     {
 98         Index uIdx = queNodeQue.front();
 99         queNodeQue.pop(); 
         if (g_mapLink.count(uIdx) == )
         {
             continue;
         }
 
         set<Index>::iterator iter = g_mapLink[uIdx].begin();
         for (; iter != g_mapLink[uIdx].end(); iter++)
         {
             if (mapLastJump.count(*iter) != )
             {
                 continue;
             }
 
             mapLastJump[*iter] = uIdx;
             if (*iter == uTarget)
             {
                 bIsFind = true;
                 break;
             }
             queNodeQue.push(*iter);
         }
     }
     
     if (!bIsFind)
     {
         return false;
     }
 
     while(uTarget != uSource)
     {
         vecRlst.push_back(uTarget);
         uTarget = mapLastJump[uTarget];
     }
     vecRlst.push_back(uSource);
 
     reverse(vecRlst.begin(), vecRlst.end());
 
     return true;
 }
 
 void cmdInitialize()
 {
     g_mapNode.clear();
     g_mapLink.clear();
 }
 
 bool cmdAddNode(const char *pcNode)
 {
     if (IsNullString(pcNode))
     {
         return false;
     }
     
     string strName(pcNode);
     if (IsNodeExist(strName))
     {
         return false;
     }
 
     Index uIndex = g_mapNode.size();
     g_mapNode[strName] = uIndex;
     g_mapNodeIndex[uIndex] = strName;
 
     return true;
 }
 
 bool cmdAddLink(const char *pcSource, const char *pcTarget)
 {
     if (IsNullString(pcSource) || IsNullString(pcTarget))
     {
         return false;
     }
 
     string strSource(pcSource);
     string strTarget(pcTarget);
 
     if (!IsNodeExist(strSource) || !IsNodeExist(strTarget) || strSource == strTarget)
     {
         return false;
     }
 
     g_mapLink[g_mapNode[strSource]].insert(g_mapNode[strTarget]);
     g_mapLink[g_mapNode[strTarget]].insert(g_mapNode[strSource]);
 
     return true;
 }
 
 bool cmdFindPath(const char *pcSource, const char *pcTarget, char **ppcRlstPath)
 {
     if (NULL == ppcRlstPath)
     {
         return false;
     }
     *ppcRlstPath = NULL;
 
     string strSource(pcSource);
     string strTarget(pcTarget);
         
     if (!IsNodeExist(strSource) || !IsNodeExist(strTarget) || strSource == strTarget)
     {
         return false; 
     }
 
     vector<Index> vecPath;
     //if(!FindPath1(g_mapNode[strSource], g_mapNode[strTarget], vecPath))
     if(!FindPath2(g_mapNode[strSource], g_mapNode[strTarget], vecPath))
     {
         return false;
     }
 
     string strOutPut;
     stringstream ssOutPut("");
     for (size_t u = ; u < vecPath.size(); ++u)
     {
         ssOutPut << g_mapNodeIndex[vecPath[u]] << "->"; 
     }
     
     strOutPut = ssOutPut.str();
     strOutPut = strOutPut.substr(, strOutPut.length() - );
 
     *ppcRlstPath = (char *)malloc(sizeof(char) * (strOutPut.length() + ));
     if (NULL == *ppcRlstPath)
     {
         return false;
     }
 
     strcpy(*ppcRlstPath, strOutPut.c_str());
 
     return true;
 }
 
 int main()
 {
     cmdInitialize();
 
     cmdAddNode("A");
     cmdAddNode("B");
     cmdAddNode("C");
     cmdAddNode("D");
 
     cmdAddLink("A", "B");
     cmdAddLink("B", "C");
     cmdAddLink("C", "D");
     cmdAddLink("B", "D");
 
     char *pcPath = NULL;
     if (cmdFindPath("A", "D", &pcPath))
     {
         printf("%s\n", pcPath);
         free(pcPath);
         pcPath = NULL;
     }
     
     return ;

254 }

c++ 最短路两种算法的更多相关文章

  1. 最小生成树算法 prim kruskal两种算法实现 HDU-1863 畅通工程

    最小生成树 通俗解释:一个连通图,可将这个连通图删减任意条边,仍然保持连通图的状态并且所有边权值加起来的总和使其达到最小.这就是最小生成树 可以参考下图,便于理解 原来的图: 最小生成树(蓝色线): ...

  2. 图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS)

    参考网址:图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS) - 51CTO.COM 深度优先遍历(Depth First Search, 简称 DFS) 与广度优先遍历(Breath ...

  3. 「每日五分钟,玩转JVM」:两种算法

    前言 上篇文章,我们了解了GC 的相关概念,这篇文章我们通过两个算法来了解如何去确定堆中的对象实例哪些是我们需要去回收的垃圾对象. 引用计数算法 引用计数法的原理很简单,就是在对象中维护一个计数器,当 ...

  4. HDU 1160 排序或者通过最短路两种方法解决

    题目大意: 给定一堆点,具有x,y两个值 找到一组最多的序列,保证点由前到后,x严格上升,y严格下降,并把最大的数目和这一组根据点的编号输出来 这里用两种方法来求解: 1. 我们可以一开始就将数组根据 ...

  5. 求最大公约数(GCD)的两种算法

    之前一直只知道欧几里得辗转相除法,今天学习了一下另外一种.在处理大数时更优秀的算法--Stein 特此记载 1.欧几里得(Euclid)算法 又称辗转相除法,依据定理gcd(a,b)=gcd(b,a% ...

  6. 求逆序对常用的两种算法 ----归并排 & 树状数组

    网上看了一些归并排求逆序对的文章,又看了一些树状数组的,觉得自己也写一篇试试看吧,然后本文大体也就讲个思路(没有例题),但是还是会有个程序框架的 好了下面是正文 归并排求逆序对 树状数组求逆序对 一. ...

  7. 一句话比较两种算法的优越性[蓝桥杯,LeetCode]

    动态规划 166 数学 155 字符串 146 树 120 哈希表 119 深度优先搜索 109 二分查找 79 贪心算法 64 双指针 59 广度优先搜索 54 栈 53 回溯算法 49 设计 41 ...

  8. 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。

    利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...

  9. Leetcode94. Binary Tree Inorder Traversal二叉树的中序遍历(两种算法)

    给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class So ...

随机推荐

  1. LINUX系统镜像下载总汇

    LINUX系统fedora.centos.debian.ubuntu ISO下载汇总 http://mirrors.sohu.com http://linux.ilvgo.cn/ ftp://gent ...

  2. 密钥,密钥对,公钥,pfx,jks和https的几个概念

    密钥: 我理解是公钥+私钥的统称. 密钥对: 公钥(证书)和私钥成对存在. 通信双方各持有自己的私钥和对方的公钥.自己的私钥需密切保护,而公钥是公开给对方的.在windows下,单独存在的公钥一般是后 ...

  3. schedule

    cocos2d-x(时间调度) 在游戏中,时常需要隔一段时间更新一些数据或者是人物位置,Cocos2D-x 中提供了这些时间调度的函数,所有CCNode 类的子类都有这样的函数. schedule(s ...

  4. Linux Ubuntu上架设FTP

    操作系统:ubuntu (GNU/Linux) 为了在机子上架设ftp服务器,我们需要安装ftp服务器软件.Linux下具有代表性的ftp服务器软件有Wu-FTP,ProFTP和Vsftp.Wu-FT ...

  5. redis持久化(摘录)

    redis是一个支持持久化的内存数据库,也就是说redis需要经常将内存中的数据同步到磁盘来保证持久化.redis支持两种持久化方式,一种是 Snapshotting(快照)也是默认方式,另一种是Ap ...

  6. mvn export runnable jar

    mvn dependency:copy-dependencies <build> <plugins> <plugin> <groupId>org.apa ...

  7. Linux学习新篇——常用命令和快捷键总结

    最近刚接触Linux,整理了一些常用的命令和快捷键 Tab补全命令 当命令记不清了,输入记得的前几个用Tab就可以将该命令自动补全. 启动tomcat服务用$startup.sh 停止tomcat服务 ...

  8. Asp.Net MVC是否针对每次请求都重新创建一个控制器实例

    一.Asp.Net MVC是否针对每次请求都重新创建一个控制器实例 默认情况下,答案是确定的. ControllerBuilder类 ControllerBuilder.Current用户获取默认的控 ...

  9. 20160327javaweb 之JSP入门

    一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写h ...

  10. 20160322 javaweb 学习笔记--response 重定向

    //一般方法 response.setStatus(302); response.setHeader("Location", "/20160314/index.jsp&q ...