zoukankan      html  css  js  c++  java
  • 校园导游图的课程设计(二)

    周二:

    主要完成了 两个任务

        一:全部简单路径的查找

        二 : 最短路径的查找

    一:简单路径:顶点序列中顶点不重复出现的路径。

         方法是 递归 和 简单回溯,但是没看懂,为了赶进度,只能先用这了,附上链接,用的是c++ 和矩阵存储

        我将代码改了,用的是 c 和 邻接表

    void ShortestEasyPath( ListMatrix * G, int star, int end, int path[],int d,int  allpath[][NUMMAX],int* count )
    {//d初始化为0,标记路的长度      allpath 用来存放走过的路径    count 用来标记存在那一行
            int m,i;
            ArcNode *p;
    
            visited[star] = 1; // 标记已访问
            d++;//长度加一
            path[d] = star;//路径符号进入
            if( star == end )
            {
                    (*count)++;
                    allpath[*count][0] = d;
                    for( i=1; i<=d; i++ )
                           allpath[*count][i] = path[i];
            }//开始等与结束时,输出路径
            p =G->vertex[star].next;
    
            while( p!=NULL )
            {
                    m = p->adjvex;
                    if(!visited[m])
                        ShortestEasyPath(G,m,end,path,d,allpath,count);
                    p = p->next;
            }
            visited[star]=0;//访问回溯遍历,这是代码核心,但看不懂
    }

    二:最短路径,使用的是dijkstra的方法,不用floyd是因为路径不好存储,唯一的特色是建立在 邻接表, 而不是一般人用的邻接邻接矩阵

    void ShortestPath( ListMatrix *G,int start, int dist[], int path[][NUMMAX])
    {//path 用来存储路径 
            int mindist;
            int i,j,k;
            int t = 1;
            ArcNode * p;
    
    
            for( i=1; i<=G->vexnum; i++ )
            {
                    dist[i] = INFNITY; 
            }
            p = G->vertex[start].next;
            while( p!=NULL )
            {
                    dist[p->adjvex] = p->weight;
                    path[p->adjvex][1] = start;//任一个和start相连的点,第一个必为 start
                    p = p->next;
            }
    
            path[start][0] = 1;//进入v集
    
            for( i=2; i<=G->vexnum; i++ )
            {
                    mindist = INFNITY;//初始化
    
                    for( j=1; j<=G->vexnum; j++ )
                    {
                            if(!path[j][0]&&dist[j]<mindist)
                            {
                                    k = j;
                                    mindist = dist[j];
                            }
                    }//寻找最短的哪一个k
                    
                    if( mindist == INFNITY ) return;
                     
                    path[k][0] = 1;//入V集
    
                    p = G->vertex[k].next;//查看他连通几个点
                    while( p!=NULL )
                    {
                            if( mindist+p->weight < dist[p->adjvex] )//是否满足变短
                            {
                                    dist[p->adjvex] = mindist+p->weight;//改变dist的值
    
                                    t = 1;
                                    while( path[k][t] != 0 )
                                    {
                                            path[p->adjvex][t] = path[k][t];
                                            t++;
                                    }
    
                                    path[p->adjvex][t] = k;
                                    path[p->adjvex][t+1] = 0;//将到从start点到p->adjvex 点的路径放入 对应的path[p->adjvex][]
                            }
    
                            p = p->next;
                    }//while 判断与k 相连的节点,是否可以通过k,缩短路径长度
    
            }//for 循环 n-1 次找到n-1的最短路径
    }
  • 相关阅读:
    最大子数组问题(分治策略实现)
    Solving the Detached Many-to-Many Problem with the Entity Framework
    Working With Entity Framework Detached Objects
    Attaching detached POCO to EF DbContext
    如何获取qq空间最近访问人列表
    Health Monitoring in ASP.NET 2.0
    problem with displaying the markers on Google maps
    WebMatrix Database.Open… Close() and Dispose()
    Accessing and Updating Data in ASP.NET: Retrieving XML Data with XmlDataSource Control
    Create web setup project that has crystal reports and sql script run manually on client system
  • 原文地址:https://www.cnblogs.com/dilidingzhi/p/4154386.html
Copyright © 2011-2022 走看看