怎么编译结果老是不对,我都对了3遍了
怎么办???
怎么办???
2016-08-20
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;
}
举报