Dijstra

#include  < iostream >

using   namespace  std;

#define  maxVertexNumber 100 
#define  Infinite 9999

unsigned 
int  VertexNumber;
struct  VertexType
{
    unsigned 
int  Distance;
    unsigned 
int  Father;
    
bool  BelongToFirstSet;
};

class  VertexSetType
{
    
public :
    VertexSetType();
    
~ VertexSetType(){};
    unsigned 
int  SelectMinimumDistance();
    
void  UnionToFirstSet(unsigned  int  U);
    
void  UpdateDistance(unsigned  int  U);
    
void  List(unsigned  int  U);
    
void  List();
    
private :
    
// unsigned int VertexNumber;
    VertexType Vertex[maxVertexNumber];
    
int  Cost[maxVertexNumber][maxVertexNumber];
    unsigned 
int  SourceVertex;
};

VertexSetType::VertexSetType()
{
    cout
<< " 请输入代价矩阵的行列数( " << " 小于 " << maxVertexNumber << " ): " ;
    cin
>> VertexNumber;
    cout
<< " 请输入代价矩阵( " << VertexNumber << " X " << VertexNumber << " ): " ;
    cout
<< endl;
    
for (unsigned  int  i = 0 ;i < VertexNumber;i ++ )
    {
    
for (unsigned  int  j = 0 ;j < VertexNumber;j ++ )
        cin
>> Cost[i][j];
    }
    
for (unsigned  int  i = 0 ;i < VertexNumber;i ++ )
    
for (unsigned  int  j = 0 ;j < VertexNumber;j ++ )
        
if (Cost[i][j] == 0 )
        Cost[i][j]
= Infinite;
    cout
<< " 请输入源点号: " ;
    cin
>> SourceVertex;
    
for (unsigned  int  i = 0 ;i < VertexNumber;i ++ )
    {
    Vertex[i].BelongToFirstSet
= false ;
    Vertex[i].Distance
= Cost[SourceVertex][i];
    Vertex[i].Father
= SourceVertex;
    }
    Vertex[SourceVertex].BelongToFirstSet
= true ;
    Vertex[SourceVertex].Distance
= 0 ;
}

unsigned 
int  VertexSetType::SelectMinimumDistance()
{
    unsigned 
int  Minimum,i,j;
    j
= 0 ;
    Minimum 
=  Infinite;
    
//     while((Vertex[j].BelongToFirstSet) && (j<VertexNumber))
    
//     j += 1;
     for (i = 0 ;i < VertexNumber;i ++ )
    
if (( ! Vertex[i].BelongToFirstSet)  &&  (Vertex[i].Distance  <  Minimum))
    {
        Minimum
= Vertex[i].Distance;
        j
= i;
    }
    
return  j;
}

void  VertexSetType::UnionToFirstSet(unsigned  int  U)
{
    Vertex[U].BelongToFirstSet
= true ;
}

void  VertexSetType::UpdateDistance(unsigned  int  U)
{
    unsigned 
int  W;
    
for (W = 0 ; W  <  VertexNumber; W ++ )
    
if ( ( ! Vertex[W].BelongToFirstSet)  &&  (Vertex[W].Distance  >  Vertex[U].Distance  +  Cost[U][W]) )
    {
        Vertex[W].Distance 
=  Vertex[U].Distance  +  Cost[U][W];
        Vertex[W].Father 
=  U;
    }
}

void  VertexSetType::List(unsigned  int  U)
{
    
if  ( U  ==  SourceVertex )
    cout 
<<   " ( "   <<  U  <<   " ) " ;
    
else
    {
    List( Vertex[U].Father );
    cout
<< " -- " <<  Vertex[U].Distance  -  Vertex[ Vertex[U].Father ].Distance  <<   " -- "   <<   " ( "   <<  U  <<   " ) " ;
    }
    
/*
       for(i=0;i<VertexNumber;i++)
       if( i != SourceVertex )
       cout << SourceVertex << "-->" << i << "===" << Vertex[i].Distance << endl;
*/
}

void  VertexSetType::List()
{
    
for (unsigned  int  i = 0 ;i < VertexNumber;i ++ )
    
if ( i  !=  SourceVertex )
    {
        List ( i );
        cout
<< endl;
    }
}

int  main()
{
    VertexSetType VertexSet;
    unsigned 
int  U;
    unsigned 
int  Count  =   2 ;
    
while  ( Count  <  VertexNumber )
    {
    U 
=  VertexSet.SelectMinimumDistance();
    VertexSet.UnionToFirstSet ( U );
    Count 
++  ;
    VertexSet.UpdateDistance ( U );
    }
    VertexSet.List();
    
return   0 ;
}

转载于:https://www.cnblogs.com/qzchenwl/archive/2008/11/10/1331001.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值