我是靠谱客的博主 健康摩托,这篇文章主要介绍[邻接表] 学习邻接表的表示方法+BFS,现在分享给大家,希望可以做个参考。

算法导论上面的伪代码实现哦,没啥技术,不过这个邻接表表示法(figo大神教的)很nice。

简单说一下,head里面是放着自己节点后面链的最后一个元素在边池中的位置,边池里面成一个一个链状,像并查集,但是没有路径压缩,边池为next通过for(int i=head[v];i!=-1;i=next[i])就能找到属于v节点的所以边,对应next下标里面存放着,dest[i]为和v构成边的节点,dist里面存放着距离,这里是没有边权的图,所以默认为1,主要看代码,给力。

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include<iostream> #include<cstdio> #include<iomanip> #include<algorithm> #include<cstring> #include<string> #include<bitset> #include<queue> #include<vector> #include<string> #include<cmath> #include<map> #define rep(i,n,m) for(int i=(n);i<=(m);++i) #define re1(i,n) rep(i,1,n) #define re0(i,n) rep(i,0,n) #define RE(a) ((a)*(a)) #define SIZE(a) (int((a).size())) #define vi vector<int> #define vl vector<ll> #define vs vector<string> #define qi queue<int> #define ql queue<ll> #define qs queue<string> //count distance 不能用哦,已经定义了。 using namespace std; typedef long long ll; template<class T> void inline maxi(T a,T b){ a=max(a,b); } template<class T> void inline mini(T a,T b){ a=min(a,b); } template<class T> void show(T &a,int n,int m){ rep(i,n,m){ cout<<setw(6)<<a[i]; } cout<<endl; } template<class T> void show(T *a[10],int n,int m){ re0(i,n){ re0(j,m) cout<<a[i]<<' '; cout<<endl; } } const int maxnum=8+1; const int maxint=2147483647; //graph int head[maxnum]; vi next,dist,dest; void add_direct(int from,int to,int dis=1){ next.push_back(head[from]); head[from]=SIZE(next)-1; dist.push_back(dis); dest.push_back(to); } void add_nodirect(int from,int to,int dis=1){ add_direct(from,to,dis); add_direct(to,from,dis); } void init(){ next.clear(); dist.clear(); dest.clear(); re0(u,maxnum-1) head[u]=-1; add_nodirect(1,3); add_nodirect(1,2); add_nodirect(4,3); add_nodirect(4,5); add_nodirect(4,6); add_nodirect(5,6); add_nodirect(5,7); add_nodirect(6,7); add_nodirect(6,8); add_nodirect(8,7); // show(head,1,8); // show(next,0,SIZE(next)-1); // show(dest,0,SIZE(next)-1); } int path[maxnum]; void bfs(int s){ qi Q; bool vis[maxnum]; re0(u,maxnum-1){ vis[u]=false; path[u]=-1; } vis[s]=true; Q.push(s); while(!Q.empty()){ int u=Q.front(); Q.pop(); for(int i=head[u];i!=-1;i=next[i]){ int v = dest[i]; if(vis[v]) continue; Q.push(v); vis[v]=1; path[v]=u; } } } void print_path(int from,int to){ if(to==from){ cout<<from<<endl; }else if(path[to]==-1){ cout<<"no way"<<endl; }else{ print_path(from,path[to]); cout<<to<<endl; } } //#define codeforces CODEFORCES int main(){ #ifdef codeforces freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif init(); bfs(3); print_path(4,7); }

  

转载于:https://www.cnblogs.com/gggin/archive/2012/12/25/2832436.html

最后

以上就是健康摩托最近收集整理的关于[邻接表] 学习邻接表的表示方法+BFS的全部内容,更多相关[邻接表]内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部