#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int MAX_N=100010;
struct edge{int to, cost;};
typedef pair <int,int >p;
vector<edge> G[MAX_N];//create ADj list
int N;
int d[MAX_N];//到顶点的最短距离
void dij(int s)
{
priority_queue<p,vector<p>,greater<p>> que;
fill(d,d+N,9999999);
d[s]=0;
que.push(p(0,s));
while (!que.empty())
{
p cur=que.top();
que.pop();
int v=cur.second;//最短路径已经确定的点
for (int i = 0; i < G[v].size(); i++)
{
edge e=G[v][i];//遍历v所能达到的所有边
if (d[e.to]>d[v]+e.cost)//用e来松弛
{
d[e.to]=d[v]+e.cost;
que.push(p(d[e.to],e.to));
}
}
}
}
Dijkstra算法【堆优化】
最新推荐文章于 2024-02-05 20:16:36 发布