﻿ 算法 – 图 – 具有顶点权重的最短路径 - 代码日志

#### 算法 – 图 – 具有顶点权重的最短路径

In certain graph problems, vertices have can have weights instead of
or in addi- tion to the weights of edges. Let Cv be the cost of vertex
v, and C(x,y) the cost of the edge (x, y). This problem is concerned
with finding the cheapest path between vertices a and b in a graph G.
The cost of a path is the sum of the costs of the edges and vertices
encountered on the path.

(a) Suppose that each edge in the graph has a weight of zero (while
non-edges have a cost of ∞).Assume that Cv =1 for all vertices 1≤v≤n
(i.e.,all vertices have the same cost). Give an efficient algorithm to
find the cheapest path from a to b and its time complexity.

(b) Now suppose that the vertex costs are not constant (but are all
positive) and the edge costs remain as above. Give an efficient
algorithm to find the cheapest path from a to b and its time
complexity.

(c) Now suppose that both the edge and vertex costs are not constant
(but are all positive). Give an efficient algorithm to find the
cheapest path from a to b and its time complexity.

(a)使用正常的BFS

(b)使用dijkstra的算法,但用顶点重量代替重量

(C)

``````if (distance[y] > distance[v]+weight) {
distance[y] = distance[v]+weight; // weight is between v and y
}
``````

``````if (distance[y] > distance[v] + weight + vertexWeight[y]) {
distance[y] > distance[v] + weight + vertexWeight[y]; // weight is between v and y
}
``````

``````w'(u,v) = w(u,v) + vertex_weight(v)
``````

(1)在这个解决方案中,你“错过”源的权重,所以从s到t的最短路径将是：dijkstra(s,t,w’)vertex_weight(s)_ [其中dijkstra(s,t,w ‘)是从s到t的距离,使用w’