c++ 用STL容器实现Dijkstraalg问题
在cgraph::cgraph(char* inputFile)
{
ifstream file(inputFile);
file>>numvertex>>numedge; 处无法正确传参,显示节点数和边数都是无穷大,错误如下

弄了两天实在不知为何,前来一试
若有高台指教,感激不尽。

#include"CEdge.h"
#include"tryy.h"
#include"add.h"
class cgraph
{
private: int numvertex,numedge;
list<cedge *>incidentlist;
map<int,cvertex *>mapVID_vertex; // all cvertex
list<cvertex *>listtempmark;// 暂时标记的顶点的集合
map<int,list<cedge*>>mapVID_listedge;//记录与顶点关联的出度边;
public:
cgraph(char* inputFile);
cgraph(list<cedge*> listedge);
cgraph(cgraph &);
~cgraph();
int getnumvertex();
int getnumedge();
void Dijkstraalg(int s);
list<cedge*> getlist();
void PrintShortestPath(int s, int d);
void PrintAllShortestPath(int s);
void DeleteIncidentList(int c);
void update(int VID);
void pro2_1output();
void update2(int VID);
void Dijkstraalg2(int s);
};
void cgraph::update(int VID)
{
list<cedge *> ledge=mapVID_listedge[VID];
list<cedge *>::iterator i, iend;
iend=ledge.end();
for(i=ledge.begin();i!=iend;i++)
{
int w=(*i)->getWeight();
cvertex* h=mapVID_vertex[(*i)->getHead()];
cvertex* t=mapVID_vertex[VID];
if(t->d+w<h->d)
{
h->d=t->d+w;
h->p=VID;
}
}
}
void cgraph::Dijkstraalg(int s)
{
map<int,cvertex*>::iterator i,iend;
iend=mapVID_vertex.end();
for(i=mapVID_vertex.begin();i!=iend;i++)
{
if(i->second->ID==s)
i->second->d=0;
listtempmark.push_back(i->second);
}
update(s);
while(!listtempmark.empty())
{
listtempmark.sort(pvertexcomp);
int j=(*listtempmark.begin())->ID;
listtempmark.pop_front();
update(j);
}
}
cgraph::cgraph(char* inputFile)
{
ifstream file(inputFile);
file>>numvertex>>numedge;
for (int i = 0; i < numvertex; i++)
{
cvertex* vp;
vp = new cvertex(INFINITY, NULL, i);
mapVID_vertex[i] = vp;
}
for (int i = 0; i< numedge; i++)
{
int edgid,head, tail, weight,capacity; //对input函数进行修改,增加容量
file>>edgid>>head>>tail>>weight>>capacity;
cedge *ep = new cedge(edgid,head, tail, weight,capacity);
incidentlist.push_back(ep);
list<cedge*> templist;
templist = mapVID_listedge[tail];
mapVID_listedge[tail].push_back(ep);
}
file.close();
}
void cgraph::PrintShortestPath(int s, int d)
{
cout<< " the length of the shortest path from "<<s<<" to "<<d<<" is: "<<mapVID_vertex[d]->d<<endl;
cout<< " the shortest path from "<<s<<" to "<<d<<" is: ";
list<int> shortest;
shortest.push_front(d);
int pre = d;
while(s!=pre)
{
pre = mapVID_vertex[pre]->p;
shortest.push_front(pre);
}
list<int>::iterator beg, tempiter;
for(beg = shortest.begin();beg != shortest.end();beg++)
{
cout<<*beg << " ";
}
cout<<endl;
}
void cgraph::PrintAllShortestPath(int s)
{
map<int, cvertex*>::iterator mapiter ;
for (mapiter = mapVID_vertex.begin(); mapiter != mapVID_vertex.end(); mapiter++)
{
if ( s != mapiter->second->ID)
{
PrintShortestPath(s, mapiter->second->ID);
}
}
}
cgraph::~cgraph()
{
}
// ****************************************************主函数部分**************************//
int main()
{
cgraph* graph = new cgraph("dijkstra.txt");
graph->Dijkstraalg(1); //测试源结点为1的所有最短路的dijkstra和相应输出
graph->PrintAllShortestPath(1);
cout<<"function pro2_1"<<endl;
graph->PrintShortestPath(1,8);//单源单宿测试
graph->Dijkstraalg2(1); //pro2_2
cout<<"function pro2_2"<<endl;
graph->PrintAllShortestPath(1);
int c;
cout<<"input the lower limit capacity"<<endl;
cin>>c;
void DeleteIncidentList(int c); //pro2_3
graph->Dijkstraalg(1);//仍以1为例,假设删除小于c权重的边后1节点仍在图中
cout<<"function pro2_3"<<endl;
graph->PrintShortestPath(1,8);
}
相关推荐
【MATLAB例程】基于APF的多无人机动态路径规划与避障仿真 四无人机协同避碰、动态障碍规避及安全性能分析(含公式)
程序基于多无人机二维运动学模型,采用人工势场法(APF)实现四架无人机在固定与动态障碍环境下的路径规划和协同避碰。绘制无人机运动轨迹、障碍物轨迹、安全净间隙、速度与加速度、航程及到达时间等图像,输出目标到达数量、碰撞统计和安全性能指标。无人机数量、障碍物分布、运动约束及APF算法参数均可自行调整。
《智能之门》配套PPT课件
《智能之门》配套PPT课件
chromedriver-win64-156.0.8078.80(Stable).zip
chromedriver-win64-156.0.8078.80(Stable).zip
三相并网逆变器虚拟阻抗+统一有源阻尼策略SVPWM+SPWM调制仿真
内容概要:本文档系统梳理了电力电子与新能源并网领域的核心技术与仿真实践,聚焦三相并网逆变器的先进控制策略与调制技术,涵盖虚拟阻抗、统一有源阻尼、SVPWM与SPWM调制、预测电流控制、中点电位平衡、低电压穿越(LVRT)及孤岛检测等关键技术。文档整合了大量基于Matlab/Simulink的高保真仿真模型,涉及三电平ANPC/T型/Z源逆变器、LCL滤波器、虚拟同步发电机(VSG)、下垂控制、构网型变流器等多种主流拓扑与控制架构,并延伸至风光储联合系统、微电网能量管理、弱电网稳定性分析、序阻抗建模与扫频法等复杂应用场景,特别强调SCI论文成果复现与工程化设计实现,具有极强的科研指导性与实践价值。; 适合人群:面向电力电子、电气工程及其自动化等相关专业的研究生、高校科研人员及从事新能源发电、微电网控制、逆变器研发的工程技术人员,要求具备扎实的电路理论、自动控制原理基础及一定的Matlab/Simulink仿真能力。; 使用场景及目标:①支撑高水平学术论文的仿真实验设计与结果验证,尤其是构网型变流器、虚拟同步技术、主动阻尼机制等前沿方向的研究;②用于毕业设计、科研项目中复杂电力电子系统的建模与控制策略开发;③助力工程师掌握从单机逆变器控制到多能互补系统协同运行的全流程仿真技术体系。; 阅读建议:建议结合文档提供的参考文献与仿真资源,采用“由简入繁、循序渐进”的学习路径,先掌握三相并网逆变器PI控制与SVPWM调制等基础模型,再逐步深入VSG并离网切换、多机并联环流抑制、弱电网阻抗交互等高阶课题,注重理论推导、参数设计与仿真调试的深度融合,提升系统级建模与问题分析能力。
三相四桥臂电压不对称跌落补偿动态电压恢复器DVR(Simulink仿真实现)
内容概要:本文介绍了一种基于三相四桥臂拓扑结构的动态电压恢复器(DVR),用于补偿电网中由电压不对称跌落引起的电能质量问题。通过Simulink平台构建仿真模型,重点研究了在不平衡负载和电网电压跌落工况下,DVR对负载侧电压的快速补偿能力。系统采用多准PR(Proportional Resonant)并联控制策略,实现对各相电压的独立精确控制,有效抑制负序分量,提升系统在复杂工况下的稳定性和响应性能。仿真结果验证了所提控制方法在电压幅值、相位补偿及波形质量改善方面的有效性,适用于对供电连续性和电能质量要求较高的工业场景。; 适合人群:电力电子、电气工程及其自动化等相关专业的研究生、科研人员及从事电能质量治理的工程技术人员。; 使用场景及目标:①研究动态电压恢复器在电压跌落、三相不平衡等典型电能质量问题中的补偿机制;②掌握三相四桥臂逆变器的建模与多准PR控制策略的设计与实现;③为高精度电能质量调节装置的研发提供理论依据和仿真验证手段。; 阅读建议:建议结合提供的参考文献和说明文档,深入理解控制算法的设计原理,并在Simulink环境中动手复现仿真模型,通过调整故障条件和控制器参数,观察系统动态响应,从而全面掌握DVR的工作特性与优化方法。


