需求是只需要得到两点间的最短路,不需要求得单源对于全图的最短路,使用boost中的dijsktra_shortest_path,当得到目标点的最短路时直接throw exception。

 #include <boost/config.hpp>
#include <iostream> #include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp> using namespace boost; // define visitor for discover_vertex event template <class Vertex, class Tag>
struct target_visitor : public default_dijkstra_visitor
{
target_visitor(Vertex u) : v(u) { } template <class Graph>
void examine_vertex(Vertex u, Graph& g)
{
if( u == v ) {
throw(-);
}
}
private:
Vertex v;
}; template <class Vertex, class Tag>
target_visitor<Vertex, Tag>
target_visit(Vertex u, Tag) {
return target_visitor<Vertex, Tag>(u);
} int main(int argc, char** argv)
{
typedef adjacency_list < listS, vecS, directedS,
no_property, property < edge_weight_t, int > > graph_t;
typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor;
typedef graph_traits < graph_t >::edge_descriptor edge_descriptor;
typedef std::pair<int, int> Edge; const int num_nodes = ;
enum nodes { A, B, C, D, E };
char name[] = "ABCDE";
Edge edge_array[] =
{
Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B)
};
int weights[] = { , , , , , , , , };
int num_arcs = sizeof(edge_array) / sizeof(Edge); graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g); std::vector<vertex_descriptor> p(num_vertices(g));
std::vector<int> d(num_vertices(g)); // choose source and target
vertex_descriptor src = vertex(A, g);
vertex_descriptor targ = vertex(C, g); try {
dijkstra_shortest_paths(g, src,
predecessor_map(&p[]).distance_map(&d[]).visitor(target_visit(targ,
on_examine_vertex())));
}
catch( ... ) {
} std::cout << "distances and parents:" << std::endl;
graph_traits < graph_t >::vertex_iterator vi, vend;
for (tie(vi, vend) = vertices(g); vi != vend; ++vi) {
std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::endl;
} system("PAUSE");
return EXIT_SUCCESS;
}

boost dijkstra获得两点间的最短路的更多相关文章

  1. [C++]boost dijkstra获得两点间的最短路

    需求是只需要得到两点间的最短路,不需要求得单源对于全图的最短路,使用boost中的dijsktra_shortest_path,当得到目标点的最短路时直接throw exception. #inclu ...

  2. [CF1051F]The Shortest Statement (LCA+最短路)(给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路)

    题目:给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路 n≤100000,m≤100000,m-n≤20. 首先看到m-n≤20这条限制,我们可以想到是围绕这个20来做这道题. 即如果我们 ...

  3. AOJ -0189 Convenient Location && poj 2139 Six Degrees of Cowvin Bacon (floyed求任意两点间的最短路)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=78207 看懂题就好. 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...

  4. HDU6166-求集合间的最短路

    Senior Pan Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  5. AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C All Pairs Shortest Path Input ...

  6. hdu 2544 最短路(两点间最短路径)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2544 方法一:dijkstra算法,求两点之间最短路径. /*********************** ...

  7. bzoj 2125 最短路——仙人掌两点间最短路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2125 因为看了TJ又抄了标程,现在感觉还是轻飘飘的……必须再做一遍. 两点间的情况: 1.直 ...

  8. Dijkstra,floyd,spfa三种最短路的区别和使用

    这里不列举三种算法的实现细节,只是简单描述下思想,分析下异同 一 Dijkstra Dijkstra算法可以解决无负权图的最短路径问题,只能应付单源起点的情况,算法要求两个集合,开始所有点在第二个集合 ...

  9. 使用PostGIS完成两点间的河流轨迹及流经长度的计算

    基础准备工作 1.PostGIS 的安装 在安装PostGIS前首先必须安装PostgreSQL,然后再安装好的Stack Builder中选择安装PostGIS组件.具体安装步骤可参照 PostGI ...

随机推荐

  1. C# 文件创建时间,修改时间

    System.IO.FileInfo fi = new System.IO.FileInfo(@"D:\site\EKECMS\skin\Grey\default#.html"); ...

  2. C51系列RAM寄存器表

    特殊功能寄存器地址表 SFR 符号 字节 地址 位地址和位名称 D7 D6 D5 D4 D3 D2 D1 D0 P0口 P0 80H P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0. ...

  3. 对libssh的golang 封装 ssh2go

    ssh2go 是对libssh的go语言绑定.libssh作为ssh的实现库,支持SSHv2和SSHv1的客户.服务端实现,通过libssh你可以远程执行程序.拷贝文件等等. ssh2go不像其他go ...

  4. linux grep shell相关

    http://blog.csdn.net/buutterfly/article/details/6615162 http://www.2cto.com/os/201307/224496.html ht ...

  5. Havel定理

    先贴一个百度百科的注释 Havel定理编辑 本词条缺少概述.名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 中文名 Havel定理 外文名 Canisters theorem 特    ...

  6. Ubuntu 14.04 64位安装Android Studio 和 genymotion (上)

    先说下,Ubuntu 上安装Android Studio真是一路坑阿,一路坑阿,加上天  朝 防火墙挡着,折腾了快一天才弄好阿 找了n多教程,md不是抄的就是转的,而且都没说清楚具体咋装阿,一个图一个 ...

  7. libvirtVirsh

    virsh基于不同协议远程连接libvirt drivers(Hypervisors) 基于OS系统账号ssh登陆

  8. linux mount命令学习

    我们在下面这篇博文中已经有笼统的学习了文件系统的一些相关知识, http://blog.csdn.net/boyxulin1986/article/details/12107113 本篇我们主要是用来 ...

  9. Spring+Mybatis+mysql配置

    mybatis的映射文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper SY ...

  10. JAVA并发七(多线程环境中安全使用集合API)

    在集合API中,最初设计的Vector和Hashtable是多线程安全的.例如:对于Vector来说,用来添加和删除元素的方法是同步的.如果只有一个线程与Vector的实例交互,那么,要求获取和释放对 ...