The Shortest Path

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2996    Accepted Submission(s): 980

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
 
暴力竟然过了,O(N^5)这题数据多水。。。要注意A,B,C矩阵不能够相同(A,B和A,C一般能够注意,主要是B,C不能相同)
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
using namespace std;
const int N = ;
const int INF= ;
struct Matrix
{
int v[N][N];
} M[N];
int n,m;
int graph[N][N];
Matrix mult(Matrix a,Matrix b)
{
Matrix temp;
memset(temp.v,,sizeof(temp.v));
for(int i=; i<m; i++)
{
for(int j=; j<m; j++)
{
for(int k=; k<m; k++)
{
temp.v[i][j] += a.v[i][k]*b.v[k][j];
}
}
}
return temp;
}
bool Judge(Matrix a,Matrix b)
{
for(int i=; i<m; i++)
{
for(int j=; j<m; j++)
{
if(a.v[i][j]!=b.v[i][j]) return false;
}
}
return true;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF,n+m)
{
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
if(i==j) graph[i][j] = ;
else graph[i][j]=INF;
}
}
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
for(int k=; k<m; k++)
{
scanf("%d",&M[i].v[j][k]);
}
}
}
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
if(i!=j)
{
Matrix t = mult(M[i],M[j]);
for(int k=; k<n; k++)
{
if(i!=k&&j!=k&&Judge(t,M[k])) ///这里要注意:矩阵i,j,k不能够相同
{
graph[i][k]=;
}
}
}
}
}
for(int k=; k<n; k++)
{
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
graph[i][j] = min(graph[i][j],graph[i][k]+graph[k][j]);
}
}
}
int t ;
scanf("%d",&t);
while(t--)
{
int a,b;
scanf("%d%d",&a,&b);
a--,b--;
if(graph[a][b]>=INF) printf("Sorry\n");
else printf("%d\n",graph[a][b]);
}
}
}
 

hdu 2807(矩阵+floyed)的更多相关文章

  1. hdu 4291 矩阵幂 循环节

    http://acm.hdu.edu.cn/showproblem.php?pid=4291 凡是取模的都有循环节-----常数有,矩阵也有,并且矩阵的更奇妙: g(g(g(n))) mod 109  ...

  2. 2021.11.03 P2886 [USACO07NOV]Cow Relays G(矩阵+floyed)

    2021.11.03 P2886 [USACO07NOV]Cow Relays G(矩阵+floyed) [P2886 USACO07NOV]Cow Relays G - 洛谷 | 计算机科学教育新生 ...

  3. hdu 2807 The Shortest Path(矩阵+floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. hdu 2807 The Shortest Path

    http://acm.hdu.edu.cn/showproblem.php?pid=2807 第一次做矩阵乘法,没有优化超时,看了别人的优化的矩阵乘法,就过了. #include <cstdio ...

  5. HDU 2807

    http://acm.hdu.edu.cn/showproblem.php?pid=2807 把矩阵相乘放在第二重循环,第三重循环只进行比较可以水过,优化的方法不懂 主要用这题练习floyd的写法 # ...

  6. HDU 2855 (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...

  7. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  8. HDU - 1575——矩阵快速幂问题

    HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...

  9. hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...

随机推荐

  1. 华为liteos了解(一)

    我来补充一下,从@華仔答案的提供的wiki中看,随便翻了一下,内核部分和协议栈的接口部分代码风格完全不一致.协议栈和相关的代码应该是直接使用或者修改了uIP (micro IP)的实现,所以整体感觉应 ...

  2. opencv使用日记之一:平台搭建Mat类以及图像的读取修改

    平台搭建就摸了一整天时间,真的是...不说了,最后我选择的是 opencv3.0(2015/06/04)  + win7 + vs2012   注意opencv的版本不同导入的库文件是不一样的,所以请 ...

  3. HDFS上传文件

    1.client端向namenode请求上传文件,查看文件是否存在,是否有权限往hdfs写入 2.如果文件不存在,权限OK就根据副本数N(例如2个),根据网络拓扑选择N个离client端最近的data ...

  4. TopK-微博今日热门话题

    大纲 TopK on single node TopK on multiple nodes Realtime topK with low QPS Realtime topK with high QPS ...

  5. C#中静态变量和 静态方法的作用

    1.静态变量 在C#程序中,没有全局变量的概念,这意味着所有的成员变量只有该类的实例才能操作这些数据,这起到了“信息隐藏”的作用.但有些时候,这样做却不是个明智的选择. 假设我们要定义一个图书类,要求 ...

  6. jQuery动态显示和隐藏datagrid中的某一列的方法

    在EasyUI中: 1)展示某列的方法:     $('#jgrid').datagrid('showColumn', 'XXX');  -----其中 XXX 是隐藏列的  field 属性值 2) ...

  7. 查看apache和nginx的负载和连接数情况

    1.查看apache当前并发访问数:netstat -an | grep ESTABLISHED | wc -l对比httpd.conf中MaxClients的数字差距多少. 2.查看有多少个进程数: ...

  8. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Khamovniki Problem J Stairways解题报告(分块+维护凸壳)

    首先ORZ一发Claris聚聚的题解:http://www.cnblogs.com/clrs97/p/8689215.html,不然我可能没机会补过这道神题了. 这里写一个更详细的题解吧(我还是太菜了 ...

  9. 求中位数为K的区间的数目

    给定一个长为 $n$ 的序列和常数 $k$,求此序列的中位数为 $k$ 的区间的数量.一个长为 $m$ 的序列的中位数定义为将此序列从小到大排序后第 $\lceil m / 2 \rceil$ 个数. ...

  10. Bolzano-Weierstrass 定理

    这个定理是从吴崇试老师的数学物理方法课里看到的,表述如下: 有界的无穷(复数)序列至少有一个聚点. 序列的聚点定义为 给定序列 $\{z_n\}$,若存在复数 $z$,对于任意给定的 $\vareps ...