dijkstra

Dijkstra算法(记录路径)

纵饮孤独 提交于 2019-12-26 17:07:52
与求最短路相比,增加一个path数组,来记录最短路的路径 先将path[i]=-1,之后每次找出最短路的点p后将path[j]=p 用path[j]=i表示从i到j最短路的路径 for(int j=1; j<=n; j++){ if(!visited[j] && dis[p]+mapp[p][j]<dis[j]){ dis[j]=dis[p]+mapp[p][j]; path[j]=p; } } 1339: 单源最短路径 时间限制: 1 Sec 内存限制: 128 MB 提交: 4 解决: 3 [ 提交 ][ 状态 ][ 讨论版 ][命题人: 外部导入 ] 题目描述 给定带权有向图G=(V,E),其中每条边的权是非负数。另外,还给定V中的一个顶点,称为源。现在要计算从源到所有其他各顶点的最短路径长度,这里路径的长度是指路径上各边权之和。这个问题通常称为单源最短路径问题(Single-Source Shortest Paths)。 如下图所示,就是要计算源点V1到其他各个顶点的最短距离,并输出相应的路径。 输入 本题有多组数据,第 1 行有 2 个数据 n 和 m ,其中 n 表示结点的个数, m 表示路径的数目。 接下来有 m 行,每行有 3 个数据 s,t 和 edge ,其中 s 表示路径的起点, t 表示路径的终点, edge 表示该路径的长度。 当 n=0 , m=0 时

Java HashMap File Input Version of Code

偶尔善良 提交于 2019-12-25 18:40:16
问题 I have here a piece of code. This is a snippet from my Dijkstra implementation. Here, we have 4 vertices instantiated and each vertex has specified edges and its distance from the vertex(node). This code is straightforward and beneficial if the data will directly be inputted in the main class. Vertex v0 = new Vertex("Redvile"); Vertex v1 = new Vertex("Blueville"); Vertex v2 = new Vertex("Greenville"); Vertex v3 = new Vertex("Orangeville"); Vertex v4 = new Vertex("Purpleville"); v0.adjacencies

Shortest Path Finder from CSV in C#

时间秒杀一切 提交于 2019-12-25 08:08:00
问题 Let's say I have the following CSV Sydney,Dubai,1 Dubai,Venice,2 Venice,Rio,3 Venice,Sydney,1 Sydney,Rio,7 First field is From second is To and third is Duration . I need a method which can take a From input and spit out the shortest path to all other To field in the following format- Selected City: Sydney To 1: Dubai, Smallest Path Length: 1, Path: Sydney, Dubai. To 2: Venice, Smallest Path Length: 3, Path: Sydney, Dubai, Venice. To 3: Rio, Smallest Path Length: 6, Path: Sydney, Dubai,

Dijkstra on adjacency matrix in C

孤街浪徒 提交于 2019-12-25 02:47:19
问题 I need some help with Dijkstra's algorithm in C. I've generated my adjacency matrix, that looks something like: int mat[NB][NB] = {{0, 171, MAX, 132, [...]}, {171, 0, 30, 39, [...]}, , [...]}; I've found this implementation: http://www.answers.com/topic/dijkstra-s-algorithm-1 but the path is an 1-dimensional array and my matrix is a 2-dimensional array. Is there a way to transform one to another? Or maybe someone has a method to deal with this kind of matrix. Thanks in advance for any help

How can I implement dijkstra's algorithm to optimize a blending schedule?

两盒软妹~` 提交于 2019-12-25 00:08:33
问题 I am attempting to automate the schedule for a blending facility. I am trying to implement Dijkstra's algorithm in a way that will allow me to select the type of blends from a drop down list, and input the quantity of blends needed. This will generate a list of nodes with a predetermined cost depending on the previous blend (Blender requires a full-day wash when switching from blend 1 to blend 2, but not when going from blend 1 to blend 1 etc). The starting and ending nodes don't matter, only

Find Two vertices with lowest path weight

ぐ巨炮叔叔 提交于 2019-12-24 21:01:17
问题 I am trying to solve this question but got stuck. Need some help,Thanks. Given an undirected Connected graph G with non-negative values at edges. Let A be a subgroup of V(G), where V(G) is the group of vertices in G. -Find a pair of vertices (a,b) that belongs to A, such that the weight of the shortest path between them in G is minimal, in O((E+V)*log(v))) I got the idea of using Dijkstra's algorithm in each node which will give me O(V*((E+V)logv))),which is too much. So thought about

Using Dijkstra to detect multiple shortest paths

本秂侑毒 提交于 2019-12-24 00:36:48
问题 Given a weighted directed graph, how can the Dijkstra algorithm be modified to test for the presence of multiple lowest-cost paths between a given pair of nodes? My current algorithm is as follows: (credit to Weiss) /** * Single-source weighted shortest-path algorithm. (Dijkstra) * using priority queues based on the binary heap */ public void dijkstra( String startName ) { PriorityQueue<Path> pq = new PriorityQueue<Path>( ); Vertex start = vertexMap.get( startName ); if( start == null ) throw

最短路径 -- Dijkstra算法

一曲冷凌霜 提交于 2019-12-22 06:34:58
定义一个有向图D=(V, A),对每一个弧a=(v i , v j ),相应地有权ω(a)=ω ij ,又给定D中的两个顶点v s ,v t 。设P是D中从v s 到v t 的一条路径,定义路P的权是P中所有弧的权之和,记为ω§。最短路径问题就是要在所有从v s 到v t 的路中,求一条权最小的路,即求一条从v s 到v t 的路P 0 ,使 ω(P 0 )=min ω(P) 在Dijkstra算法中,用P(v),T(v)分别表示点v的P标号和T标号,S i 表示第i步时,具P标号的点的集合。为了求出从v s 到各点的距离的同时,也求出从v s 到各点的最短路径,给每个点v以一个λ值,算法终止时,如果λ(v)=m,表示在从v s 到v的最短路径上,v的前一个点是v m ;如果λ(v)=M,表示D中不含从v s 到v的路;λ(v)=0表示v=v s 。 Dijkstra算法具体步骤: 给定赋权有向图D=(V, A)。 开始(i=1)令S1={v s },P(vs)=0,λ(v s )=0,对每一个v != v s ,令T(v)= ++,λ(v)=M,令k=s。 第一步:如果S i =V,算法终止,这时,对每个v∈S i ,d(v s ,v)=P(v);否则转入第二步。 第二步:考查每个使(v k , v j )∈A且v j !∈ S i 的点v j 。如果T(v j ) > P(v

Shortest path in a map

不打扰是莪最后的温柔 提交于 2019-12-21 19:50:43
问题 I have designed a weighted graph using a normalized adjacency list in mysql. Now I need to find the shortest path between two given nodes. I have tried to use Dijkstra in php but I couldnt manage to implement it (too difficult for me). Another problem I felt was that if I use Dijkstra I would need to consider all the nodes, that may be perhaps very inefficient in a large graph. So does anybody has a code relating to the above problem? It would be great if somebody atleast shows me a way of

FInding All Shortest Paths Between Two Vertices

我是研究僧i 提交于 2019-12-21 18:40:42
问题 Given a directed graph G=(V,E) , two vertices s , t and two weight functions w1 , w2 , I need to find the shortest path from s to t by w2 among all the shortest paths between s to t by w1 . First of all , how could I find all the shortest paths between two vertices s and t ? Dijkstra's algorithm helps us find shortest path from a vertex to to every other accessible vertex, is it possible to modify it in order to get all the shortest paths between two vertices? 回答1: I will expand on comments