zoukankan      html  css  js  c++  java
  • 链式前向星-邻接表--模板

    http://blog.csdn.net/acdreamers/article/details/16902023(原文链接,因为太好了,所以copy过来直接收藏)

    我们首先来看一下什么是前向星.

    前向星是一种特殊的边集数组,我们把边集数组中的每一条边按照起点从小到大排序,如果起点相同就按照终点从小到大排序,

    并记录下以某个点为起点的所有边在数组中的起始位置和存储长度,那么前向星就构造好了.


    用len[i]来记录所有以i为起点的边在数组中的存储长度.

    用head[i]记录以i为边集在数组中的第一个存储位置.

    那么对于下图:

     

    我们输入边的顺序为:

    1 2

    2 3

    3 4

    1 3

    4 1

    1 5

    4 5

    那么排完序后就得到:

    编号:     1      2      3      4      5      6      7

    起点u:    1      1      1      2      3      4      4

    终点v:    2      3      5      3      4      1      5

    得到:

    head[1] = 1    len[1] = 3

    head[2] = 4    len[2] = 1

    head[3] = 5    len[3] = 1

    head[4] = 6    len[4] = 2

    但是利用前向星会有排序操作,如果用快排时间至少为O(nlog(n))

    如果用链式前向星,就可以避免排序.

    我们建立边结构体为:

    struct Edge

    {

         int next;           //该点为起点的下一条边的位置;

         int to;               //终点;

         int w;               //边权;

    };

    其中edge[i].to表示第i条边的终点,edge[i].next表示与第i条边同起点的下一条边的存储位置,edge[i].w为边权值.

    另外还有一个数组head[],它是用来表示以i为起点的第一条边存储的位置,实际上你会发现这里的第一条边存储的位置其实

    在以i为起点的所有边的最后输入的那个编号.

    head[]数组一般初始化为-1,对于加边的add函数是这样的:


     

    1 cnt是全局变量,用来记录当前是第几条边;初始化为0;
    2 
    3  void add(int from,int to,int w)
    4 {
    5     edge[cnt].w=w;    
    6     edge[cnt].to=to;
    7     edge[cnt].next=head[from];
    8     head[from]=cnt++;        //更新起始边的位置;
    9 }
    View Code

      

    初始化cnt = 0,这样,现在我们还是按照上面的图和输入来模拟一下:

    edge[0].to = 2;     edge[0].next = -1;      head[1] = 0;

    edge[1].to = 3;     edge[1].next = -1;      head[2] = 1;

    edge[2].to = 4;     edge[2],next = -1;      head[3] = 2;

    edge[3].to = 3;     edge[3].next = 0;       head[1] = 3;

    edge[4].to = 1;     edge[4].next = -1;      head[4] = 4;

    edge[5].to = 5;     edge[5].next = 3;       head[1] = 5;

    edge[6].to = 5;     edge[6].next = 4;       head[4] = 6;

    这样在遍历时是倒着遍历的,也就是说与输入顺序是相反的,不过这样不影响结果的正确性.

    比如以上图为例,以节点1为起点的边有3条,它们的编号分别是0,3,5   而head[1] = 5

    我们在遍历以u节点为起始位置的所有边的时候是这样的:

    for(int i=head[u];~i;i=edge[i].next)

    那么就是说先遍历编号为5的边,也就是head[1],然后就是edge[5].next,也就是编号3的边,然后继续edge[3].next,也

    就是编号0的边,可以看出是逆序的.

    ***************************************************

    邻接表:

    n个结点;

    n个vector 中保存结构体;

    vector的序号表示边的起点,同一个起点的边在同一个vector中;

    结构体中记录终点和距离;

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<vector>
     6 #define N 100
     7 using namespace std;
     8 
     9 struct Node
    10 {
    11     int to , dis;
    12 
    13     Node(int a,int b)
    14     {
    15         to=a;
    16         dis=b;
    17     }
    18     bool operator < (const Node & A)const
    19     {
    20         if(dis== A.dis)
    21             return to < A.to;
    22         else
    23             return dis > A.d;
    24     }
    25 };
    26 
    27 vector< Node > from[N];
    28 int n,m;
    29 
    30 int main()
    31 {
    32     scanf("%d%d",&n,&m);
    33     for(int i=0;i<m;i++)
    34     {
    35         int a,b,d;
    36         scanf("%d%d%d",&a,&b,&d);
    37         from[a].push_back(Node(b,d));
    38         from[b].push_back(Node(a,d));
    39     }
    40 
    41     return 0;
    42 }
  • 相关阅读:
    矩阵Frobenius范数、核范数与奇异值的关系
    范数与正则化
    对偶上升法,增光拉格朗日乘子法,交替方向乘子法
    拉格朗日函数,拉格朗日对偶函数,KKT条件
    relint, 相对内点集的理解
    转:Mac 弹出App can’t be opened because Apple cannot check it for malicious software的解决方法
    数组分块1
    fzu 2275 Game [第八届福建省大学生程序设计竞赛 Problem D]
    fzu 2275 Game [第八届福建省大学生程序设计竞赛 Problem D]
    Plug-in CodeForces
  • 原文地址:https://www.cnblogs.com/by-1075324834/p/4512793.html
Copyright © 2011-2022 走看看