dijkstra

朴素版


int dist[maxn], g[40][40];
bool vis[maxn];
void dijkstra(int cc) {
	//cc是单源节点编号 
	for (int i = 1; i <= n; i++) {
		dist[i] = g[i][cc];
	}
	dist[cc] = 0;
	
	memset(vis, 0, sizeof(vis));
	vis[cc] = 1;

	for (int i = 1; i <= n; i++) {
		int mark = -1, mindis = INF;
		for (int j = 1; j <= n; j++) {
			if (!vis[j] && dist[j] < mindis) {
				mindis = dist[j];
				mark = j;
			}
		}
		vis[mark] = 1;
		for (int j = 1; j <= n; j++) {
			if (!vis[j]) {
				dist[j] = min(dist[j], dist[mark] + g[mark][j]);
			}
		}
	}
}
posted @ 2019-11-18 16:43  阳离子  阅读(135)  评论(0编辑  收藏  举报