#include<iostream>
#define N 10
using namespace std;
int min(int x,int y)
{
return x<y? x:y;
}
struct weight
{
int value[N][N];
};
struct graph
{
friend struct weight;
int vex;
int edge;
weight wt;
weight d[N];
graph()
{
int v,e;
cout<<"input the num of the vex :";
cin>>v;
vex=v;
cout<<"input the num of the edge :";
cin>>e;
edge=e;
for(int i=1;i<=vex;++i)
for(int j=1;j<=vex;++j)
{
if(i==j)
wt.value[i][j]=0;
else
wt.value[i][j]=0xfffffff;
}
}
void insert()
{
int start,end,w;
for(int i=1;i<=edge;++i)
{
cout<<"input the start point of the weight:";
cin>>start;
cout<<"input the end point of the weight:";
cin>>end;
cout<<"input the weight :"<<endl;
cin>>w;
wt.value[start][end]=w;
}
}
void floyd()
{
cout<<"init d0 :"<<endl;
for(int i=1;i<=vex;++i)
for(int j=1;j<=vex;++j)
d[0].value[i][j]=wt.value[i][j];
for(int k=1;k<=vex;++k)
for(int i=1;i<=vex;++i)
for(int j=1;j<=vex;++j)
{
d[k].value[i][j]=min(d[k-1].value[i][j],(d[k-1].value[i][k])+d[k-1].value[k][j]);
}
for(int i=1;i<=vex;++i)
for(int j=1;j<=vex;++j)
{
cout<<"value:["<<i<<"]"<<"["<<j<<"] :"<<d[vex].value[i][j]<<endl;
}
}
};
int main()
{
graph gp;
gp.insert();
gp.floyd();
return 0;
}
每对顶点间的最短路径2
最新推荐文章于 2023-03-07 23:09:55 发布