[C++]boost dijkstra获得两点间的最短路
需求是只需要得到两点间的最短路,不需要求得单源对于全图的最短路,使用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;
}
[C++]boost dijkstra获得两点间的最短路的更多相关文章
- boost dijkstra获得两点间的最短路
需求是只需要得到两点间的最短路,不需要求得单源对于全图的最短路,使用boost中的dijsktra_shortest_path,当得到目标点的最短路时直接throw exception. #inclu ...
- [CF1051F]The Shortest Statement (LCA+最短路)(给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路)
题目:给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路 n≤100000,m≤100000,m-n≤20. 首先看到m-n≤20这条限制,我们可以想到是围绕这个20来做这道题. 即如果我们 ...
- AOJ -0189 Convenient Location && poj 2139 Six Degrees of Cowvin Bacon (floyed求任意两点间的最短路)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=78207 看懂题就好. 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...
- 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 ...
- HDU6166-求集合间的最短路
Senior Pan Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- 使用PostGIS完成两点间的河流轨迹及流经长度的计算
基础准备工作 1.PostGIS 的安装 在安装PostGIS前首先必须安装PostgreSQL,然后再安装好的Stack Builder中选择安装PostGIS组件.具体安装步骤可参照 PostGI ...
- 用 Excel 测试“绘制两点间连线”的算法
最近在研究和制作数字示波器,其中涉及一个小算法:需要将 ADC 采样的数值在 TFT LCD 屏幕上面显示并且用“线”连接起来. ADC 按照时序对输入电压采样后,记录的是一个个的数值,如果显示的时候 ...
- 图算法之Floyd-Warshall 算法-- 任意两点间最小距离
1.Floyd-Warshall 算法 给定一张图,在o(n3)时间内求出任意两点间的最小距离,并可以在求解过程中保存路径 2.Floyd-Warshall 算法概念 这是一个动态规划的算法. 将顶点 ...
- HDOJ2001计算两点间的距离
计算两点间的距离 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
随机推荐
- redis安装及测试
http://jingyan.baidu.com/article/9113f81b0333e12b3214c7a8.html 下载地址:http://git.oschina.net/bingoPure ...
- Python cx_oracle自动化操作oracle数据库增删改查封装,优化返回查询数据
# coding=utf-8 import cx_Oracle import os import json os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_C ...
- ubuntu配置telnet服务
1.安装xinetd 以及telnetd #:~$ sudo apt-get install xinetd telnetd 2.配置文件(若文件不存在就手动添加文件和相应配置信息) 1): #:~$ ...
- 与MySQL的零距离接触
存储引擎 查看数据表的创建命令:show create table tbl_name
- 一篇很好的解释了.Net Core, .Net Framework, .Net standard library, Xamarin 之间关系的文章 (转载)
Introducing .NET Standard In my last post, I talked about how we want to make porting to .NET Core e ...
- 在Red Hat Enterprise Linux 7.3上安装SQL Server 2017
必要条件: 1.在此快速安装过程中,您需要安装SQL Server 2017或SQL Server 2019上Red Hat Enterprise Linux (RHEL) 7.3 +.然后使用sql ...
- 水仙花数(类型:一级、C++)
题目描述: 输入一个三位数n,判断是否为水仙花数,如果是则输出“YES”,不是则输出“NO”.水仙花数:是指一个3位数,它的每个位上的数字的3次幂之和等于它本身.(例如:1^3 + 5^3+ 3^3 ...
- 基于CLGeocoder - 反地理编码
iOS中CoreLocatio框架中的CLGeocoder 类不但为我们提供了地理编码方法,而且还提供了反地理编码: 同样需要导入框架: #import <CoreLocation/CoreLo ...
- opacity(透明度)属性设置
1.opacity:是属性,整个元素包含子元素全部都会透明,取值范围0-1; 2.background-color:rgba(0,0,0,.5)rgba是值,用于背景,取值范围0-1. 补充:a-al ...
- Golddata如何采集需要登录/会话的数据?
概要 本文将介绍使用GoldData半自动登录功能,来采集需要登录网站的数据.GoldData半自动登录功能,就是指通过脚本来执行登录,如果需要验证码或者其它内容需要人工输入时,可以通过收发邮件来执行 ...