相对于无权最短路径,赋权最短路径多了一个成员know来标记顶点是否已知。从未知的顶点中找到路径最短的顶点,标记为已知,然后如果它的未知邻接顶点距离dw大于dv+Cv,w,则更新dw。
头文件
#include <vector>
#include <map>
#include <iostream>
static const int MAX=10000;
using namespace std;
class Graph
{
public:
explicit Graph(int vertexNum):v(vertexNum+1)
{
for(auto x:v)
{
x.dist=MAX;
x.path=nullptr;
x.adjacentList=map<Vertex*,int>{};
x.know=false;
}
initialVertex=0;
}
void setVertex(int vertexIndex,const map<int,int> & adjacentIndex)
{
//创建邻接表
for(auto x:adjacentIndex)
{
v[vertexIndex].adjacentList[&v[x.first]]=x.second;
}
}
void weighted(