hdu-----(2807)The Shortest Path(矩阵+Floyd)
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
HDU 2009-4 Programming Contest
题目很清楚:
如果满足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=82;
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=1;i<=n;i++) //城市初始化
16 {
17 for(int j=1;j<=n;j++)
18 {
19 if(i==j)a[i][j]=0;
20 else a[i][j]=inf;
21 }
22 }
23 }
24
25 void floyd(int a[][maxn]) //运用floyd算法求城市间的最短路径
26 {
27 for(int k=1;k<=n;k++)
28 {
29 for(int i=1;i<=n;i++)
30 {
31 for(int j=1;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=1;i<=m;i++)
43 {
44 for(int j=1;j<=m;j++)
45 {
46 a[i][j]=0; // init()
47 for(int k=1;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=1;i<=n;i++)
59 {
60 for(int j=1;j<=n;j++)
61 {
62 if(i==j) continue; //a,b 两数组不能相同
63 Matrix(tem,i,j); //两个矩阵相乘
64 for(t1=1;t1<=n;t1++)
65 {
66 //a,b,c三数组不能相同
67 if(t1!=i&&t1!=j)
68 {
69 for( t2=1;t2<=m;t2++)
70 {
71 for(t3=1;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]=1;
81 }
82 }
83 }
84 }
85 }
86
87 int main()
88 {
89 int a,b;
90 while(scanf("%d%d",&n,&m)&&n+m!=0)
91 {
92 for(int i=1;i<=n;i++)
93 for(int j=1;j<=m;j++)
94 for(int k=1;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("Sorryn");
105 else
106 printf("%dn",ans[a][b]);
107 }
108 }
109 return 0;
110 }
- WordPress主题下funtions.php的一段“恶意”代码
- eclipse/intellij idea 远程调试hadoop 2.6.0
- Jexus 5.8.2 正式发布为Asp.Net Core进入生产环境提供平台支持
- 区块链技术发展前景可观,银链原子链已开始投入正常交易
- 分布式服务框架 dubbo/dubbox 入门示例
- SmtpClient发送邮件遭遇The specified string is not in the form required for a subject.
- 彻底解决WordPress 中 半角全角字符转义 的问题
- 利用Spring的@Async异步处理改善web应用中耗时操作的用户体验
- Oracle中使用Entity Framework 6.x Code-First方式开发
- 边缘计算推动AI发展 未来能摆脱云计算吗?
- intellij idea 高级用法之:集成JIRA、UML类图插件、集成SSH、集成FTP、Database管理
- 微软.NET Core RC2正式发布,横跨所有平台
- 二帮主:央行数字货币的崛起,会给比特币带来什么影响
- cas原理介绍
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法