#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define maxn 3005
struct HeapNode{ //优先队列结点
int d,u;
bool operator < (const HeapNode& rhs) const{
return d>rhs.d;
}
};
struct Edge{ //边
int from,to,dist,id;
};
struct Dijkstra{
int n,m; //点数和边数
vector<Edge> edges; //边列表
vector<int> G[maxn]; //每个结点出发的边编号(从0开始)
bool done[maxn]; //是否已永久标号
int d[maxn]; //s到各个点的距离
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,int id){
edges.push_back((Edge){from,to,dist,id});
m=edges.size();
G[from].push_back(m-1);
}
void dijkstra(int s){ //求s到所有点的最短距离
priority_queue<HeapNode> 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]=1;
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]=e.id;
Q.push(HeapNode{d[e.to],e.to});
}
}
}
}
};
Dijkstra(优先队列)
最新推荐文章于 2024-11-28 09:15:00 发布