图论 邻接表建图+dfs
来源:互联网 发布:易中天品三国 知乎 编辑:程序博客网 时间:2024/06/05 05:26
上一篇用的是邻接矩阵建图:点击打开链接
所以dfs(&G,0)结果为0 2 3 4 1
dfs(&G,1)结果为1 2 3 4
dfs(&G,2)结果为2 3 4
dfs(&G,3)结果为 3 4
dfs(&G,4)结果为4
#include<stdio.h>#include<string.h>#include<string>#include<iostream>using namespace std;int vis[100];typedef struct Enode//邻接表边集{ int v; int weight; struct Enode *next;} edgenode;typedef struct Vertexnode//邻接表首结点{ int v; edgenode *first;} vertexnode;typedef struct Graph//图{ vertexnode Vnode[100]; int num_v,num_e;} mygraph;void create(mygraph *G)//建图{ int i,j,k,x,y,z; edgenode *E; cin>>G->num_v>>G->num_e; for(i=0; i<G->num_v; i++) { cin>>G->Vnode[i].v; G->Vnode[i].first=NULL; } for(i=0; i<G->num_e; i++) { scanf("%d%d%d",&x,&y,&z); E=new Enode; E->v=y; E->weight=z; E->next=G->Vnode[x].first; G->Vnode[x].first=E; //如果是无向图就把下面这段代码加上// E=new Enode;//// E->v=x;// E->weight=z;// E->next=G->Vnode[y].first;// G->Vnode[y].first=E; }}void dfs(mygraph *g,int x)dfs{ vis[x]=1; Enode *p; int i,j; printf("%d ",g->Vnode[x]); p=g->Vnode[x].first; while(p) { if(!vis[p->v]) { dfs(g,p->v); } p=p->next; }}int main(){ int i,j,k; int tmp,tmp1,tmp2; mygraph G; create(&G);// for(i=0;i<G.num_v;i++)// if(!vis[i])dfs(&G,i); memset(vis,0,sizeof(vis)); dfs(&G,0); printf("\n"); memset(vis,0,sizeof(vis)); dfs(&G,1); printf("\n"); memset(vis,0,sizeof(vis)); dfs(&G,2); printf("\n"); memset(vis,0,sizeof(vis)); dfs(&G,3); printf("\n"); memset(vis,0,sizeof(vis)); dfs(&G,4); printf("\n"); return 0;}/*5 50 1 2 3 40 1 90 2 21 2 32 3 53 4 1*/
测试数据中5 5代表节点数和边数为5,下面是结点名0 1 2 3 4 下面几行中分别为为 弧尾(边的起点),弧首(边的终点),和边的权值
阅读全文
0 0
- 图论 邻接表建图+dfs
- 邻接表—DFS
- 邻接表 DFS
- hdu1269(邻接表+dfs)
- DFS遍历邻接表
- 【图论】关于邻接表建图
- MYC归来(3)图论,邻接表+dfs!!!
- hdu4707 Pet【DFS&邻接表】
- 邻接表的dfs遍历
- 数据结构 邻接链表 DFS
- hrbust 1326 哈理工oj 1326 Leyni的国家【DFS+邻接表建图】
- 图论 邻接链表存储 BFS DFS 拓扑排序 最小生成树 KRUSKAL PRIM
- hrbust 1954 哈理工oj 1954 神奇的国家【图论+邻接表+dfs】
- 图论(一):DFS,BFS,邻接链表,并查集
- DFS搜索,用邻接表存储图
- DFS BFS遍历图 邻接表实现
- 邻接表的深度优先搜索(DFS)
- Another Postman Problem (邻接表+DFS)
- Aladdin and the Flying Carpet Light OJ 1341
- tensoflow练习3:卷积神经网络用于分类
- 图解修改Maven仓库下载到本地jar包默认存储位置
- usaco 3.2 Magic Squares
- linux驱动复习之阻塞与非阻塞的IO操作 [一]
- 图论 邻接表建图+dfs
- [leetcode]211. Add and Search Word
- 并查集初步
- Unity3D之UI按键绑定事件(二)
- H
- English words page three
- 语音增强-理论与实践----笔记-谱减法
- 修改Android5.1源码,配置以太网设置默认为静态ip
- jQuery笔记(动画)