单源多目标最短路径模型

/*
	单源多目标最短路径模型
		————从顶点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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值