图的存储_邻接表_前向星

//***********************储存***********************

struct node {
    int nxt;  // 相连的点。
    int val;
    int lst;  // 上一条同一起点的边的编号
    node () {}
    node (int next, int value) : nxt(next), val(value) {}
};

node edge[maxn];
int  last[maxn];  // last[i]表示以i为起点的最后一条边的编号。通常初始化为-1

int a, b, c;
for (i=1; i<=q; ++i) {
    sf("%d%d%d", &a, &b, &c);
    edge[i].nxt = b;
    edge[i].val = c;
    edge[i].lst = last[a];
    last[a] = i;
}

//********************遍历**************************

for (int i=1; i<=n; ++i) {
    for (int u = last[now.i]; u!=-1; u=edge[u].lst) {
        // u为以i为起点的一条边的编号。
    }
}

//邻接表用Vector则是很方便的 edge[a].push_back(node(b, c));
原文地址:https://www.cnblogs.com/cgjh/p/9043891.html