The Shortest Path

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2440    Accepted Submission(s): 784

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
 
 
题目很清楚: 
如果满足A*B=C 那么就说A到C是联通的....
反之则为不连通...
这里需要用到的求最短路径,对于稠密图的,用弗洛伊德算法比狄斯喹诺算法要好一点。
至于要优化,看来结题报告之后,觉得还是不大靠谱,就没有写,写的是一个朴素的矩阵相乘算法+floyd算法
代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #define inf 0x3f3f3f3f
  4. using namespace std;
  5.  
  6. const int maxn=;
  7. int arr[maxn][maxn][maxn];
  8. int ans[maxn][maxn];
  9. int tem[maxn][maxn];
  10.  
  11. int n,m,w;
  12.  
  13. void init(int a[][maxn])
  14. {
  15. for(int i=;i<=n;i++) //城市初始化
  16. {
  17. for(int j=;j<=n;j++)
  18. {
  19. if(i==j)a[i][j]=;
  20. else a[i][j]=inf;
  21. }
  22. }
  23. }
  24.  
  25. void floyd(int a[][maxn]) //运用floyd算法求城市间的最短路径
  26. {
  27. for(int k=;k<=n;k++)
  28. {
  29. for(int i=;i<=n;i++)
  30. {
  31. for(int j=;j<=n;j++)
  32. {
  33. if(ans[i][j]>ans[i][k]+ans[k][j])
  34. ans[i][j]=ans[i][k]+ans[k][j];
  35. }
  36. }
  37. }
  38. }
  39.  
  40. void Matrix(int a[][maxn],int p1,int p2)
  41. {
  42. for(int i=;i<=m;i++)
  43. {
  44. for(int j=;j<=m;j++)
  45. {
  46. a[i][j]=; // init()
  47. for(int k=;k<=m;k++)
  48. {
  49. a[i][j]+=arr[p1][i][k]*arr[p2][k][j];
  50. }
  51. }
  52. }
  53. }
  54.  
  55. void work()
  56. {
  57. int t1,t2,t3;
  58. for(int i=;i<=n;i++)
  59. {
  60. for(int j=;j<=n;j++)
  61. {
  62. if(i==j) continue; //a,b 两数组不能相同
  63. Matrix(tem,i,j); //两个矩阵相乘
  64. for(t1=;t1<=n;t1++)
  65. {
  66. //a,b,c三数组不能相同
  67. if(t1!=i&&t1!=j)
  68. {
  69. for( t2=;t2<=m;t2++)
  70. {
  71. for(t3=;t3<=m;t3++)
  72. {
  73. //得到的结果相比较
  74. if(tem[t2][t3]!=arr[t1][t2][t3])
  75. goto loop;
  76. }
  77. }
  78. loop:
  79. if(t3>m)
  80. ans[i][t1]=;
  81. }
  82. }
  83. }
  84. }
  85. }
  86.  
  87. int main()
  88. {
  89. int a,b;
  90. while(scanf("%d%d",&n,&m)&&n+m!=)
  91. {
  92. for(int i=;i<=n;i++)
  93. for(int j=;j<=m;j++)
  94. for(int k=;k<=m;k++)
  95. scanf("%d",&arr[i][j][k]);
  96. init(ans);
  97. work();
  98. floyd(ans);
  99. scanf("%d",&w);
  100. while(w--)
  101. {
  102. scanf("%d%d",&a,&b);
  103. if(ans[a][b]==inf)
  104. printf("Sorry\n");
  105. else
  106. printf("%d\n",ans[a][b]);
  107. }
  108. }
  109. return ;
  110. }

hdu-----(2807)The Shortest Path(矩阵+Floyd)的更多相关文章

  1. hdu 2807 The Shortest Path(矩阵+floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. hdu 2807 The Shortest Path

    http://acm.hdu.edu.cn/showproblem.php?pid=2807 第一次做矩阵乘法,没有优化超时,看了别人的优化的矩阵乘法,就过了. #include <cstdio ...

  3. Hdu 4725 The Shortest Path in Nya Graph (spfa)

    题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...

  4. HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]

    HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...

  5. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

  6. HDU 2224 The shortest path

    The shortest path Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. HDU 4725 The Shortest Path in Nya Graph

    he Shortest Path in Nya Graph Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged o ...

  8. (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  9. HDU 4725 The Shortest Path in Nya Graph(构图)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

随机推荐

  1. js对数组排序

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. CodeForces 496B Secret Combination

    Secret Combination Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

  3. mysql中bigint、int、mediumint、smallint 和 tinyint的取值范围

    mysql数据库设计,其中,对于数据性能优化,字段类型考虑很重要,搜集了些资料,整理分享出来,这篇为有关mysql整型bigint.int.mediumint.smallint 和 tinyint的语 ...

  4. JS学习笔记(二) 数据类型

    参考资料: 1. http://www.w3school.com.cn/js/js_datatypes.asp 2. http://blog.sina.com.cn/s/blog_85c1dc1001 ...

  5. GIMP学习笔记

    参考文献: ① http://www.gimp.org/docs/userfaq.html GIMP是GNU Image Manipulation Program的缩写,sired by Spence ...

  6. 访问Google搜索,Google学术镜像搜索

    Google学术镜像搜索:http://dir.scmor.com/google/ 不用FQ也能访问谷歌搜索网站,让我们一起Google 不用FQ也能访问谷歌搜索网站,让我们一起Google(摘自:h ...

  7. Java I/O NIO学习

    给出一个学习的链接讲的很全.. http://ifeve.com/java-nio-all/ 上边的是中文翻译的这里是原地址:http://tutorials.jenkov.com/java-nio/ ...

  8. EntityManager方法简介

    EntityManager 是用来对实体Bean 进行操作的辅助类.他可以用来产生/删除持久化的实体Bean,通过主键查找实体bean,也可以通过EJB3 QL 语言查找满足条件的实体Bean.实体B ...

  9. Windows Live Writer编写Octopress

    Windows live Writer是一个可以用来离线编写,并发布博客的工具. Octopress是一个静态博客生成系统.使用群体多是geek,主要有显示代码清晰,git同步,并且不用购买空间的特点 ...

  10. 禁止ubuntu的super快捷键

    在mac上安装了ubuntu虚拟机, 但是发现command健(ubuntu中叫super健)被系统占用了, 习惯了command健的同学来说非常不方便, 如何禁用默认的command健呢? You ...