图的邻接数组表示法

2017-07-25 09:05:06 

writer:pprp

今天学的是图,邻接数组表示法。

邻接数组表示法是用一个n*n的数组表示一个具有n个顶点的图形。

数组的索引值表示顶点,以数组内容表示顶点议案的边是否存在边(1,代表有,0代表没有)

#include <iostream>

using namespace std;

int main()
{
    int n;
    cout << "The max number of map: "<<endl;
    cin >> n;
    int **a = new int*[n];
    for(int i = 0 ; i < n ; i++)
    {
        a[i] = new int[n];
    }
    int s,e;

    for(int i = 0 ; i < n ; i++)
        for(int j = 0; j < n ; j++)
            a[i][j] = 0;

    while(1)
    {
        cout <<"Please enter the start and end number" << endl;
        cin >> s >> e;
        if(s<0||e<0)
            break;
        if(s == e)
            cout <<"error"<<endl;
        if(s>n||e>n)
            cout <<"error"<<endl;
        a[s][e] = 1;
    }

    for(int i = 0 ; i < n ; i++)
    {
        for(int j = 0; j < n ; j++)
            cout << a[i][j]<<" ";
        cout << endl;
    }

    return 0;
}

 

posted @ 2017-07-25 09:22  pprp  阅读(811)  评论(0编辑  收藏  举报