【模板】Bellman-Ford

C++版本:

Bellman-Ford

void shortest_path(int s) {
	for (int i = 0; i <= V; i++)d[i] = INF;
	d[s] = 0;
	while (true) {
		bool update = false;
		for (int i = 1; i <= E; i++) {
			edge e = es[i];
			if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
				d[e.to] = d[e.from] + e.cost;
				update = true;
			}
		}
		if (!update)break;
	}
}
posted @ 2020-09-13 18:19  pjhui  阅读(87)  评论(0编辑  收藏  举报