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

稠密图(邻接矩阵)

时间:2015-08-27 10:49:30      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
//稠密图,邻接矩阵表示
struct edge{
    int v, w, val;
    edge(int v = -1, int w = -1, int val = NOEDGE) :v(v), w(w), val(val){}
};
struct denseGraph{
    int Vcnt, Ecnt;    //顶点数,边数
    bool dg;    //有向图 ? 
    vector< vector<int> > adj;    //邻接矩阵

    denseGraph(int v, bool dg = false) :adj(v), Vcnt(v), Ecnt(0), dg(dg){
        for (int i = 0; i < v; ++i)
            adj[i].assign(v, NOEDGE);
    }

    void insert(edge e){
        int v = e.v, w = e.w, val = e.val;
        if (adj[v][w] == NOEDGE) ++Ecnt;
        adj[v][w] = val;
        if (!dg) adj[w][v] = val;
    }

    void show(){
        printf("Vcnt = %d, Ecnt = %d, Directed : ", Vcnt, Ecnt);
        if (dg) printf("true\n");
        else printf("false\n");
        for (int i = 0; i < Vcnt; ++i){
            for (int j = 0; j < Vcnt-1; ++j)
                cout << adj[i][j] <<  ;
            cout << adj[i][Vcnt - 1] << endl;
        }
    }
};
View Code

 

稠密图(邻接矩阵)

标签:

原文地址:http://www.cnblogs.com/jokoz/p/4762572.html

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