[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 ...
随机推荐
- PHP面试系列 之Linux(五)---- 案例
题:如何实现每天0点重新启动服务器? 答: (1)创建定时任务,并进行编辑 crontab -e (2)编写脚本内容 * * * reboot 0分 0时 每日 每月 每周 执行的命令:reb ...
- Java Calendar and SimpleDateFormat 时间模块
package UtilTest; import java.util.Calendar; import java.text.SimpleDateFormat; import org.apache.co ...
- 闲话缓存:ZFS 读缓存深入研究-ARC(二)
Solaris ZFS ARC的改动(相对于IBM ARC) 如我前面所说,ZFS实现的ARC和IBM提出的ARC淘汰算法并不是完全一致的.在某些方面,它做了一些扩展: · ZFS A ...
- What to do next to activate this settings for already existing users
Link: http://sharepoint.stackexchange.com/questions/89284/sharepoint-2013-mysite-increase-quota Cent ...
- spring 包的依赖关系
转自:http://www.cnblogs.com/ywlaker/p/6136625.html 很多人都在用spring开发java项目,但是配置maven依赖的时候并不能明确要配置哪些spring ...
- Knowledge-Reserve
Knowledge-Reserve ComputerOperatingSystem 编译 静态库&动态库(Linux) 静态链接&动态链接 内存 内联函数&宏 Static&a ...
- canvas转img,blob相互转换
摘自:https://www.cnblogs.com/jyuf/p/7251591.html 函数都比较简单,直接看就ok了 /*----------------------------------- ...
- 给Extjs的window弹窗的关闭事件添加验证
问题:我想在window点击右上角叉关闭时添加一些验证,来确定是否关闭? 实现: 首先想到的是拦截window的关闭事件,在它关闭前添加验证,但是有一个问题是,如何阻止它的关闭和组织关闭后,如何让它再 ...
- BZOJ 5248: [2018多省省队联测]一双木棋(对抗搜索)
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 439 Solved: 379[Submit][Status][Discuss] Descriptio ...
- JS变量、作用域、内存
写到这个题目<JS变量.作用域,内存>,我就不由自主想起了黄金三嫖客.可能是名字有点像,嗯,一定是这样子的! JS接触下来,应该是要比Java简单不少的,所以,要学好啊.立个flag半年后 ...