在这里插入代码#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
#define SIZE 50
#define MaxValue 3000
typedef struct
{
int arcs[SIZE][SIZE];
int VertexNum;
int EdgeNum;
char Vertex[SIZE];
int IsTravel[SIZE];
bool S[SIZE];
int D[SIZE];
int Path[SIZE];
}GraphMatrix;
GraphMatrix *CreateGraph(GraphMatrix *GM)
{
char start,end;
int head,tail,weight;
if((GM=(GraphMatrix *)malloc(sizeof(GraphMatrix)))==NULL)
{
cout<<"内存分配失败"<<endl;
exit(1);
}
cout<<"请输入图中有多少个顶点和边长"<<endl;
cin>>GM->VertexNum>>GM->EdgeNum;
for(int i=0;i<GM->VertexNum;i++)
{
GM->IsTravel[i]=0;
for(int j=0;j<GM->VertexNum;j++)
{
GM->arcs[i][j]=MaxValue;
}
}
cout<<"请输入顶点的信息"<<endl;
for(int i=0;i<GM->VertexNum;i++)
{
cin>>GM->Vertex[i];
}
cout<<"请输入个边的顶点<start,end>和权值"<<endl;
for(int i=0;i<GM->EdgeNum;i++)
{
cin>>start>>end>>weight;
for(head=0;start!=GM->Vertex[head];head++);
for(tail=0;end!=GM->Vertex[tail];tail++);
GM->arcs[head][tail]=weight;
}
return GM;
}
void ShortestPath_DIJ(GraphMatrix *GM,int v0)
{
int min;
int v,w;
for(v=0;v<GM->VertexNum;v++)
{
GM->S[v]=false;
GM->D[v]=GM->arcs[v0][v];
if(GM->D[v]<MaxValue) GM->Path[v]=v0;
else GM->Path[v]=-1;
}
GM->S[v0]=true;
GM->D[v0]=0;
for(int i=1;i<GM->VertexNum;i++)
{
min=MaxValue;
for(w=0;w<GM->VertexNum;w++)
{
if(!(GM->S[w]) && GM->D[w]<min)
{
v=w;
min=GM->D[w];
}
}
GM->S[v]=true;
for(w=0;w<GM->VertexNum;w++)
{
if(!(GM->S[w]) && GM->D[v]+GM->arcs[v][w]<GM->D[w])
{
GM->D[w]=GM->D[v]+GM->arcs[v][w];
GM->Path[w]=v;
}
}
}
}
int main()
{
int head,tail;
char start,end;
GraphMatrix *GM=NULL;
GM=CreateGraph(GM);
cout<<"请输入起点为"<<endl;
cin>>start;
for(head=0;start!=GM->Vertex[head];head++);
cout<<head<<endl;
ShortestPath_DIJ(GM,head);
cout<<"请输入终点为"<<endl;
cin>>end;
for(tail=0;end!=GM->Vertex[tail];tail++);
cout<<tail<<endl;
cout<<start<<"到"<<end<<"的距离为"<<GM->D[tail];
return 0;
} 片

- 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
```#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cmath>
using namespace std;
#define SIZE 10
#define MaxValue 3000
typedef struct
{
int arcs[SIZE][SIZE];
char Vertex[SIZE];
bool IsTravel[SIZE];
int D[SIZE][SIZE];
int VertexNum;
int EdgeNum;
int Path[SIZE][SIZE];
}GraphMatrix;
GraphMatrix *CreateGraph()
{
GraphMatrix *GM;
char start,end;
int head,tail,weight;
if((GM=(GraphMatrix *)malloc(sizeof(GraphMatrix)))==NULL)
{
cout<<"内存分配失败"<<endl;
exit(1);
}
cout<<"请输入图中有多少个顶点和边长"<<endl;
cin>>GM->VertexNum>>GM->EdgeNum;
for(int i=0;i<GM->VertexNum;i++)
{
GM->IsTravel[i]=false;
for(int j=0;j<GM->VertexNum;j++)
{
GM->arcs[i][j]=MaxValue;
}
}
cout<<"请输入个顶点的信息"<<endl;
for(int i=0;i<GM->VertexNum;i++) cin>>GM->Vertex[i];
cout<<"请输入边长的信息<start,end>和权值"<<endl;
for(int k=0;k<GM->EdgeNum;k++)
{
cin>>start>>end>>weight;
for(head=0; start!= GM->Vertex[head];head++);
for(tail=0; end!= GM->Vertex[tail];tail++);
GM->arcs[head][tail]=weight;
}
return GM;
}
void ShortestPath_Floyd(GraphMatrix *GM)
{
for(int i=0;i<GM->VertexNum;i++) GM->arcs[i][i]=0;
for(int i=0;i<GM->VertexNum;i++)
{
for(int j=0;j<GM->VertexNum;j++)
{
GM->D[i][j]=GM->arcs[i][j];
if(GM->D[i][j]<MaxValue && i!=j) GM->Path[i][j]=i;
else GM->Path[i][j]=-1;
}
}
for(int k=0;k<GM->VertexNum;k++)
{
for(int i=0;i<GM->VertexNum;i++)
{
for(int j=0;j<GM->VertexNum;j++)
{
if(GM->D[i][k]+GM->D[k][j]<GM->D[i][j])
{
GM->D[i][j]=GM->D[i][k]+GM->D[k][j];
GM->Path[i][j]=GM->Path[k][j];
}
}
}
}
}
int main()
{
GraphMatrix *GM;
GM=CreateGraph();
ShortestPath_Floyd(GM);
cout<<GM->D[0][2]<<endl;
return 0;
}

- 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