链式前向星

链式前向星:头插法的静态邻接链表(类似于头插法的邻接链表)

①int tot:用于计算边的编号

②head[MAX]:用来存储以i为起点的最新加入的边在edge[]中的存储位置

③struct Node{ int to; int w; int next}; 

to表示终点;w是权值;next表示下一条边在edge[]数组中的存储位置

Node edge[MAX]:用来存储边的信息

int head[maxn];    //存以i为起点的最近加入的一条边的存储位置 
int tot;        //边的编号 

struct Node{        //链式前向星 struct
    int to;        //边的终点     
    int w;        //权值 
    int next;    //相同起点的上一次加入的边的存储位置 (指向下一条边在edge数组中的位置)
}edge[maxn*2];

void init(){
    tot = 0;
    memset(head,-1,sizeof(head));    //将head初始化为-1 
}

void add_edge(int from, int to, int w){        //from起点, to终点, w权值 
    edge[tot].to=to;
    edge[tot].w=w;
    edge[tot].next=head[from]; //head[from]:上一次加入的边的位置 
    head[from]=tot++;          //更新以from为起点的最新加入的边的编号 
}
posted @ 2019-12-13 23:03  北冥有鱼兮  阅读(705)  评论(0编辑  收藏  举报