/* 链式前向星(静态建邻接表) 2014-4-2 09:49:16 */ #include <iostream> #include <cstdlib> #define MAX 10000 using namespace std; int head[MAX]; //存储节点数组中起点为i的位置,0为结束标记 struct Node{ int to, w, next; //起点,权值,下一点 }; Node Edges[MAX]; int main(){ int n, m; //n个点,m条边 int to, from, w; //存储 cin >> n >> m; for(int i = 0; i < m; ++i){ cin >> from >> to >> w; Edges[i].to = to; Edges[i].w = w; Edges[i].next = head[from]; head[from] = i; //更新 } //遍历 for(int i = 1; i <= n; ++i){ for(int j = head[i]; j != 0; j = Edges[j].next) cout << i << ‘ ‘ << Edges[j].to << ‘ ‘ << Edges[j].w << endl; } system("pause"); return 0; }
【链式前向星】(静态建邻接表),布布扣,bubuko.com
原文:http://blog.csdn.net/chang_mu/article/details/22782895