(正权最短路)Dijkstra+优先队列模板---持续更新

#include
#include
#include
#include
using namespace std;
const int maxn = 1005;
const int INF = 1<<30;
int S, E;

struct Edge {
	int from, to, dist;
	Edge(int u, int v, int d):from(u), to(v), dist(d) {}
};

struct HeapNode{
		int d, u;
		bool operator < (const HeapNode& rhs) const {
			return d > rhs.d;
		}
};

struct Dijksdtra{
	int n, m;
	vector edges;
	vector G[maxn];
	bool done[maxn];
	int d[maxn];
	int p[maxn];
	
	void init(int n) {
		this->n = n;
		for(int i = 0; i < n; i++) G[i].clear();
		edges.clear();
	}
	
	void AddEdge(int from, int to, int dist) {
		edges.push_back(Edge(from, to, dist));
		m = edges.size();
		G[from].push_back(m-1);
	}
	
	void dijkstra(int s) {
		priority_queue Q;
		for(int i = 0; i < n; i++) d[i] = INF;
		d[s] = 0;
		memset(done, 0, sizeof(done));
		Q.push((HeapNode){0, s});
		while(!Q.empty()) {
			HeapNode x = Q.top(); Q.pop();
			int u = x.u;
			if(done[u]) continue;
			done[u] = true;
			for(int i = 0; i < G[u].size(); i++) {
				Edge& e = edges[G[u][i]];
				if(d[e.to] > d[u] + e.dist) {
					d[e.to] = d[u] + e.dist;
					p[e.to] = G[u][i];
					Q.push((HeapNode){d[e.to], e.to});
				}
			}
		}
	}
};

你可能感兴趣的:((正权最短路)Dijkstra+优先队列模板---持续更新)