问答详情
源自:4-4 图的编码实战-最小生成树之普利姆算法(四)

怎么编译结果老是不对,我都对了3遍了

http://img.mukewang.com/57b855950001f65d06770442.jpg怎么办???

提问者:慕粉3741271 2016-08-20 21:07

个回答

  • wonder_skye
    2018-07-18 09:54:46

    在主函数中设定边的时候,采用对无向图的赋值方法进行,setValueToMatrixForUndirectedGraph()

  • 精慕门4947531
    2016-11-10 17:18:26

    nodeVect.push_back(nodeIndex);

    m_pNodeArray[nodeIndex].m_bVisited=true;、、

    你的最小边都未找到,认真查;;;

    void DMap::primTree(int nodeIndex)

    {

    int value=0;

    int edgeCount=0;

    vector<int>nodeVect;

    vector<Edge> edgeVect;


    cout<<m_pNodeArray[nodeIndex].m_cData<<endl;


    nodeVect.push_back(nodeIndex);

    m_pNodeArray[nodeIndex].m_bVisited=true;

    //*****************************************************

    while(edgeCount < m_iCapacity-1)

    {

    int temp=nodeVect.back();

    for(int i=0;i<m_iCapacity;i++)

    {

    getValueFromMatrix(temp,i,value);

    if(value!=0)

    {

    if(m_pNodeArray[i].m_bVisited)

    {

    continue;

    }

    else

    {

    Edge edge(temp,i,value);

    edgeVect.push_back(edge);

    }

    }

    }

    //从可选边集合找出最小边

    int edgeIndex=getMinEdge(edgeVect);

    edgeVect[edgeIndex].m_bSelected=true;


    cout<<edgeVect[edgeIndex].m_iNodeIndexA<<"-------"<<edgeVect[edgeIndex].m_iNodeIndexB<<"   ";

    cout<<edgeVect[edgeIndex].m_iWeightValue<<endl;


    m_pEdge[edgeCount]=edgeVect[edgeIndex];

    edgeCount++;


    int nextNodeIndex=edgeVect[edgeIndex].m_iNodeIndexB;

    nodeVect.push_back (nextNodeIndex);

    m_pNodeArray[nextNodeIndex].m_bVisited=true;

    cout<<m_pNodeArray[nextNodeIndex].m_cData<<endl;

    }

    }


    int   DMap::getMinEdge(vector<Edge> edgeVect)

    {

    int minWeight=0;

    int edgeIndex=0;

    int i=0;

    for(;i<(int)edgeVect.size();i++)

    {

    if(!edgeVect[i].m_bSelected)

    {

    minWeight=edgeVect[i].m_iWeightValue;

    edgeIndex=i;

    break;

    }

    }


    if(minWeight==0)

    {

    return -1;

    }


    for(;i<(int)edgeVect.size();i++)

    {

    if(edgeVect[i].m_bSelected)

    {

    continue;

    }

    else

    {

    if(minWeight>edgeVect[i].m_iWeightValue)

    {

    minWeight=edgeVect[i].m_iWeightValue;

    edgeIndex=i;

    }

    }

    }

    return edgeIndex;

    }


  • gwl002
    2016-09-20 18:56:07

    没问题的,你对错了。