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

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
 
题意:先输入n,m,接着输入n个m*m的矩阵,再输入k表示k次询问,接下来输入k行输入x,y,询问x点是否能到y点,可以则输出最短路。 当矩阵 A*B = C 的时候,A点到C点的距离是1.
 
先处理矩阵,建图,此题数据只有85,直接用floyd就好。
 
附上代码:
 
 #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)的更多相关文章

  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. Java问题解读系列之String相关---String类的常用方法?

    今天的题目是:String类的常用方法? 首先,我们在eclipse中定义一个字符串,然后使用alt+/就会出现String类的所有方法,如下图所示: 下面我就挑选一些常用的方法进行介绍: 首先定义两 ...

  2. java opencv 4.0.1安装配置

    如果没有把dll扔到jdk会报错 Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: no ...

  3. mybatis中使用包装对象

    在实际的应用中,很多时候我们需要的查询条件都是一个综合的查询条件,因此我们需要对已经存在的实体进行再一次的包装,以方便我们进行查询操作,于是包装对象的作用就很明显了,在这里我举一个简单的例子 1.首先 ...

  4. xmlns详解(转载)

    我们经常会在网页中碰到形如<html xmlns=”http://www.w3.org/2001/xhtml”>这样的代码, 或在是android 编码中的main.xml中看到形如< ...

  5. 字符串的trim()用法

      trim() 方法会从一个字符串的两端删除空白字符.在这个上下文中的空白字符是所有的空白字符 (space, tab, no-break space 等) 以及所有行终止符字符(如 LF,CR). ...

  6. LintCode刷题笔记-- BackpackIII

    标签:动态规划 问题描述: Given n items with size Ai and value Vi, and a backpack with size m. What's the maximu ...

  7. 写论文,没数据?R语言抓取网页大数据

    写论文,没数据?R语言抓取网页大数据 纵观国内外,大数据的市场发展迅猛,政府的扶持也达到了空前的力度,甚至将大数据纳入发展战略.如此形势为社会各界提供了很多机遇和挑战,而我们作为卫生(医学)统计领域的 ...

  8. 【JZOJ5064】【GDOI2017第二轮模拟day2】友好城市 Kosarajo算法+bitset+ST表+分块

    题面 在Byteland 一共有n 座城市,编号依次为1 到n,这些城市之间通过m 条单向公路连接. 对于两座不同的城市a 和b,如果a 能通过这些单向道路直接或间接到达b,且b 也能如此到达a,那么 ...

  9. Mac上homebrew使用

    一.官方网址 Homebrew 二.目录 安装 查看帮助信息 查看版本 更新Homebrew自己 安装软件包 查询可更新的包 更新包 (formula) 清理旧版本 锁定不想更新的包 卸载安装包 查看 ...

  10. DirectX11笔记(八)--Direct3D渲染4--VERTEX SHADER

    原文:DirectX11笔记(八)--Direct3D渲染4--VERTEX SHADER 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u0103 ...