zoukankan      html  css  js  c++  java
  • 链式前向星模板

    整理一下,按边存储的结构都可以用此存储。

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

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

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

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

    边是反向连接的,head[u]指向u连接的最后一条边的e索引,而nxt指的是e数组中的索引。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cstdlib>
     5 #include<iostream>
     6 #include<cmath>
     7 using namespace std;
     8 typedef long long ll;
     9 const int maxn=1e5+3;
    10 struct Edge{
    11     int nxt;
    12     int to;
    13     int w;
    14 }e[maxn];
    15 int head[maxn],cnt;
    16 void add_edge(int u,int v,int w){//单向
    17     e[cnt].w=w;
    18     e[cnt].to=v;
    19     e[cnt].nxt=head[u];
    20     head[u]=cnt++;
    21 }
    22 int main(){
    23     int u=1;
    24     for(int i=head[u];i!=-1;i=e[i].nxt){
    25         
    26     }
    27 }
  • 相关阅读:
    Hive安装教程
    HBase安装教程
    Hadoop集群搭建
    Redis集群安装详细步骤
    Python绘图工具turtle库的使用
    python程序语法元素分析
    Selenium请求库爬取京东商品实例
    python爬虫入门
    python入门
    pytest fixture场景一:参数传入
  • 原文地址:https://www.cnblogs.com/elpsycongroo/p/7413180.html
Copyright © 2011-2022 走看看