每对顶点间的最短路径2

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值