C++ c++;Prim';s算法(映射、向量和最小优先级队列)

C++ c++;Prim';s算法(映射、向量和最小优先级队列),c++,algorithm,vector,stdmap,prims-algorithm,C++,Algorithm,Vector,Stdmap,Prims Algorithm,我已经在这一问题上工作了三天,我被难住了。我们必须使用2个贴图(字符串、顶点(类)和字符串、向量)实现prim算法。第一个类在vertex类中存储一个字母作为名称、pi和键。第二个贴图存储顶点的字母名称及其所有相邻顶点的向量。我得到了编译错误,这是因为我在访问地图中的向量元素时遇到了问题。不管怎样,这是我的密码。(n.weight我知道是错误的,因为它是迭代器,但我需要向量中该点内的邻居类来访问weight变量) void图形::mst(字符串开始){ u字串; if(顶点.查找(开始)!=顶点

我已经在这一问题上工作了三天,我被难住了。我们必须使用2个贴图(字符串、顶点(类)和字符串、向量)实现prim算法。第一个类在vertex类中存储一个字母作为名称、pi和键。第二个贴图存储顶点的字母名称及其所有相邻顶点的向量。我得到了编译错误,这是因为我在访问地图中的向量元素时遇到了问题。不管怎样,这是我的密码。(n.weight我知道是错误的,因为它是迭代器,但我需要向量中该点内的邻居类来访问weight变量)

void图形::mst(字符串开始){
u字串;
if(顶点.查找(开始)!=顶点.结束(){
对于(std::map::iterator it=vertices.begin();it!=vertices.end();++it){
it->second.key=100;
it->second.pi=“NIL”;
}
顶点[start].key=0;
对于(std::map::iterator it=vertices.begin();it!=vertices.end();++it){
minQ.insert(it->first,it->second.key);
}
u=minQ.extractMin();
而(u!=“空”){

看起来您只是在单向添加边,但此算法适用于无向图。您需要在两端的邻接列表中添加边。您可能不应该将行号与代码一起发布,以防有人试图在那里复制粘贴.oo ty以获取建议kennythanks kenny以供您输入。在我没有发布的mst.cpp中(很抱歉没有澄清),我有一个readGraph函数,在readGraph函数中,它调用addVertex一次(在一个while循环中直到行尾)和addEdge两次(在一个while循环中确定文件的结尾)。其中一个函数的参数为“to”和“from”第二次使用相反的值。这是有效的还是最好在函数本身内执行?void MSTapp::readGraph(){string顶点;string from;string to;int weight;while(cin>>顶点){if(cin.peek()!='\n'){myGraph.addVertex(顶点);}else{myGraph.addVertex(顶点);break;}}while(cin>>from){cin>>到;cin>>权重;myGraph.addEdge(from,to,weight);myGraph.addEdge(to,from,weight);}myGraph.mst(“A”);}
void Graph::mst(string start){
     string u;
     if(vertices.find(start) != vertices.end()){

        for (std::map<string,Vertex>::iterator it=vertices.begin(); it!=vertices.end(); ++it){
           it->second.key = 100;
           it->second.pi = "NIL";
        }
        vertices[start].key = 0;

        for (std::map<string,Vertex>::iterator it=vertices.begin(); it!=vertices.end(); ++it){
           minQ.insert(it->first, it->second.key);
        }
        u = minQ.extractMin();
        while(u != "empty"){
           cout << u << " " << vertices[u].pi << " " << vertices[u].key <<endl;
           for (std::vector<Neighbor>::iterator v = adjList.find(u)->second.begin();
                 v!=adjList.find(u)->second.end(); ++v){
              if(minQ.isMember(v->name) && vertices[u].key < (problem here)){


              }
           }
        }  

        u = minQ.extractMin();
     }  
  }