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算法
代码:

 #include<cstdio>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std; const int maxn=;
int arr[maxn][maxn][maxn];
int ans[maxn][maxn];
int tem[maxn][maxn]; int n,m,w; void init(int a[][maxn])
{
for(int i=;i<=n;i++) //城市初始化
{
for(int j=;j<=n;j++)
{
if(i==j)a[i][j]=;
else a[i][j]=inf;
}
}
} void floyd(int a[][maxn]) //运用floyd算法求城市间的最短路径
{
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(ans[i][j]>ans[i][k]+ans[k][j])
ans[i][j]=ans[i][k]+ans[k][j];
}
}
}
} void Matrix(int a[][maxn],int p1,int p2)
{
for(int i=;i<=m;i++)
{
for(int j=;j<=m;j++)
{
a[i][j]=; // init()
for(int k=;k<=m;k++)
{
a[i][j]+=arr[p1][i][k]*arr[p2][k][j];
}
}
}
} void work()
{
int t1,t2,t3;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(i==j) continue; //a,b 两数组不能相同
Matrix(tem,i,j); //两个矩阵相乘
for(t1=;t1<=n;t1++)
{
//a,b,c三数组不能相同
if(t1!=i&&t1!=j)
{
for( t2=;t2<=m;t2++)
{
for(t3=;t3<=m;t3++)
{
//得到的结果相比较
if(tem[t2][t3]!=arr[t1][t2][t3])
goto loop;
}
}
loop:
if(t3>m)
ans[i][t1]=;
}
}
}
}
} int main()
{
int a,b;
while(scanf("%d%d",&n,&m)&&n+m!=)
{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
for(int k=;k<=m;k++)
scanf("%d",&arr[i][j][k]);
init(ans);
work();
floyd(ans);
scanf("%d",&w);
while(w--)
{
scanf("%d%d",&a,&b);
if(ans[a][b]==inf)
printf("Sorry\n");
else
printf("%d\n",ans[a][b]);
}
}
return ;
}

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. [POJ1830]开关问题(高斯消元,异或方程组)

    题目链接:http://poj.org/problem?id=1830 题意:中文题面,求的是方案数. 首先可以知道, 如果方案数不止一个的话,说明矩阵行列式值为0,即存在自由变元,由于变量只有两种状 ...

  2. debian attempt to kill init!

    之前想在debian下安装Oracle 11g,结果在安装之前要安装好多的依赖包. 在centos和redhat下可以很方便的使用yum来安装,哗啦啦一下全装完了, 后来我在debian下用apt-g ...

  3. 用t4模板和head.js进行css和js的版本控制

    head.js  介绍 http://headjs.com/site/api/v1.00.html#load 原文http://www.cnblogs.com/wang2650/p/5102690.h ...

  4. 加载xib文件

    // Test.xib --编译--> Test.nib // 方式1 NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"Te ...

  5. Linux基础01 学会使用命令帮助

    Linux基础01 学会使用命令帮助 概述 在linux终端,面对命令不知道怎么用,或不记得命令的拼写及参数时,我们需要求助于系统的帮助文档:linux系统内置的帮助文档很详细,通常能解决我们的问题, ...

  6. python_way day16 JQuary

    python_way day16 JQuery 封装dom js代码 jQuery(1.10,1.12-兼容性好,2.0.以后放弃了ie9以下) - 封装了Dom & JavaScript 查 ...

  7. python_way.day7 模块(configparser,xml,shutil,subprocess)、面向对象(上)(创建类,类的构成,函数式编程与面向对象编程的选择,类的继承)

      python_way.day7 1.模块 configparser,xml,shutil,subprocess 1.模块 a.configparser 用于处理特定格式的文件,其本职上使用open ...

  8. 《Linux内核设计的艺术》学习笔记(七)INT 0x15中断

    参考资料: 1. <IBM-PC汇编语言程序设计> 2. http://blog.sina.com.cn/s/blog_5028978101008wk2.html 3. http://ww ...

  9. iOS - Notification 通知

    1.Notification 通知中心实际上是在程序内部提供了消息广播的一种机制,它允许我们在低程度耦合的情况下,满足控制器与一个任意的对象进行通信的目的.每一个 iOS 程序(即每一个进程)都有一个 ...

  10. iOS - UIWebView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIWebView : UIView <NSCoding, UIScrol ...