每对顶点最短路径(1)

 #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;	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值