图 数组表示法

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <conio.h>
#include <limits.h>

#define MAXN 256
#define INFINITY INT_MAX
#define MAX_VERTEX_NUM 20
#define OK 1

typedef int VRType;
typedef int InfoType;
typedef int VertexType;
typedef int Status;

typedef VRType AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef VRType ArcCell;
typedef int GraphKind;

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


int LocateVex(MGraph *G, VertexType v){
    int i;
    for(i=0; i<G->vexnum; i++)
        if(G->vexs[i] == v) return i;
}

Status CreateUDN(MGraph *G){
    int i,j,k,v1,v2,w;
    scanf("%d %d", &G->vexnum, &G->arcnum);
    for(i=0; i<G->vexnum; i++) scanf("%d", &G->vexs[i]);
    for(i=0; i<G->vexnum; i++)
        for(j=0; j<G->vexnum; j++) G->arcs[i][j] = INFINITY;
    for(k=0; k<G->arcnum; k++){
        scanf("%d %d %d", &v1, &v2, &w);
        i = LocateVex(G, v1); j = LocateVex(G, v2);
        G->arcs[i][j] = w;
        G->arcs[j][i] = G->arcs[i][j];
    }
    return OK;
}

void print(MGraph *G){
    int i,j;
    for(i=0; i<G->vexnum; i++){
        for(j=0; j<G->vexnum; j++){
            if(G->arcs[i][j] == INFINITY) printf("∞\t");
            else printf("%d\t", G->arcs[i][j]);
        }
        putchar('\n');
    }
}

int main(){
    freopen("d:\\my.txt", "r", stdin);
    MGraph G;
    CreateUDN(&G);
    print(&G);

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值