dijkstra

Dijkstra算法的Java实现

时光总嘲笑我的痴心妄想 提交于 2019-11-29 11:46:26
package main.java; 2 3 import main.java.utils.GraphUtil; 4 5 import java.util.ArrayDeque; 6 import java.util.List; 7 import java.util.Queue; 8 9 15 public class DijkstraTest { 16 17 //邻接矩阵的表示 18 public final static double[][] GRAPH_DISTANCE = GraphUtil.getDijkstraGraph(); 19 20 //起点到某节点的临时最短距离 21 public static double distance[] = new double[GRAPH_DISTANCE.length]; 22 23 //某节点的前驱节点 24 public static int pre[] = new int[GRAPH_DISTANCE.length]; 25 26 static int originIndex = 0, toIndex = 4; 27 28 29 public static void main(String[] args) { 30 31 init(); 32 findDijkstraShortestPath(); 33 } 34 35 /*

Weight map as function in Boost Graph Dijkstra algorithm

£可爱£侵袭症+ 提交于 2019-11-29 11:09:58
I'm using Boost Graph Libraries and need to use a weightmap which is not constant, but which is a function of a parameter K (i.e. the edge costs depend on K). In practice, given the following code: #include <boost/config.hpp> #include <iostream> #include <fstream> #include <boost/graph/graph_traits.hpp> #include <boost/graph/dijkstra_shortest_paths.hpp> #include <boost/graph/adjacency_list.hpp> struct Edge { Edge(float weight_) : weight(weight_) {} float weight; float getWeight(int K) { return K*weight; } }; int main(int, char**){ typedef boost::adjacency_list < boost::vecS, boost::vecS, boost

Dijkstra算法的Java实现

纵然是瞬间 提交于 2019-11-29 10:27:49
1 package main.java; 2 3 import main.java.utils.GraphUtil; 4 5 import java.util.ArrayDeque; 6 import java.util.List; 7 import java.util.Queue; 8 9 10 /** 11 * @Tme 2019/9/12 10:40 12 * @Author chenhaisheng 13 * @Email:ecjutsbs@foxmail.com 14 */ 15 public class DijkstraTest { 16 17 //邻接矩阵的表示 18 public final static double[][] GRAPH_DISTANCE = GraphUtil.getDijkstraGraph(); 19 20 //起点到某节点的临时最短距离 21 public static double distance[] = new double[GRAPH_DISTANCE.length]; 22 23 //某节点的前驱节点 24 public static int pre[] = new int[GRAPH_DISTANCE.length]; 25 26 static int originIndex = 0, toIndex = 4; 27 28 29

Find distance of route from get.shortest.paths()

匆匆过客 提交于 2019-11-29 09:54:17
问题 I'm using the igraph package in R to do something rather simple: Calculate the shortest distance between two nodes in my network. Is there a straightforward way to extract the distance of a path calculated via get.shortest.paths() ? Here is some reproducible code that exemplifies my problem: ## reproducible code: df2 = rbind(c(234,235,21.6), c(234,326,11.0), c(235,241,14.5), c(326,241,8.2), c(241,245,15.3), c(234,245,38.46)) df2 = as.data.frame(df2) names(df2) = c("start_id","end_id","newcost

how to save shortest path in dijkstra algorithm

我们两清 提交于 2019-11-29 06:43:56
So first let's define Dijkstra algorithm: Dijkstra's algorithm finds single-source shortest paths in a directed graph with non-negative edge weights. I want to know how can I save the shortest path form s to t with Dijkstra algorithm. I searched on google, but I couldn't find anything particular; I also changed Dijkstra algorithm, but I could't get any answer. How can I save the shortest path from s to t with Dijkstra ? I know my question is basic and unprofessional, but any help would be appreciated. Thanks for considering my question. If you look at the pseudocode from the Wikipedia link you

Dijkstra's algorithm with 'must-pass' nodes

老子叫甜甜 提交于 2019-11-29 05:15:49
I am trying to implement Dijkstra's algorithm which can find the shortest path between the start node and the end node. Before reach the end node there are some 'must-pass' intermediate nodes (more than one) for example 2 or 3 must pass nodes which must pass before reach the end node. If i have one must pass node the solution i found is find two different paths from the must pass node to destination and from must pass node to start node. I am out of ideas how i can implement such an algorithm. Any suggestions? Thanks. List<Node> closestPathFromOrigin = null; double maxD = Double.POSITIVE

cogs 2. 旅行计划 dijkstra+打印路径小技巧

回眸只為那壹抹淺笑 提交于 2019-11-29 05:07:54
2. 旅行计划 ★★ 输入文件: djs.in 输出文件: djs.out 简单对比 时间限制:3 s 内存限制:128 MB 【题目描述】 过暑假了,阿杜准备出行旅游,他已经查到了某些城市的两两之间的距离及可行路线(可行路线有方向),如下图所示。请你编程计算从阿杜所住城市到其它城市的最短路径以帮助阿杜制定旅行计划。 【输入格式】 输入由若干行组成,第一行有三个整数 n ( 1 ≤ n ≤ 100 ) 为城市数, m ( 1 ≤ m ≤ n 2 ) 为城市间道路数, s ( 0 ≤ s ≤ n − 1 ) 是阿杜所住城市。第 2 至 m + 1 行是每条路的信息,每行三个整数,为道路的起点、终点和两城市间距离。(城市从 0 开始编号) 【输出格式】 输出 n 组(按城市编号由小至大),每组三行 第一行:城市编号及一个冒号 第二行:path及一个冒号,后面是最短路径节点编号序列(编号间用一个空格隔开) 第三行:cost及一个冒号,后面是一个整数,表示路径距离 如果没有通路则输出 no 【输入样例】 6 8 0 0 2 10 0 4 30 0 5 100 1 2 5 2 3 50 3 5 10 4 3 20 4 5 60 【输出样例】 0: no 1: no 2: path:0 2 cost:10 3: path:0 4 3 cost:50 4: path:0 4 cost:30 5:

第二周星期六

时间秒杀一切 提交于 2019-11-29 02:27:24
1.dijkstra算法的一种代码实现,一个dijkstra类 public class Dijkstra { public static void main(String[] args) { int[][] graph=new int[][]{{0,Integer.MAX_VALUE,10,Integer.MAX_VALUE,30,100}, {Integer.MAX_VALUE,0,5,Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE}, {Integer.MAX_VALUE,Integer.MAX_VALUE,0,50,Integer.MAX_VALUE,Integer.MAX_VALUE}, {Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE,0,Integer.MAX_VALUE,10}, {Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE,20,0,60}, {Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE,0}}; int[] dis=new int[6];

最短路spaf及dijkstra模板

我们两清 提交于 2019-11-28 22:12:41
spaf的双端队列优化: #include<bits/stdc++.h> #define ll long long const ll maxn=210000; using namespace std; ll n,m,s,tot,link[maxn],dis[maxn],vis[maxn]; struct bian { ll y,v,next; }; bian a[maxn]; inline void add(ll x,ll y,ll v) { a[++tot].y=y; a[tot].v=v; a[tot].next=link[x]; link[x]=tot; } inline void spaf() { memset(dis,127,sizeof(dis)); deque<ll>q; q.push_front(s); dis[s]=0;vis[s]=1; while(!q.empty()) { ll x=q.front();q.pop_front();vis[x]=0; for(int i=link[x];i;i=a[i].next) { int y=a[i].y; if(dis[y]>dis[x]+a[i].v) { dis[y]=dis[x]+a[i].v; if(!vis[y]) { if(!q.size()||dis[y]>dis[q.front()]) q.push

Dijkstra graph with a table of weights on each edge

一世执手 提交于 2019-11-28 21:59:50
I have a boost graph with multiples weights for each edges (imagine one set of weights per hour of the day). Every one of those weights values is stored in a propretyEdge class : class propretyEdge { std::map<std::string,double> weights; // Date indexed } I created a graph with those properties, and then filled it with the right values. The problem is now that I want to launch the Dijkstra algorithm over a particular set of weight on the graph : for example a function that could be : void Dijkstra (string date, parameters ... ) That would use the weights[date] value for each Edge of the graph.