HDU2807:The Shortest Path(矩阵乘法+快速矩阵比较+Floyd)

本文介绍了一种通过优化矩阵运算来快速判断两个城市间是否存在路径的方法,并实现了一个算法用于解决此问题。通过将矩阵乘法从三维压缩到二维,显著提高了效率。
Problem Description
There are N cities in the country. Each city is represent by a matrix size of M*M. If city A, B and C satisfy that A*B = C, we say that there is a road from A to C with distance 1 (but that does not means there is a road from C to A).
Now the king of the country wants to ask me some problems, in the format:
Is there is a road from city X to Y?
I have to answer the questions quickly, can you help me?
 

Input
Each test case contains a single integer N, M, indicating the number of cities in the country and the size of each city. The next following N blocks each block stands for a matrix size of M*M. Then a integer K means the number of questions the king will ask, the following K lines each contains two integers X, Y(1-based).The input is terminated by a set starting with N = M = 0. All integers are in the range [0, 80].
 

Output
For each test case, you should output one line for each question the king asked, if there is a road from city X to Y? Output the shortest distance from X to Y. If not, output "Sorry".
 

Sample Input
3 2 1 1 2 2 1 1 1 1 2 2 4 4 1 1 3 3 2 1 1 2 2 1 1 1 1 2 2 4 3 1 1 3 0 0
 

Sample Output
1 Sorry
 

可以暴力破题,但这样的话,就成水题了,也很容易超时(貌似G++可以保证1200+ms解决)

看了网上题解才学会如何用一维矩阵去优化,这个优化主要体现在a*b 与c相比O(m^2)转化为O(m),矩阵相乘a*b的O(n^3)转化为O(n^2)

其实主要是根据若a*b=c,则a*b*temp=c*temp,temp是一维矩阵[1,....m],即1到m行,这样就全部转化为一维矩阵去判断了


#include<stdio.h>
#define N 100
#define inf 9999999
int n,m;
int map[N][N],arr[N][N][N],tem[N][N];
void  cmp(int a,int c)
{
	for(int i=1;i<=m;i++)
		if(tem[0][i]!=tem[c][i])
			return ;
	map[a][c]=1;
}
void GreatMap(int a,int b)
{
	int i,j;
	for(i=1;i<=m;i++)
	{
		tem[0][i]=0;
		for(j=1;j<=m;j++)
			tem[0][i]+=arr[a][i][j]*tem[b][j];//也是优化的关键之一,跟下面的意思是一样
	}

	//矩阵比较,比较除a、b以外的矩阵是否与a*b所得的矩阵相等
	for(i=1;i<=n;i++)
	{
		if(i==a||i==b)  continue;
		cmp(a,i);
	}

}
int main()
{
	while(scanf("%d%d",&n,&m),n+m)
	{
		int i,j,k;
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
			{
				tem[i][j]=0;
				for(k=1;k<=m;k++)
				{
					scanf("%d",&arr[i][j][k]);
					//下一步是优化的关键之一:k代表一个m*1的列矩阵[1.....m],arr[i][j][k]表示第i个m*m的矩阵
					//arr[i][j][k]*k,就成了m*1的列矩阵,即tem[i][j](i表第i个矩阵,j表示该列矩阵中对应数字的位置)
					tem[i][j]+=arr[i][j][k]*k;
				}
			}
		for(i=1;i<=n;i++)
		{
			//map[i][i]=0;
			for(j=i+1;j<=n;j++)
				map[i][j]=map[j][i]=inf;
		}
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
			{
				if(i==j) continue;
				GreatMap(i,j);
			}
		for(k=1;k<=n;k++)
			for(i=1;i<=n;i++)
				for(j=1;j<=n;j++)
					if(map[i][j]>map[i][k]+map[k][j])
						map[i][j]=map[i][k]+map[k][j];
		scanf("%d",&n);
		int s,e;
		while(n--)
		{
			scanf("%d%d",&s,&e);
			if(map[s][e]==inf)
				puts("Sorry");
			else
				printf("%d\n",map[s][e]); 
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值