版本比较
链式前向星
正文
增加 7 行删除 7 行
5 行未改动6789101011121314111 行未改动全部展开1261271281291301311301311321331341351361371381381391401411429 行未改动15215315415515615615715815815916016116219 行未改动1821831841851861861871881891908 行未改动5 行未改动
在链式前向星存图中,我们需要定义一个结构体:```struct EDGE struct EDGE{ int next; int to;}edge[1000000];111 行未改动全部展开代码:(带权值)```#include<iostream>using namespace std;struct edge { struct edge{ int next; int to; int wei;}edge[MAXM];int head[MAXN];//head[i]为i点的第一条边int cnt=0;void addedge(int u,int v,int w) //起点,终点,权值 void addedge(int u,int v,int w) //起点,终点,权值{ edge[++cnt].next=head[u];//更新cnt edge[cnt].to=v; edge[cnt].w=w;9 行未改动 //如果是无向图,还要addedge(b,a,wei); }}```
#### 注意:
这里的next指的是遍历时的下一条边,head指的是遍历时的第一条边,而存边时相当于反过来操作,所以next记录上一条边,而head记录最后一条边。
19 行未改动
## 补充+重点 (2019/1/15update) ``` void addedge(int u,int v,int w) //起点,终点,权值 void addedge(int u,int v,int w) //起点,终点,权值{ edge[++cnt].next=head[u];//更新cnt edge[cnt].to=v; edge[cnt].w=w;8 行未改动