码迷,mamicode.com
首页 > 其他好文 > 详细

Floyd

时间:2019-11-23 20:03:59      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:ges   ++   typedef   rap   lse   struct   max   div   lang   

各队顶点之间的最短路径及其路径长度

typedef int InfoType;
typedef struct {
	int edges[MAXV][MAXV];
	InfoType vex[MAXV];
	int n, e;
}MatGraph;

  

void Dispath(MatGraph g, int A[][MAXV], int path[][MAXV])
{
    int apath[MAXV], d;
    int i, j, k;
    for (i = 0; i < g.n; i++)
    {
        for (j = 0; j < g.n; j++)
        {
            if (A[i][j] < INF && i != j)
            {
                printf("从%d到%d的路径为:", i, j);
                d = 0;
                apath[d] = j;
                k = path[i][j];
                while (k != -1 && k != i)
                {
                    apath[++d]=k;
                    k = path[i][k];
                }
                apath[++d] = i;
                printf("%d", apath[d]);
                for (k = d - 1; k >= 0; k--)
                    printf(",%d", apath[k]);
                printf("\t路径长度为:%d\n", A[i][j]);
            }
        }
    }
}

  

void Floyd(MatGraph g)
{
    int A[MAXV][MAXV];
    int path[MAXV][MAXV];
    int i, j, k;
    for (i = 0; i < g.n; i++)
    {
        for (j = 0; j < g.n; j++)
        {
            A[i][j] = g.edges[i][j];
            if (i != j && g.edges[i][j] < INF)
                path[i][j] = i;
            else
                path[i][j] = -1;
        }
    }
    for (k = 0; k < g.n; k++)
    {
        for (i = 0; i < g.n; i++)
        {
            for (j = 0; j < g.n; j++)
            {
                if (A[i][j] > A[i][k] + A[k][j])
                {
                    A[i][j] = A[i][k] + A[k][j];
                    path[i][j] = path[k][j];
                }
            }
        }
    }
    Dispath(g,A,path);
}

测试数据

3 5

0 4 11

6 0 2

3 100 0

Floyd

标签:ges   ++   typedef   rap   lse   struct   max   div   lang   

原文地址:https://www.cnblogs.com/KIROsola/p/11919360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!