链式前向星|前向星|STL中vector模拟链表(图的存储)
2013-03-27 22:27
253 查看
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1000; const int maxm = 1000000; int n, m; int head[maxn]; int num = 1; /*链式前向星 */ struct node { int to; int next; int w; }; node edge[maxm]; void output_map() { for(int i = 1; i <= n; i++) { for(int k = head[i]; k != -1; k = edge[k].next) { printf("%d->%d w = %d\n", i, edge[k].to, edge[k].w); } } } void init() { memset(head, -1, sizeof(head)); num = 1; } int main() { int a, b, w; while(scanf("%d%d", &n, &m) != EOF) { init(); for(int i = 1; i <= m; i++) { scanf("%d%d%d", &a, &b, &w); edge[num].to = b; edge[num].w = w; edge[num].next = head[a]; head[a] = num; num++; } output_map(); } return 0; } /* 前向星 */ #include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x7fffffff; const int maxn = 1000; const int maxm = 1000000; int head[maxn]; int n, m;///the numbers of edges and point struct node{ int from; int to; int w; }; node edge[maxn];//边 bool cmp(node a, node b) { if(a.from == b.from && a.to == b.to) { return a.w < b.w; } if(a.from == b.from) { return a.to < b.to; } return a.from < b.from; } int main() { cin >> n >> m; for(int i = 1; i <= m; i++) { cin >> edge[i].from >> edge[i].to >> edge[i].w; } sort(edge+1, edge+m+1, cmp); memset(head, -1, sizeof(head));///init head[]; for(int i = 1; i <= m; i++) { if(edge[i].from != edge[i-1].from) {///judge if the neibor edges has the same beginning point head[edge[i].from] = i;///确定起点为Vi的第一条边的位置。 } } for(int i = 1; i <= n; i++) { for(int k = head[i]; edge[k].from== i && k <= m; k++) { cout << edge[k].from << ' ' << edge[k].to << ' ' << edge[k].w << endl; } } return 0; } /* vector模拟链表 */ #include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int maxn = 1000; const int maxm = 1000000; struct node { int to; int w; }; int n, m; vector<node> a[maxn]; void output() { vector<node>::iterator k; for(int i = 1; i <= n; i++) { for(k = a[i].begin(); k != a[i].end(); k++) { node tmp = *k; cout << i << ' ' << tmp.to << ' ' << tmp.w << endl; } } } int main() { int n, m; node e; int i, j, w; for(int i = 1; i<= m; i++) { cin >> i >> j >> w; e.to = j; e.w = w; a[i].push_back(e); } output(); return 0; } /*************************** 测试样例: 5 8 1 2 2 1 3 3 2 4 7 2 5 12 3 5 10 4 5 9 3 2 6 3 4 1 喜爱编程的朋友加油! ***********************/
STL中vector模拟链表实现,代码量较少,也不易犯错误,内存的申请与释放都不需要自己处理,比较好!
静态建表(链式前向星)优点在于可以存储重边,需要空间也不多,与动态建表相比没有内存管理,
更安全,应该说除了不能直接
用起点和终点确定是否有边以外,链式前向星几乎是完美的!
相关文章推荐
- 链式前向星,前向星,vector模拟链表
- 【图论】数组模拟邻接表存储(链式前向星)
- C++学习总结——顺序存储,链式存储,索引存储,哈希存储的优缺点,以及vector数组,list链表,十字链表,索引结构,hash表的实现
- 图的存储(2)---链式前向星的学习
- 队列的链式存储---链表实现(有头结点)
- 2-5-归并链式存储的单链表-线性表-第2章-《数据结构》课本源码-严蔚敏吴伟民版
- 线性表链式存储_调试_链表模板库封装思想
- Java基础 - 线性表之链式存储结构-循环链表
- 单链表-链式存储-Java实现
- hdu3572 Task Schedule--Dinic算法 & 最大流 & 链式前向星 & vector
- 2008秋季-线性表的链式存储(仅单链表)
- 03.线性表(二)链式存储结构.单链表1
- 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest A Email Aliases(模拟STL vector+map)
- 2.3_线性表的链式存储结构_单链表
- 图的存储结构---(*链式前向星*)
- <<C#版数据结构>>之--线性表的链式存储(单链表)
- 链式前向星存储结构
- 【转载松松】 hashtable (vector&链式前向星)
- 数据结构与算法——线性表链式存储(单循环链表)
- 艾伟_转载:C#版数据结构之--线性表的链式存储(单链表)