【模板】Dijkstra

C++版本:

Dijkstra

void dijkstra(int s) {
	priority_queue<P, vector<P>, greater<P> >que;
	fill(d + 1, d + V + 1, INF);
	d[s] = 0;
	que.push(make_pair(0, s));
	while (!que.empty()) {
		int u = que.top().second;
		int di = que.top().first;
		que.pop();
		if (di > d[u])continue;
		for (int i = head[u]; i; i = eg[i].next) {
			int v = eg[i].to;
			if (d[v] > d[u] + eg[i].cost) {
				d[v] = d[u] + eg[i].cost;
				que.push(make_pair(d[v], v));
			}
		}
	}
}
posted @ 2020-09-13 18:25  pjhui  阅读(94)  评论(0编辑  收藏  举报