图的应用----最短路径(C语言+&引用)

test.cpp

#define _CRT_SECURE_N0_WARNINGS 1

#pragma warning (disable:4996)

# include 
# include 
using namespace std;

# define MAX_LENGTH 1024
# define MAX_VERTEX_NUM 20
# define FALSE 0
# define TRUE 1
typedef enum{
    DG, DN, UDG, UDN
} GraphKind;
typedef int VRType;
typedef int InfoType;
typedef int VertexType;
typedef int PathMatrix;
typedef int ShortPathTable;

typedef struct ArcCell
{
    VRType adj;
    InfoType info;
}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

typedef struct
{
    VertexType vexs[MAX_VERTEX_NUM];
    AdjMatrix  arcs;
    int vexnum, arcnum;
    GraphKind kind;
}MGraph;

int CreateDN(MGraph &G)   //构建有向网G
{
    int i = 0, j = 0, k, vi, vj, w;
    printf("请输入有向网的顶点数:");
    scanf("%d", &G.vexnum);
    printf("请输入有向网的弧数:");
    scanf("%d", &G.arcnum);

    for (i = 0; i < G.vexnum; ++i)
    for (j = 0; j < G.vexnum; ++j)
    {
        G.arcs[i][j].info = MAX_LENGTH;
    }

    printf("请输入每一条边依附的顶点及权值-->\n");
    for (k = 0; k < G.arcnum; ++k)
    {
        printf("第%d条边的vi:", k + 1);
        scanf("%d", &vi);
        printf("第%d条边的vj:", k + 1);
        scanf("%d", &vj);
        printf("第%d条边的权值:", k + 1);
        scanf("%d", &w);

        //检验顶点和该边权值的合法性
        i = vi;
        j = vj;
        while (i<0 || i>G.vexnum - 1 || j<0 || j>G.vexnum - 1 || w < 0 || w >= MAX_LENGTH)
        {
            printf("输入非法!\n");
            printf("请重新输入-->\n");
            printf("第%d条边的vi:", k + 1);
            scanf("%d", &vi);
            printf("第%d条边的vj:", k + 1);
            scanf("%d", &vj);
            printf("第%d条边的权值:", k + 1);
            scanf("%d", &w);

            i = vi;
            j = vj;
        }

        G.arcs[i][j].info = w;

    }
    return true;
}

void PrintDN(MGraph G)
{
    printf("---------------------------------------\n");
    printf("顶点  ");
    for (int i = 0; i
image.png

你可能感兴趣的:(图的应用----最短路径(C语言+&引用))