/*
单源多目标最短路径模型
————从顶点v到其他顶点的最短距离
*/
#define MAX_VERTICE 100
#define TRUE 1
#define FALSE 0
int cost[MAX_VERTICE][MAX_VERTICE];
int distance[MAX_VERTICE];
short int found[MAX_VERTICE];
int n = MAX_VERTICE;
void shortestpath(int v, int cost[][MAX_VERTICE], int distance[], int n, short int found[])
{
/* distance[i] represents the shortest path from vertex v to vertex i */
/* found[i] holds a 0 if the shortest path from vertex i has not been found and a 1 if it has */
/* cost[][] is the adjacency matrix */
/* n stand for n vertex */
/* v stand for the source vertex */
int i, u, w;
for(i = 0; i < n; i++){
found[i] = FALSE;
distance[i] = cost[v][i];
}
found[v] = TRUE;
distance[v] = 0;
for(i = 0; i < n - 2; i++){
u = choose(distance, n, found);
found[u] = TRUE;
for(w = 0; w < n; w++)
if(!found[w])
if(distance[u] + cost[u][w] < distance[w])
distance[w] = distance[u] + cost[u][w];
}
}
int choose(int distance[], itn n, short int found[])
{
/* find the smallest distance not yet checked */
int i, min, minpos;
min = INT_MAX;
minpos = -1;
for(i = 0; i < n; i++)
if(distance[i] < min && !found[i]){
min = distance[i];
minpos = i;
}
return minpos;
}
单源多目标最短路径模型
最新推荐文章于 2023-04-07 19:58:03 发布