首页 > 其他 > 详细

采用邻接矩阵表示法创建无向网

时间:2019-11-17 20:10:25      阅读:266      评论:0      收藏:0      [点我收藏+]
//采用邻接矩阵表示法创建无向网
#include <iostream>
using namespace std;

#define MaInt 32767
#define MVNum 100
#define OK 1

typedef char VerTextType;
typedef int ArcType;

typedef struct {
    VerTextType Vexs[MVNum];
    ArcType arcs[MVNum][MVNum];
    int vexnum, arcnum;
}AMGrach;

int LocateVex(AMGrach G, VerTextType v) {
    for (int i = 0;i < G.vexnum;i++) {
        if (G.Vexs[i] == v)
            return i;
    }
    return -1;
}

int CreateUDN(AMGrach& G) {
    int i, j, k;
    cout << "请输入总顶点数,总边数,以空格隔开:";
    cin >> G.vexnum >> G.arcnum;
    cout << endl;

    cout << "输入点的名称,如a" << endl;
        for (i = 0;i < G.vexnum;i++) {
            cout << "input the " << i << " name";
            cin >> G.Vexs[i];
        }
    cout << endl;

    for (i = 0;i < G.vexnum;i++)
        for (j = 0;j < G.vexnum;++j)
            G.arcs[i][j] = MaInt;
    cout << "输入边依附的顶点及权值,如 a b 5" << endl;

    for (k = 0;k < G.arcnum;++k) {
        VerTextType v1, v2;
        ArcType w;
        cout << "input the" << (k + 1) << " side of weigth";
        cin >> 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 0;
}

int main() {
    cout << "采用邻接矩阵表示法创建无向网";
    AMGrach G;
    int i, j;
    CreateUDN(G);
    cout << endl;
    for (i = 0;i < G.vexnum;i++) {
        for (j = 0;j < G.vexnum;++j) {

            if (j != G.vexnum - 1) {

                if (G.arcs[i][j] != MaInt) {
                    cout << G.arcs[i][j] << "\t";
                }
                else {
                    cout << "~" << "\t";
                }

            }
            else {

                if (G.arcs[i][j] != MaInt)
                    cout << G.arcs[i][j] << endl;
                else
                    cout << "~" << endl;
            }

        }
    }
    cout << endl;
    return 0;
}

采用邻接矩阵表示法创建无向网

原文:https://www.cnblogs.com/ygjzs/p/11877585.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!