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): 3164 Accepted Submission(s):
1030
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?
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].
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".
Sorry
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 85
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
int map[N][N];
int a[N][N][N],tem[N][N]; void floyd()
{
int i,j,k;
for(k=; k<=n; k++)
for(i=; i<=n; i++)
for(j=; j<=n; j++)
if(map[i][j]>map[i][k]+map[k][j])
map[i][j]=map[i][k]+map[k][j];
} void getmap()
{
int i,j,k,x,y,z;
for(i=; i<=n; i++)
for(j=; j<=m; j++)
for(k=; k<=m; k++)
scanf("%d",&a[i][j][k]);
for(i=; i<=n; i++)
for(j=; j<=n; j++)
if(i==j) map[i][j]=;
else map[i][j]=INF;
for(i=; i<=n; i++)
{
for(j=; j<=n; j++)
{
if(i==j) continue;
memset(tem,,sizeof(tem));
for(x=; x<=m; x++)
for(y=; y<=m; y++)
{
tem[x][y]=;
for(z=; z<=m; z++) ///矩阵计算
tem[x][y]+=a[i][x][z]*a[j][z][y];
}
for(x=; x<=n; x++)
{
if(x==i||x==j)
continue;
int flag=;
for(y=; y<=m; y++)
{
for(z=; z<=m; z++)
{
if(tem[y][z]!=a[x][y][z]) ///比较是否完全相同
{
flag=;
break;
}
}
if(!flag)
break;
}
if(flag)
map[i][x]=;
}
}
}
floyd();
}
int main()
{
int i,j,k,x,y;
while(~scanf("%d%d",&n,&m))
{
if(n==&&m==)
break;
getmap();
scanf("%d",&k);
while(k--)
{
scanf("%d%d",&x,&y);
if(map[x][y]<INF)
printf("%d\n",map[x][y]);
else
printf("Sorry\n");
}
}
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 ...
随机推荐
- torch.backends.cudnn.benchmark = true 使用情形
设置这个 flag 可以让内置的 cuDNN 的 auto-tuner 自动寻找最适合当前配置的高效算法,来达到优化运行效率的问题. 应该遵循以下准则: 如果网络的输入数据维度或类型上变化不大,设置 ...
- js中的如何定位固定层的位置
需要获取一些HTML的对象的坐标来更灵活的设置目标层的坐标,这里可以通过用到document.body.scrollTop等属性,但是这些属性在xhtml的标准网页中或更简单的说就是带<!DOC ...
- SaaS launch Kit成回收宝和友盟云合作纽带,帮助提升3倍上云效率
导语:叶飞表示,全球二手手机市场未来几年将发生巨大变革, 回收宝正进行积极布局.与阿里云开展紧密技术合作,回收宝期待成为这一变革的引领者. 7月26日,在阿里云上海峰会上,阿里云了发布SaaS生态战略 ...
- 转: CentOS上安装LAMP之第二步:PHP环境及安装过程报错解决方案(纯净系统环境)
最近有空就配置CentOS系统上的AMP环境,现在配置到PHP环境了 多话不说上传送门:http://blog.csdn.net/zhangatle/article/details/77447653 ...
- Poj 2796 单调栈
关于单调栈的性质,和单调队列基本相同,只不过单调栈只使用数组的尾部, 类似于栈. Accepted Code: /******************************************* ...
- Codeforces 113B
题目链接 B. Petr# time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- php thrift TServerSocket实现端口复用
<?php namespace Message\Controller; use Think\Controller; use Thrift\Exception\TException; use Th ...
- 【JZOJ3635】【BOI2012】Peaks
╰( ̄▽ ̄)╭ 有一个居住在多山岛屿的登山家,已经攀上了一座山峰,并且要攀爬另外一座更高的山峰. 更精确地说,岛上的每一点都有一个大于零的海拔(海面的海拔为零),并且如果登山家位于海拔Ei的山峰上,那 ...
- python变量、变量属性
1.简述执行Python程序的两种方式以及他们的优缺点 答:交互型优点:马上就能看到结果,排错方便.交互型缺点:代码无法保存,断电即消失 命令型优点:代码永久保存.命令型缺点:不易排错 2.简述Pyt ...
- DOM事件机制
前言 本文主要介绍DOM事件级别.DOM事件模型.事件流.事件代理和Event对象常见的应用,希望对你们有些帮助和启发! 本文首发地址为GitHub博客,写文章不易,请多多支持与关注! 一.DOM事件 ...