链式前向星,前向星,vector模拟链表
来源:互联网 发布:mac百度云盘怎么卸载 编辑:程序博客网 时间:2024/06/11 07:43
链式前向星:
#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;}
0 0
- 链式前向星|前向星|STL中vector模拟链表(图的存储)
- 链式前向星,前向星,vector模拟链表
- hashtable (vector&链式前向星)
- 链式前向星
- 链式前向星
- 链式前向星
- 链式前向星
- 链式前向星
- 链式前向星
- 链式前向星
- 链式前向星
- 链式前向星
- 链式前向星
- 链式前向星
- 链式前向星
- 链式前向星
- 链式前向星
- 链式前向星
- NYOJ677谍战(最大流最小割问题)
- hbbtv
- linux服务器时间同步
- Android弹窗_PopupWindow详解
- JDBC学习笔记
- 链式前向星,前向星,vector模拟链表
- spark.mllib源码阅读-分类算法4-DecisionTree
- C语言编写FIR数字低通滤波器
- Navicat导出MySQL的数据字典
- 【精品】对同步的理解,进而理解lock与synchronized。
- SPOJ 18469 UOFTCG
- 第三方支付
- Delete Node in a Linked List
- 输入任意一年,判断该年是否是闰年