#include<iostream>
#define N 10
using namespace std;
int min(int x,int y)
{
return x<y? x:y ;
}
struct weight
{
int v[N][N];
};
struct graph
{
friend struct weight;
int vex;
int edge;
// int* p[N][N];
weight wt;
graph()
{
int v,e;
cout<<"input the vex :"<<endl;
cin>>v;
vex=v;
cout<<"input the edge :"<<endl;
cin>>e;
edge=e;
cout<<"init the weight of all the edge"<<endl;
for(int i=1;i<=vex;++i)
for(int j=1;j<=vex;++j)
{
if(i==j)
wt.v[i][j]=0;
else
wt.v[i][j]=0xfffffff;
p[i][j]=NULL;
}
}
void insert()
{
cout<<"insert the weight of the edge....."<<endl;
for(int i=1;i<=edge;++i)
{
int start,end,w;
cout<<"input the start point of the edge : ";
cin>>start;
cout<<" and the end point of the edge : ";
cin>>end;
cout<<"input the weight of the edge : ";
cin>>w;
wt.v[start][end]=w;
cout<<endl;
}
}
void printv(weight &wt)
{
for(int i=1;i<=vex;++i)
for(int j=1;j<=vex;++j)
cout<<"value["<<i<<"]"<<"["<<j<<"] :"<<wt.v[i][j]<<endl;
}
weight shortpath(weight& a)
{
weight al;
for(int i=1;i<=vex;++i)
for(int j=1;j<=vex;++j)
{
al.v[i][j]=0xfffffff;
for(int k=1;k<=vex;++k)
{
al.v[i][j]=min(al.v[i][j],(a.v[i][k]+wt.v[k][j]));
}
}
return al;
}
weight slowshortestpath()
{
weight q[N];
q[1]=wt;
for(int m=2;m!=vex;++m)
{
q[m]=shortpath(q[m-1]);
}
cout<<"finished !"<<endl;
return q[vex-1];
}//
int main()
{
graph gp;
gp.insert();
weight wv=gp.slowshortestpath();
gp.printv(wv);
cout<<"---------------------------"<<endl;
return 0;
}
每对顶点最短路径(1)
最新推荐文章于 2023-03-07 23:09:55 发布