typedef struct arc //作为邻接表头节点所指向的边 { char name1[20]; //存储指向的节点 float dist; struct arc *next; } arc; typedef struct Head //作为邻接表头节点 { char name[20]; //存储站点名称 arc * add; } Head;
时间: 2024-02-14 18:06:02 浏览: 145
这段代码定义了两个结构体,分别是 `arc` 和 `Head`。
`arc` 结构体表示邻接表中的边,包含三个成员变量:
- `name1`:存储指向的节点的名称,类型为 `char` 数组。
- `dist`:存储节点之间的距离,类型为 `float`。
- `next`:存储下一个边节点的指针,类型为 `arc*`。
`Head` 结构体表示邻接表中的头节点,包含两个成员变量:
- `name`:存储站点的名称,类型为 `char` 数组。
- `add`:存储指向该站点的第一条边的指针,类型为 `arc*`。
这两个结构体的定义可以用于实现无向图的邻接表存储结构。其中,每个头节点表示一个站点,其 `add` 指向该站点的第一条边;每个边节点表示两个站点之间的连接关系,包含指向另一个站点的名称和距离信息以及指向下一条边的指针。
相关问题
typedef struct ArcNode //边节点 { ArcType adjvex; //边的编号 struct ArcNode *next; //下一条边 int lowcost; //边权值 }ArcNode; typedef struct VNode //顶点信息 { VerTexType data; //顶点序号 char build[20]; //顶点名称 ArcNode* FirstArc; //顶点的关联的一条边 }VNode,AdjList; typedef struct { AdjList vertices[MVNum];//所有顶点 int vexnums; //总顶点数 int arcnums; //总边数 }ALGraph; 请基于以上结构体编写邻接表无向带权图实现弗洛伊德算法的代码
好的,我来基于以上结构体编写邻接表无向带权图实现弗洛伊德算法的代码。
假设 MVNum 定义为顶点的最大数量,INF 定义为无穷大,代码如下:
```c
#define MVNum 100
#define INF 0x3f3f3f3f
typedef int VerTexType;
typedef struct ArcNode //边节点
{
int adjvex; //与该边相连的顶点编号
struct ArcNode *next; //下一条边
int weight; //边权值
}ArcNode;
typedef struct VNode //顶点信息
{
VerTexType data; //顶点序号
char build[20]; //顶点名称
ArcNode* FirstArc; //顶点的关联的一条边
}VNode, AdjList;
typedef struct {
AdjList vertices[MVNum]; //所有顶点
int vexnums; //总顶点数
int arcnums; //总边数
}ALGraph;
//邻接表无向带权图实现弗洛伊德算法
void Floyd(ALGraph G, int dist[][MVNum])
{
int i, j, k;
//初始化距离矩阵
for (i = 0; i < G.vexnums; i++) {
for (j = 0; j < G.vexnums; j++) {
dist[i][j] = INF;
}
dist[i][i] = 0;
}
//构建距离矩阵
for (i = 0; i < G.vexnums; i++) {
ArcNode* p = G.vertices[i].FirstArc;
while (p) {
dist[i][p->adjvex] = p->weight;
p = p->next;
}
}
//计算最短路径
for (k = 0; k < G.vexnums; k++) {
for (i = 0; i < G.vexnums; i++) {
for (j = 0; j < G.vexnums; j++) {
if (dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
}
```
以上代码中,用 dist 数组表示距离矩阵,其中 dist[i][j] 表示从顶点 i 到顶点 j 的最短距离。在计算距离矩阵时,先将 dist 数组中的所有元素初始化为 INF,然后遍历邻接表中的每条边,将边的权值存储到 dist 数组中对应的位置。最后,使用三重循环计算所有点对之间的最短距离,更新 dist 数组中的元素。
#include<iostream> #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MVNum 100 //最大顶点数 #define MaxInt 32767 using namespace std; typedef struct {//图的邻接矩阵存储表示 int vexnum,arcnum; //图的当前顶点数和边数 int vexs[MVNum]; //顶点表 int arcs[MVNum][MVNum]; //邻接矩阵 }AMGraph; int CreateUDN(AMGraph &G,int vexnum,int arcnum) {//采用邻接矩阵表示法,创建无向网G G.vexnum=vexnum; //输入总顶点数 G.arcnum=arcnum; //输入总边数 if(G.vexnum>MVNum) return ERROR; //超出最大顶点数则结束函数 int i,j,a,b,c; for(i=1;i<=G.vexnum;i++) G.vexs[i]=i; for(i=1;i<=G.vexnum;i++) //初始化邻接矩阵,边的权值均置为极大值 for(j=1;j<=G.vexnum;j++) G.arcs[i][j]=MaxInt; for(i=0;i<G.arcnum;i++) //顶点a和顶点b之间有一条长度为c的路 { cin>>a>>b>>c; G.arcs[a][b]=c; G.arcs[b][a]=c; } return OK; } void ShortPathMAX(AMGraph G,int v0) {//用Dijkstra算法求图G中距离顶点v0的最短路径长度最大的一个顶点 /**begin/ /**end/ } int main() { int n,m; while(cin>>n>>m) { if(n==0&&m==0) break; AMGraph G; CreateUDN(G,n,m); //创建无向网G int v; cin>>v; ShortPathMAX(G,v); //最长的最短路径的求解 } return 0; }补全代码,测试输入: 4 4 1 2 1 2 3 1 3 4 1 2 4 1 4 4 3 1 2 3 2 3 2 2 4 6 3 0 0 预期输出: 1 2 4 8
#include<iostream>
#include<algorithm>
#include<cstring>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MVNum 100 //最大顶点数
#define MaxInt 32767
using namespace std;
typedef struct {//图的邻接矩阵存储表示
int vexnum,arcnum; //图的当前顶点数和边数
int vexs[MVNum]; //顶点表
int arcs[MVNum][MVNum]; //邻接矩阵
}AMGraph;
int CreateUDN(AMGraph &G,int vexnum,int arcnum) {//采用邻接矩阵表示法,创建无向网G
G.vexnum=vexnum; //输入总顶点数
G.arcnum=arcnum; //输入总边数
if(G.vexnum>MVNum)
return ERROR; //超出最大顶点数则结束函数
int i,j,a,b,c;
for(i=1;i<=G.vexnum;i++)
G.vexs[i]=i;
for(i=1;i<=G.vexnum;i++) //初始化邻接矩阵,边的权值均置为极大值
for(j=1;j<=G.vexnum;j++)
G.arcs[i][j]=MaxInt;
for(i=0;i<G.arcnum;i++) //顶点a和顶点b之间有一条长度为c的路
{
cin>>a>>b>>c;
G.arcs[a][b]=c;
G.arcs[b][a]=c;
}
return OK;
}
void ShortPathMAX(AMGraph G,int v0) {//用Dijkstra算法求图G中距离顶点v0的最短路径长度最大的一个顶点
int dist[MVNum];//存储源点v0到其他顶点的最短距离
bool visited[MVNum]={false};//记录顶点是否已被访问
memset(dist,0,sizeof(dist));
for(int i=1;i<=G.vexnum;i++)//初始化dist数组
if(i!=v0)
dist[i]=MaxInt;
for(int i=1;i<=G.vexnum;i++) //循环n次,每次找出一个顶点的最短路径
{
int maxDist=-1,u;
for(int j=1;j<=G.vexnum;j++)
{
if(!visited[j]&&dist[j]>maxDist)
{
maxDist=dist[j];
u=j;
}
}
visited[u]=true;
for(int v=1;v<=G.vexnum;v++)
{
if(!visited[v]&&G.arcs[u][v]<MaxInt)
{
int newDist=max(dist[u],G.arcs[u][v]);
if(newDist<dist[v])
dist[v]=newDist;
}
}
}
for(int i=1;i<=G.vexnum;i++)
if(dist[i]!=MaxInt)
cout<<dist[i]<<" ";
cout<<endl;
}
int main() {
int n,m;
while(cin>>n>>m) {
if(n==0&&m==0)
break;
AMGraph G;
CreateUDN(G,n,m); //创建无向网G
int v;
cin>>v;
ShortPathMAX(G,v); //最长的最短路径的求解
}
return 0;
}
阅读全文
相关推荐
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)