Java教程

Dijkstra:计算最短路径,不适用负权边

本文主要是介绍Dijkstra:计算最短路径,不适用负权边,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include <stdio.h>
#include <limits.h>
#define inf 0x3f3f3f3f
const int maxn = 10001;
int  n, dist[maxn], map[maxn][maxn], pre[maxn], s;
//s为起点,dist[i]表示i到起点的最短距离,map记录图信息,pre记录前驱,原点,终点
void Dijkstra() {
	int min = INT_MAX;//导入limits.h
	int k;
	bool p[maxn];
	for (int i = 1; i <= n; i++) {//初始化map
		p[i] = false;
		if (i != s) {
			dist[i] = map[s][i];
			pre[i] = s;
		}
	}
	dist[s] = 0;
	p[s] = true;
	for (int i = 1; i <= n - 1; i++) {
		k = 0;
		for (int j = 1; j <= n; j++) {
			if (!p[j] && dist[j] < min) {
				min = dist[j];
				k = j;
			}
		}
		if (k == 0) {
			return;
		}
		p[k] = true;
		for (int j = 1; j <= n; j++) {
			if (!p[j] && map[k][j] != INT_MAX && dist[j] > dist[k] + map[k][j]) {
				dist[j] = dist[k] + map[k][j];
				pre[j] = k;
			}
		}
	}
}

int main() {
	int m;//m条路线
	while (true) {
		scanf("%d %d %d", &n, &m, &s);
		if (n == 0)
			break;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				if (i == j) {
					map[i][j] = 0;
				} else {
					map[i][j] = inf;
				}
			}
		}
		for (int i = 1; i <= m; i++) {
			int x, y;
			scanf("%d %d", &x, &y);
			scanf("%d", &map[x][y]);
			map[y][x] = map[x][y];
		}
		Dijkstra();
		for (int i = 1; i <= n; i++) {
			printf("%d", dist[i]);
			printf("\n");
		}
	}
	return 0;
}

这篇关于Dijkstra:计算最短路径,不适用负权边的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!