The Shortest Path(矩阵快速幂构造有向图+floyed算法求图多源最短路)
2015-08-05 22:03
393 查看
Link:http://acm.hdu.edu.cn/showproblem.php?pid=2807
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
Sample Output
Source
HDU 2009-4 Programming Contest
AC code:
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; }
相关文章推荐
- 字符串和整数之间的相互转化
- Android - Fragment(二)加载Fragment
- JSP基础
- leetcode 092 —— Reverse Linked List II
- window平台单进程多线程服务器通信
- poj 1251 Jungle Roads 解题报告(kruskal+prim)
- machine learning in coding(python):pandas数据包DataFrame数据结构简介
- 系统设计
- JAVA监听
- KMP算法中next数组的手工计算方法
- 签名书项目
- YUV格式学习:YUV444转换RGB24
- 使用Device Farm真机测试Android程序
- struts2中ajax(jQuery)返回值data中文乱码问题
- xode自动对齐快捷键
- Java心得7
- iPhone开发入门系列1(iOS8+Swift版)天天打靶APP学习07-09
- Leetcode: House Robber
- Extended ActionBar沉浸式状态栏(顶部状态栏填充与actionBar颜色相同)
- jquery autocomplete 自动补全