单源最短路径

#include<iostream>

#include<string>

using namespace std;

#define maxnum 100

#define maxlength 10000

typedef int ElemType;

typedef int WType;

typedef struct{

         ElemType tu[maxnum][maxnum];    //邻接矩阵

         string point[maxnum];          //顶点矩阵

         int point_num;                 //顶点个数

}Graph;

void input(Graph &G)

{

         cout<<"请输入图的顶点数:";

         cin>>G.point_num;

         cout<<"请输入顶点的信息:"<<endl;

         for(int j=0;j<G.point_num;j++)

                   cin>>G.point[j];

         cout<<"请输入图的邻接矩阵:"<<endl;

         for(int i=0;i<G.point_num;i++)

         {

                   for(int j=0;j<G.point_num;j++)

                            cin>>G.tu[i][j];

         }

}

 

void ShortPath(Graph &G,int v,WType dist[],int path[])//path[]存储v1点的前一个点,dist[]存储各个点的最短路径

{

         bool *s=new bool[G.point_num];

         int i,j,k;

         for(i=0;i<G.point_num;i++)//对长度和路径的初始化

         {

                   dist[i]=G.tu[v][i];

                   s[i]=false;

                   if((i!=v)&&(dist[i]<maxlength))

                            path[i]=v;

                   else

                            path[i]=-1;

         }

         s[v]=true;//顶点v加入s集合

         dist[v]=0;

         WType min,w;

         for(i=0;i<G.point_num-1;i++)//逐个求v到其余point_num-1个点的最短路径

         {

                   min=maxlength;

                   int u=v;                    //选不在s中的最短路径的顶点u

                   for(j=0;j<G.point_num;j++)

                            if(!s[j]&&dist[j]<min)

                            {

                                     u=j;

                                     min=dist[j];

                            }

                            s[u]=true;

                            for(k=0;k<G.point_num;k++)     //修改经过u到其他顶点的最短路径

                            {

                                     w=G.tu[u][k];

                                     if((!s[k])&&(w<maxlength)&&(dist[u]+w<dist[k]))

                                     {

                                               dist[k]=dist[u]+w;

                                               path[k]=u;           //修改到k的最短路径

                                     }

                            }

         }

}

 

 

void printShortPath(Graph&G,int v,WType dist[],int path[])

{

         cout<<"从顶点 "<<G.point[v]<<" 到其他各顶点的最短路径为:"<<endl;

         int i,j,k;

         int *d=new int[G.point_num];

         for(i=0;i<G.point_num;i++)

                   if(i!=v)

                   {

                            j=i; k=0;

                            if(dist[i]==maxlength)

                            {

                                     cout<<"源点"<<G.point[v]<<"到"<<G.point[i]<<"之间无路径"<<endl;

                            }

                            else

                            {

                                     while(j!=v)//起点没有在d【k】数组里

                                     {

                                               d[k++]=j;//第一次j是中点

                                               j=path[j];

                                     }

                                     cout<<"顶点"<<G.point[i]<<"的最短路径为:"<<G.point[v]<<" ";

                                     while(k>0)

                                     {

                                               cout<<G.point[d[--k]]<<" ";

                                              

                                     }

                                     cout<<"\t\t最短路径的长度为:"<<dist[i]<<endl;

                            }

                   }

                   delete[]d;

}

void main()

{

         Graph G;

         WType dist[maxnum];

         int path[maxnum];

         input(G);

         cout<<"请输入起点(对应的数据下标):"<<endl;

         int v;

         cin>>v;

         ShortPath(G,v,dist,path);

         printShortPath(G,v,dist,path);

         system("pause");

}

 

 

/*

4

V0 V1 V2 V3

0     1     10000 4

10000 0     9     2

3     5     0     8

10000 10000 6     0

 

0

*/

/*

6

V0 V1 V2 V3 V4 V5   

10000 10000 10    10000 30    100

10000 10000 5     10000 10000 10000

10000 10000 10000 50    10000 10000

10000 10000 10000 10000 10000 10

10000 10000 10000 20    10000 60

10000 10000 10000 10000 10000 10000

                  

0

                    

*/

posted @ 2012-11-22 22:57  ♂咱說 ろ算  阅读(253)  评论(0编辑  收藏  举报