日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

图存储-前向星

發(fā)布時(shí)間:2025/7/14 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图存储-前向星 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
//前向星是將所有的邊進(jìn)行編號(hào),每個(gè)節(jié)點(diǎn)u的邊集合通過head[u]來找到u的第一條邊, //再通過next[head[u]]依次遍歷節(jié)點(diǎn)u的所有邊。 int head[maxn]; int to[maxn*2]; int next[maxn*2]; int cnt = 0;//邊的編號(hào) memset(head, -1, sizeof(head));inline void add(int x,int y){ to[cnt]=y,next[cnt]=head[x],head[x]=cnt++; to[cnt]=x,next[cnt]=head[y],head[y]=cnt++;}inline void dfs(int u) {int i;//從節(jié)點(diǎn)u的第一條邊開始,遍歷與u相連的所有邊for(i=head[u];i!=-1;i=next[i]) { dfs(to[i]);} } /* head[i]: 以i為節(jié)點(diǎn)的邊集的第一條邊編號(hào) next[i]:編號(hào)為i的邊集中的下一條邊編號(hào),特定節(jié)點(diǎn)u的邊的編號(hào)連成一個(gè)鏈表 to[i]:編號(hào)為i的邊的終點(diǎn) */

?



//另一種實(shí)現(xiàn)
#include <iostream> #include <cstdio> using namespace std; const int maxn = 100; const int maxm = 100000; typedef struct edgenode {int to; //邊的終點(diǎn)int next; //當(dāng)前下一條邊的編號(hào)int w; //邊的權(quán)值 }edgenode; int head[maxn]; //head[i]存放已i為起點(diǎn)的第一條邊 edgenode edge[maxm]; int edgenum = 1; int n = 0, m = 0; int init() {edgenum = 1;memset(head, 0, sizeof(head));//chu shi hua 0;return 0; } int outputmap(){for (int i = 1; i <= n; i++) {for (int k = head[i]; k != 0; k = edge[k].next) {printf("(%d --- > %d) == %d\n", i, edge[k].to, edge[k].w);}}return 0; } int main() {init();int a = 0, b = 0, c = 0;while (scanf("%d%d", &n, &m) == 2) {for (int i = 0; i < m; i++) {scanf("%d%d%d", &a, &b, &c);edge[edgenum].to = b;edge[edgenum].w = c;edge[edgenum].next = head[a];head[a] = edgenum;edgenum++;edge[edgenum].to = a;edge[edgenum].w = c;edge[edgenum].next = head[b];head[b] = edgenum;edgenum++;}outputmap();init();}return 0; }

?


轉(zhuǎn)載于:https://www.cnblogs.com/OUSUO/p/3805699.html

總結(jié)

以上是生活随笔為你收集整理的图存储-前向星的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。