图的邻接表存储

图的邻接表存储

struct Edge
{
    int v;
    ll w;
    Edge *next;
};Edge e[maxn*10];

void add_edge(int u,int v,ll w) ///插入邻接表的首部而非尾部,避免遍历
{
    Edge *pre=&e[u];
    Edge *p=(Edge*)malloc(sizeof(Edge));
    p->v=v;p->w=w;
    p->next=pre->next;
    pre->next=p;
}
    //遍历
        for(Edge *p=e[u].next;p!=NULL;p=p->next)//遍历结点u的每条出边
View Code

 

posted @ 2015-04-09 17:42  __560  阅读(165)  评论(0编辑  收藏  举报