这就是问题所在:
予有n个点(P1,P2,P3,...的pn),其中每一个可以连接到任何其他与确定的费用为X
I have n points (p1, p2, p3, .. pn), each of them can connect to any other with a determined cost x.
每个点所属的一组点类型中的一个(例如ABCD...)。
Each point belongs to one of a set of point-types (for example "A" "B" "C" "D"...).
该方法的输入是我想要遵循,例如ABCADB的路径。
The input of the method is the path I want to follow, for example "A-B-C-A-D-B".
的输出是连接I给在输入的类型的点的最短路径,以便例如P1〜P4-P32-P83-P43-P12,其中p1为A型,P4 B型,p32的一个C型,P83的A型,P43的是D型和P12的B型
The output is the shortest path connecting the points of the type I give in input so for example "p1-p4-p32-p83-p43-p12" where p1 is an A-type, p4 a B-type, p32 a C-type, p83 an A-type, p43 a D-type and p12 a B-type.
易解决方案包括计算所有可能的路径,但计算成本是非常高的!
The "easy" solution consists of calculating ALL the possible paths but the computational cost is very high!
有人能找到一个更好的算法?
Can someone find a better algorithm?
正如我在标题说,我不知道它的存在!
As I said in title, I don't know if it exists!
更新:
这$ P $使用Dijkstra算法和其他类似的算法pvents我关键的一点是,我必须按照类型来链接点。
The key point that prevents me from using Dijkstra and the other similar algorithms is that I have to link the points according to type.
由于输入我的类型的数组和我的顺序链接。
As input I have an array of types and I have to link in that order.
这是弗雷德里克·肯特(非常感谢),它描述了初始状态(红色允许链接)!
This is an image of Kent Fredric (thanks a lot) which describes the initial situation (in red allowed links)!
一个现实生活中的例子:
A real life example:
一个人想参观教堂早上,去餐厅,最后参观在下午的博物馆。
A man wants to visit a church in the morning, go to restaurant and finally visit a museum in the afternoon.
在地图上有6个教堂,30个餐厅和4家博物馆。
In the map there are 6 churchs, 30 restaurants and 4 museums.
他想的是,距离教堂休息博物馆是最小可能的。
He wants that the distance church-rest-museum is the minimum possible.
推荐答案
您可以用弗洛伊德算法。下面是维基百科给出的伪code:
You can use the Floyd–Warshall algorithm. Here's the pseudocode given by WikiPedia:
/* Assume a function edgeCost(i,j) which returns the cost of the edge from i to
(infinity if there is none).
Also assume that n is the number of vertices and edgeCost(i,i)=0
*/
int path[][];
/* A 2-dimensional matrix. At each step in the algorithm, path[i][j] is the shortest path
from i to j using intermediate vertices (1..k-1). Each path[i][j] is initialized to
edgeCost(i,j) or infinity if there is no edge between i and j.
*/
procedure FloydWarshall ()
for k: = 1 to n
for each (i,j) in {1,..,n}2
path[i][j] = min ( path[i][j], path[i][k]+path[k][j] );
我只好一个算法当然这个同样的问题写一个程序。该算法的工作就像一个魅力!古德勒克。
I had to write a program for an algorithms course about this same problem. This algorithm worked like a charm! Goodluck.
相关推荐
最新文章