/**
* [Dijkstra description]
* @param mp 权值矩阵,从1开始
* @param n 点数
* @param s 起始点
* @param t 终点
* @param path 起始点到各点的路(前驱)
* @return 起点到终点的最短长度
*/
int Dijkstra(int s, int t, int path[])
{
int i, j, w, minc;
bool visit[n + 1];
int price[n + 1];
for (i = 1; i <= n; i++)
visit[i] = false;
for (i = 1; i <= n; i++)
{
price[i] = mp[s][i];
path[i] = s;
}
visit[s] = true;
price[s] = 0;
//path[s]=0;
for (i = 1; i < n; i++)
{
minc = INF;
w = 0;
for (j = 1; j <= n; j++)
if ((visit[j] == false) && (minc >= price[j]))
{
minc = price[j];
w = j;
}
visit[w] = true;
for (j = 1; j <= n; j++)
if ((visit[j] == false) && (mp[w][j] != INF) && (price[j] > price[w] + mp[w][j]))
{
price[j] = price[w] + mp[w][j];
path[j] = w;
}
}
int k=t;
while (k != s)
{
printf("%d<--", k);
k = path[k];
}
return price[t];
}
Dijstra
最新推荐文章于 2019-08-04 18:35:54 发布