zoukankan      html  css  js  c++  java
  • 图论加边算法--链式向前星

    很多图论的算法都有一个函数

    struct Edge {
        int to;
        int w;
        int next;
    } edge[N * 2];
    
    int cnt_edge = 0;
    
    void add_edge(int from, int to, int w)
    {
        edge[cnt_edge].to = to;
        edge[cnt_edge].w = w;
        edge[cnt_edge].next = head[from];
        head[from] = cnt_edge++;
    }
    
    memset(head, -1, sizeof head);

    我纠结了好久这个函数的意思……

    实际上是用邻接表存一个图。

    cnt_edge是给每一个边标号,从0开始。

    edge[i].to 表示第i条边指向哪个点,edge[i].next表示第i条边的下一条边的序号。

    head[from]表示以第from为初始结点的边的序号。

    例如图(随便画的= =#):

    存在边1,3   1,4   2,5   2,6   4,3   5,3  六条边

    依次调用函数add_edge之后,可以得到

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

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

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

    edge[3].to=6; edge[3].next=2; head[2]=3;

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

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

    用图来表示就是:

    喵。箭头的方向该怎么画呢。

    虽然vector建图会方便许多,但是碰到恶心的卡stl的题就gg了。所以还是要学一下链表建图。

    多写几次就很熟练了。

  • 相关阅读:
    线性表ADT实现
    基数排序
    二叉树之已知前序和中序遍历求后序遍历(POJ2255 &&HDU )
    acm头文件
    快排
    快读
    二分
    数据结构大师
    AC_2. 01背包问题
    AC_94. 递归实现排列型枚举
  • 原文地址:https://www.cnblogs.com/wenruo/p/4680930.html
Copyright © 2011-2022 走看看