链式前向星,前向星,vector模拟链表
2017-04-11 09:14
295 查看
链式前向星:
前向星:
vector模拟链表:
#include <bits/stdc++.h> using namespace std; const int maxn = 1000; int num = 1; int n, m; int head[maxn]; struct node { int to; int next; int w; }edge[maxn]; void init() { memset(head, -1, sizeof(head)); num = 1; } void out_put() { 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); } } } int main() { int a, b, w; while (~scanf("%d %d",&n,&m)) { 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++; } out_put(); } return 0; }
前向星:
#include <bits/stdc++.h> using namespace std; const int maxn = 1000; int n,m; int head[maxn]; struct node { int from; int to; int w; }edge[maxn]; bool cmp(node a, node b) { if (a.from == b.from && a.to == b.to) { return a.w < b.w; } else if (a.from == b.from && a.to != b.to) { return a.to < b.to; } else return a.from < b.from; } int main() { while (~scanf("%d%d",&n,&m)) { for (int i = 1; i <= m; i++) { scanf("%d%d%d",&edge[i].from,&edge[i].to,&edge[i].w); } sort(edge+1,edge+1+m,cmp); memset(head,-1,sizeof(head)); for (int i = 1; i <= m; i++) { if (edge[i].from != edge[i-1].from) { head[edge[i].from] = i; } } for (int i = 1; i <= n; i++) { for (int k = head[i]; edge[k].from == i && k <= m; k++) { printf("%d->%d,w is %d\n",edge[k].from,edge[k].to,edge[k].w); } } } return 0; }
vector模拟链表:
#include <bits/stdc++.h> using namespace std; const int maxn = 1000; const int maxm = 1000000; int n, m; struct node { int to; int w; }; vector<node> val[maxn]; bool cmp(node a, node b) { return a.to < b.to; } void out_put() { vector<node>::iterator it; for (int i = 1; i <= n && val[i].size() > 0; i++) { sort(val[i].begin(),val[i].end(),cmp); for (it = val[i].begin(); it != val[i].end(); it++) { node tmp = *it; cout << i << " " << tmp.to << " " << tmp.w << endl; } } } int main() { int a, b, w; while (cin >> n >> m) { for (int i = 1; i <= m; i++) { node e; cin >> a >> b >> w; e.to = b; e.w = w; val[a].push_back(e); } out_put(); } return 0; }
相关文章推荐
- 链式前向星|前向星|STL中vector模拟链表(图的存储)
- Dijkstra的实现(用vector模拟邻接链表)
- NYOJ 20 吝啬的国度 【BFS+链式前向星建图,Vector建图】
- 【图论】数组模拟邻接表存储(链式前向星)
- 九度 OJ 1447 题 最短路径问题 Dijkstra(迪杰斯特拉)算法实现,使用vector模板模拟邻接链表
- 【转载松松】 hashtable (vector&链式前向星)
- hdu3572 Task Schedule--Dinic算法 & 最大流 & 链式前向星 & vector
- C++学习总结——顺序存储,链式存储,索引存储,哈希存储的优缺点,以及vector数组,list链表,十字链表,索引结构,hash表的实现
- NYOJ 115 城市平乱 【无向图 + 链式前向星 + SPFA】
- 2.3_线性表的链式存储结构_单链表
- UVA11988 Broken Keyboard (a.k.a. Beiju Text)【数组模拟链表】
- PTA--双向链表模拟Deque
- 【算法】数组模拟链表
- csu 1365 双向链表模拟超时
- 模拟实现vector容器
- 树的存储方式数组链表+vector
- 单链表实现队列链式存储
- 栈的模拟(内核为链表).c
- 【算法入门经典】5.1.2不定长数组:vector【uva101 模拟】
- UVA - 133 The Dole Queue(模拟链表)