2.10--图(2)
链式前向星
向x的链表中加入指向y,边权为z的边
用抽象的胡话形象的记法就是
void add(int x,int y,int z){ to[++cnt] = y; //记姓名 v[cnt] = z; //记年龄 nxt[cnt] = head[x]; // 报名表中记前员
head[x] = cnt; //留言板上写序号 }
输出所有从x出发的边的终点,边权(姓名年龄)
for(int i = head[x];i;i=nxt[i]) printf("%d %d",to[i],v[i]);
图的遍历
dfs/bfs
void dfs(int x){ vis[x] = 1;printf("%d\n",x); for(int i = head[x];i;i=nxt[i]) if(!vis[to[i]]) dfs(to[i]); } void bfs(int x){ z[++top] = x; for(int i = 1;i <= top;i++){ int now = z[top++]; printf("%d\n",now); for(int j = head[now];j;j=nxt[j]){ if(!vis[to[j]]) vis[to[i]] = 1,bfs(to[i]); } } }
浙公网安备 33010602011771号