Floyed
作者:互联网
#include <iostream>
using namespace std;
const int MAX = 32667;
int path[100][100];
int Distance[100][100];
struct AMGraph
{
int m_vexnum;
int m_arcnum;
char m_vexname[100];
int m_arcweight[100][100];
};
int locateVex(AMGraph AMG, char vexname)
{
for(int i = 0; i < AMG.m_vexnum; i++)
{
if(AMG.m_vexname[i] == vexname)
{
return i;
}
}
return -1;
}
void CreateAMGraph(AMGraph &AMG)
{
cout << "pls input the vexnum and arcnum "<< endl;
cin >> AMG.m_vexnum >> AMG.m_arcnum;
cout << "pls input the name of vex " << endl;
for(int i = 0; i < AMG.m_vexnum; i++)
{
cout << "the name " << i+1 << " " << endl;
cin >> AMG.m_vexname[i];
}
for(int i = 0; i < AMG.m_vexnum; i++)
{
for(int j = 0; j < AMG.m_vexnum; j++)
{
if(j != i)
{
AMG.m_arcweight[i][j] = MAX;
}
else
{
AMG.m_arcweight[i][j] = 0;
}
}
}
cout << "pls input the arcweight as 'a b 10' " <<endl;
for(int i = 0; i < AMG.m_arcnum; i++)
{
char vexname1, vexname2;
int weight;
cin >> vexname1 >> vexname2 >> weight;
int m = locateVex(AMG, vexname1);
int n = locateVex(AMG, vexname2);
AMG.m_arcweight[m][n] = weight;
}
}
void Floyed(AMGraph AMG)
{
for(int i = 0; i < AMG.m_vexnum; i++)
{
for(int j = 0; j < AMG.m_vexnum; j++)
{
Distance[i][j] = AMG.m_arcweight[i][j];
if(Distance[i][j] < MAX && j != i)
{
path[i][j] = i;
}
else
{
path[i][j] = -1;
}
}
}
for(int k = 0; k < AMG.m_vexnum; k++)
{
for(int m = 0; m < AMG.m_vexnum; m++)
{
for(int n = 0; n < AMG.m_vexnum; n++)
{
if(Distance[m][n] > Distance[m][k] + Distance[k][n])
{
Distance[m][n] = Distance[m][k] + Distance[k][n];
}
path[m][n] = path[m][k];
}
}
}
}
void showPath(AMGraph AMG, int startVexAdd, int endVexAdd)
{
if(path[startVexAdd][endVexAdd] != -1)
{
showPath(AMG, startVexAdd, endVexAdd);
cout << AMG.m_vexname[path[startVexAdd][endVexAdd]];
}
}
void test()
{
AMGraph mygraph;
CreateAMGraph(mygraph);
Floyed(mygraph);
char startVexName, endVexName;
cout << "pls input the startvexname and endvexname " << endl;
cin >> startVexName >> endVexName;
int startVexAdd = locateVex(mygraph, startVexName);
int endVexAdd = locateVex(mygraph, endVexName);
showPath(mygraph, startVexAdd, endVexAdd);
cout << " distance is " << Distance[startVexAdd][endVexAdd] << endl;
}
int main()
{
test();
return 0;
}
标签:vexnum,Distance,int,Floyed,AMG,++,100 来源: https://blog.csdn.net/m0_56776789/article/details/121884667