邻接表存图
思想
将每个节点作为一个表头,构建了n个单链表1
2
3
4
5
6
7
8
9
10
11int n, m, tot; //n个点,m条边,tot为边的个数
int edge[M], ver[M], Next[M]; //边权值,顶点,接着的边(tot序号)
int d[N], head[N]; //每个点的最短距离,链表头
bool v[N]; //访问标记
void add(int x, int y, int z) //起点,终点,边权值
{
ver[++ tot] = y; //终点
edge[tot] = z; //这个边的边权值
Next[tot] = head[x]; //由于从链表头开始插入,则表头存这个边的序号
head[x] = tot; //这边链接的下一个连接原来的表头,最后传递遍历到0,表示结束
}