【笔记】图论

链式前向星

能够链式遍历以同一个节点为起点的所有边,后加入的先遍历到

变量定义

struct Edge{
    int v,w,nex;
}edge[M];
int head[N],cnt;

加边操作

void add(int u,int v,int w){
    edge[cnt].v = v;edge[cnt].w = w;
    edge[cnt].nex = head[u];head[u] = cnt++;
}

遍历u节点为起点的所有边

for(int i=head[u];~i;i=edge[i].nex){
    do_something(u,edge[i].v,edge[i].w);
}

猜你喜欢

转载自www.cnblogs.com/greenty1208/p/9454805.html
今日推荐