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
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?
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].
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".
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
Sorry
#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)的更多相关文章
- hdu 2807 The Shortest Path(矩阵+floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 2807 The Shortest Path
http://acm.hdu.edu.cn/showproblem.php?pid=2807 第一次做矩阵乘法,没有优化超时,看了别人的优化的矩阵乘法,就过了. #include <cstdio ...
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- 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 ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- HDU 2224 The shortest path
The shortest path Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 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 ...
- (中等) 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 ...
- 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 ...
随机推荐
- 【翻译】《深入解析windows操作系统第6版下册》第10章:内存管理
[翻译]<深入解析windows操作系统第6版下册>第10章:内存管理(第一部分) [翻译]<深入解析windows操作系统第6版下册>第10章:内存管理(第二部分) [翻译] ...
- netbeans环境的建立
这是一个简单的实验,熟悉NetBeans的IDE环境的开发 首先下载一个NetBeans,可以在官网上下https://netbeans.org/downloads/index.html 要装NetB ...
- [转]-Gradle使用手册(一):为什么要用Gradle?
原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Using-sourceCompatibility-1. ...
- Ant build ${renderscript.opt.level}问题解决方案
问题如下: BUILD FAILEDD:\adt-bundle-windows-x86_64-20131030\sdk\tools\ant\build.xml:653: The following e ...
- iOS - Swift SingleClass 单例类
前言 单例对象能够被整个程序所操作.对于一个单例类,无论初始化单例对象多少次,也只能有一个单例对象存在,并且该对象是全局的,能够被整个系统访问到. 单例类的创建 1.1 单例类的创建 1 单例类的创建 ...
- [转载] TLS协议分析 与 现代加密通信协议设计
https://blog.helong.info/blog/2015/09/06/tls-protocol-analysis-and-crypto-protocol-design/?from=time ...
- mysql批量替换数据库某字段部分内容
update 表名 set 字段名=replace(字段名,’要替换的内容’,’替换后的内容’) eg:修改scenario表中的picture字段中的ip地址. UPDATE scenario SE ...
- gO语言的安装和环境变量的配置
一.Go语言下载 go语言官方下载地址:https://golang.org/dl/ 找到适合你系统的版本下载,本人下载的是windows版本.也可以下载Source自己更深层次研究go语言. 二.G ...
- 【服务器环境搭建-Centos】系统分区 待续
df命令查看,显示/dev/vda,而不是sda或hda ,为什么? 虚拟机为了提升性能,一般使用virtio作为磁盘驱动,在虚拟机里面磁盘会显示成vda而不是传统的sda,有什么办法可以让他显示成s ...
- VS2013和VS2008项目的互通
VS2013和VS2008项目的互通,大家可能都查到了百度经验里面的一个帖子: http://jingyan.baidu.com/article/f54ae2fc3c3adc1e92b849de.ht ...