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

图的几种存储方式

时间:2014-11-24 22:36:56      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:style   ar   color   sp   for   数据   on   div   bs   

1,     邻接矩阵:

struct MGraph
 {
     int ver[Max];
     int arc[Max][Max];
     int vers;
     int Eges;
};

int Locate(MGraph G,int x)
{
    for(int i=1;;i++)
        if(G.ver[i]==x)  return i;
}

void CreateUDN_MG(MGraph &G)
{
    cin>>G.vers>>G.Eges;
    for(int i=1;i<=G.vers;i++)
       cin>>G.ver[i];
    for(int i=1;i<=G.vers;i++)
        for(int j=1;j<=G.vers;j++)
           G.arc[i][j]=-1;
    for(int k=0;k<G.Eges;k++)
    {
        int v1,v2,w;
        cin>>v1>>v2>>w;
        int i=Locate(G,v1);
        int j=Locate(G,v2);
        G.arc[i][j]=w;
    }


数据:

6 10
1 2 3 4 5 6
1 2 5
1 4 7
6 1 3
3 1 8
2 3 4
4 3 5
4 6 6
5 4 5
6 5 1
3 6 9



图的几种存储方式

标签:style   ar   color   sp   for   数据   on   div   bs   

原文地址:http://blog.csdn.net/u013514722/article/details/41452305

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