您的位置:首页 > 其它

The Shortest Path(矩阵快速幂构造有向图+floyed算法求图多源最短路)

2015-08-05 22:03 393 查看
Link:http://acm.hdu.edu.cn/showproblem.php?pid=2807


The Shortest Path

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2856 Accepted Submission(s): 911



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




Source

HDU 2009-4 Programming Contest



AC code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#define LL long long 
#define MAXN 1000010
using namespace std;
const int INF=0x3f3f3f3f;
//----以下为矩阵快速幂模板-----// 
//const int mod=1000;//模3,故这里改为3即可 
int mod=1000;
int n,m,k;
int dis[81][81];
const int NUM=81;//定义矩阵能表示的最大维数 
int N;//N表示矩阵的维数,以下的矩阵加法、乘法、快速幂都是按N维矩阵运算的 
struct Mat{//矩阵的类
	int a[NUM][NUM];
	Mat(){memset(a,0,sizeof(a));}  
	void init()//将其初始化为单位矩阵  
	{
		memset(a,0,sizeof(a));
		for(int i=0;i<NUM;i++)
		{
			a[i][i]=1;
		}
	}
};
Mat A[81];
Mat add(Mat a,Mat b)//(a+b)%mod  矩阵加法  
{
	Mat ans;
	for(int i=0;i<N;i++)
	{
		for(int j=0;j<N;j++)
		{
			ans.a[i][j]=(a.a[i][j]%mod)+(b.a[i][j]%mod);
			ans.a[i][j]%=mod;
		}
	}
	return ans;
}
Mat mul(Mat a,Mat b) //(a*b)%mod  矩阵乘法  
{
	Mat ans;
	for(int i=1;i<=N;i++)
	{
		for(int j=1;j<=N;j++)
		{
			ans.a[i][j]=0;
			for(int k=1;k<=N;k++)
			{
				ans.a[i][j]+=(a.a[i][k]*b.a[k][j]);
			}
			//ans.a[i][j]%=mod;
		}
	}
	return ans;
}
Mat power(Mat a,int num)//(a^n)%mod  矩阵快速幂 
{
	Mat ans;
	ans.init();
	while(num)
	{
		if(num&1)
		{
			ans=mul(ans,a);
		}
		num>>=1;
		a=mul(a,a);
	}
	return ans;
}
Mat pow_sum(Mat a,int num)//(a+a^2+a^3....+a^n)%mod 矩阵的幂和
{
	int m;
	Mat ans,pre;
	if(num==1)
		return a;
	m=num/2;
	pre=pow_sum(a,m);
	ans=add(pre,mul(pre,power(a,m)));
	if(num&1)
		ans=add(ans,power(a,num));
	return ans;
}
void output(Mat a)//输出矩阵 
{
	for(int i=1;i<=N;i++)
	{
		for(int j=1;j<=N;j++)
		{
			printf("%d%c",a.a[i][j],j==N-1?'\n':' ');
		}
	}
}
//----以上为矩阵快速幂模板-----// 
void floy()
{
	int i,j,k;
	for(k=1;k<=n;k++)
	{
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				if(i==j||i==k||j==k)
				{
					continue;
				}
				if(dis[i][j]>dis[i][k]+dis[k][j])
				{
					dis[i][j]=dis[i][k]+dis[k][j];
				}
			}
		}
	}
}
int main()
{
	//freopen("D:\in.txt","r",stdin);
	int x,y,i,j,ai;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		if(n==0&&m==0)
			break;
		N=m;
		for(ai=1;ai<=n;ai++)
		{
			for(i=1;i<=m;i++)
			{
				for(j=1;j<=m;j++)
				{
					scanf("%d",&A[ai].a[i][j]);
				}
			}
		}
		memset(dis,INF,sizeof(dis));
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				if(i==j)
					continue;
				Mat ans=mul(A[i],A[j]);
				int fg=1;
				for(ai=1;ai<=n;ai++)
				{
					if(ai==i||ai==j)	continue;//jianzhi
					fg=1;
					for(int ii=1;ii<=N;ii++)
					{
						for(int jj=1;jj<=N;jj++)
						{
							if(ans.a[ii][jj]!=A[ai].a[ii][jj])
						    {
						    	fg=0;
						    	break;
							}
						}
						if(!fg)
							break;
					}
					if(fg)
					{
						dis[i][ai]=1;
						//break;
					}	
				}
			}
		}
		floy();
		scanf("%d",&k);
		while(k--)
		{
			scanf("%d%d",&x,&y);
			if(dis[x][y]>=INF)
				printf("Sorry\n");
			else
				printf("%d\n",dis[x][y]);
		}
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: