链式前向星

看了网络流,别人都是用链式前向星写。。。没学过的我真的看不懂

这就回来补了补

用这个结构是因为邻接矩阵的太浪费空间,占内存,用这个高效快速

struct node{
    int to,w,next;//to连着的下个点,w为权值,next为同起点的上一条边 
};
int head[maxn];//为这个起点的最后一条边 
int num=0;//
void add(int from,int to,int w)
{
    node[num].to=to;
    node[num].w=w;
    node[num].next=head[from];
    head[from].num++;
}

我一开始是看懵逼了 手动画画才理解

 

咳咳。。图有点丑  将就着和下面的一起耐心看看就应该能理解

1->2
node[0].to=2;
node[0].next=head[1]=-1;
node[0].w=1;
head[1]=1; 

1->3
node[1].to=3;
node[1].next=head[1]=1;
node[1].w=1;
head[1]=2; 


1->4
node[2].to=4;
node[2].next=head[1]=2;
node[2].w=1;
head[1]=3; 


2->5
node[3].to=5;
node[3].next=head[2]=-1;
node[3].w=1;
head[2]=4; 

2->6
node[4].to=2;
node[4].next=head[1]=4;
node[4].w=1;
head[2]=5; 

 

posted @ 2018-08-22 09:37  踩在浪花上  阅读(171)  评论(0编辑  收藏  举报