-----图论建图:模板
来源:互联网 发布:赶聪互刷源码 编辑:程序博客网 时间:2024/06/06 07:07
临接表建图:#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int N,M;struct node{ int to,w;};vector<node>G[105];int main(){ int i,j,x,y,z; scanf("%d%d",&N,&M); { scanf("%d%d%d",&x,&y,&z); G[x].push_back((node){y,z}); } for(i=1;i<=N;i++) for(j=0;j<G[i].size();j++) printf("%d %d %d\n",i,G[i][j].to,G[i][j].w);}
链式前向星:#include <iostream>#include<stdio.h>#include<vector>#include<cstring>using namespace std;int n,m;struct node{ int to,w,next;} G[105];int head[105];int main(){ int x,y,z; cin>>n>>m; memset(head,-1,sizeof(head)); for(int i=0; i<m; i++) { cin>>x>>y>>z; G[i].to=y; G[i].w=z; G[i].next=head[x]; head[x]=i; } for(int i=1; i<=n; i++) { for(j=head[i]; j!=-1; j=G[i].next)///haed[i]记录的是当前顶点为i的输入时最后一条边的位置, ///G[i].next记录的是同样顶点为i的上一条边的位置,当G[i].next为-1时,说明顶点为i的边已经全部输出完了 printf("%d %d %d\n",i,G[j].to,G[j].w);0 }}链式前向星的代码实现过程:样例:M=5 x y z 1 2 3 2 3 4 2 4 3 4 5 6 4 1 2for(int i=0;i<M;i++){ G[0].to=2; G[0].w=3; G[0].next=head[1]=-1; head[1]=0; G[1].to=3; G[1].w=4; G[1].next=head[2]; head[2]=1; G[2].to=4; G[2].w=3; G[2].next=head[2]=1; head[2]=2; G[3].to=5; G[3].w=6; G[3].next=head[4]=-1; head[4]=3; G[4].to=1; G[4].w=2; G[4].next=head[4]=3; head[4]=4; }for(int i=1;i<=n;i++) for(int j=head[i];j!=-1;j=G[i].next) { i=1; j=head[1]=0; G[1].to; G[1].w; G[1].next=-1; i=2; j=head[2]=2; G[2].to; G[2].w; G[2].next=1;(G[2].next!=-1) G[1].to; G[1].w; G[1].next=-1; i=3; head[3]=-1; i=4; head[4]=4; G[4].to; G[4].w; G[4].next=3;(G[4].next!=-1); G[3].to; G[3].w; G[3].next=-1; }
0 0
- -----图论建图:模板
- 模板
- 模板?
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- NYOJ1022合纵连横(支持删除操作的并查集)
- Oracle 11gR2 RAC修改VIP
- 40个Java多线程问题总结
- 最新IOS App上架App Store图文教程(2017跨平台版)
- 文件的上传和下载
- -----图论建图:模板
- MP4容器系列文章之STSD BOX
- cocos study notes-Scene
- Matplotlib绘制树形图
- matlab之循环结构
- Flex项目中"...\version4locale"错误解决方法
- ORA-01722:invalid number-解决GAT项目中车辆维修Bug(续)
- Spring- HibernateTemplate使用
- 多任务融合的模型