ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

链式前向星

2022-06-28 22:34:20  阅读:167  来源: 互联网

标签:head int ffmax next 100005 链式 前向星 赋值


大概定义:

以图中每一顶点为起点,记录下其所有边在图中的起始位置和权值,可快速访问某顶点的所有邻接点。

 

链式前向星存储一般包括边集数组(e[],e[i]表示第i条边)和头结点数组(head[],head[i]表示以i为起点的一条边在e[]中的下标)两种。

则建立结构体为:

1 struct kk{
2     int to,w,next;
3 }e[100005];//边集数组

定义数组为:

int head[100005];//头结点数组

 

 

 

 

在上图中,to为边的终点,w为边的权值,next为以该边的终点为起点的下一条边的起点。

讲head初始化为0或-1,表示该点无下一条可到达边。

以1为起点,按顺序输入为:

1 2 2
1 3 3
1 2 1
1 5 4
2 3 5
3 4 1
4 5 1

在输入1到2时,e[1]中,to=2,w=2,next则赋值为head[1]的-1,由于是第一条边,head[1]赋值为1。

在输入1到3时,e[2]中,to=3,w=3,next则赋值为head[1]的1,第二条边,head[1]赋值为2。

在输入1到4时,e[2]中,to=4,w=1,next则赋值为head[1]的2,第二条边,head[1]赋值为3。

。。。。。。

以此类推,写出代码:

 1 int t = 0;//记录边数
 2 for(int i=1; i<=m; i++)
 3     {
 4         cin >> x >> y >> z;//输入
 5         e[t].to = y;//赋值to
 6         e[t].next = head[x];//赋值next
 7         head[x] = t;//heat刷新
 8         e[t].w = z;//权值
 9         t++;
10     }
11 //勤劳的人可以写成函数

我好懒啊

如果不放心,可以写测试代码:

 1 for(int i=1; i<=n; i++)
 2     {
 3         cout << i << " : ";
 4         int j=head[i];
 5         while(e[j].to)
 6         {
 7             cout << e[j].to << " " << e[j].w << " " << e[j].next << "   ";
 8             j = e[j].next;
 9         }
10         cout << endl;
11     }

 

完整代码:

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int m,n;
 6 int x,y,z;
 7 int t=1;
 8 int head[100005];
 9 int ffmax[100005];
10 
11 struct kk{
12     int to,next;
13 }e[100005];
14 
15 int main()
16 {
17     cin >> n >> m;
18     memset(head,-1,sizeof(head));
19     /*
20     for(int i=0; i<=100; i++)
21     {
22         cout << i << " : " << head[i] << endl;
23     }
24     */
25     for(int i=1; i<=m; i++)
26     {
27         cin >> x >> y >> z;
28         e[t].to = y;
29         e[t].next = head[x];
30         head[x] = t;
31         e[t].w = z;
32         t++;
33     }
34     for(int i=1; i<=n; i++)
35     {
36         cout << i << " : ";
37         int j=head[i];
38         while(e[j].to)
39         {
40             cout << e[j].to << " " << e[j].w << " " << e[j].next << "   ";
41             j = e[j].next;
42         }
43         cout << endl;
44     }
45     return 0;
46 }

另附练习:P3916 图的遍历

 

AC代码:

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int m,n;
 6 int x,y;
 7 int t=1;
 8 int head[100005];
 9 int ffmax[100005];
10 
11 struct kk{
12     int to,next;
13 }e[100005];
14 
15 void findmax(int a,int b)
16 {
17     if(ffmax[a]) return;
18         ffmax[a]=b;
19         for(int i=head[a]; i; i=e[i].next)
20         {
21             int c=e[i].to;
22             if(!ffmax[c]) findmax(c,b);
23         }
24 }
25 
26 int main()
27 {
28     cin >> n >> m;
29     memset(head,-1,sizeof(head));
30     /*
31     for(int i=0; i<=100; i++)
32     {
33         cout << i << " : " << head[i] << endl;
34     }
35     */
36     for(int i=1; i<=m; i++)
37     {
38         cin >> y >> x;
39         e[t].to = y;
40         e[t].next = head[x];
41         head[x] = t;
42         //e[t].w = z;
43         t++;
44     }
45     /*
46     for(int i=1; i<=n; i++)
47     {
48         cout << i << " : ";
49         int j=head[i];
50         while(e[j].to)
51         {
52             cout << e[j].to << " " << e[j].w << " " << e[j].next << "   ";
53             j = e[j].next;
54         }
55         cout << endl;
56     }
57     */
58     for(int i=n; i>=1; i--) findmax(i,i);
59     for(int i=1; i<=n; i++) cout<<ffmax[i]<<" ";
60     return 0;
61 }

 

标签:head,int,ffmax,next,100005,链式,前向星,赋值
来源: https://www.cnblogs.com/kkk05/p/16421445.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有