hdu 2807(矩阵+floyed)
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
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 <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)的更多相关文章
- hdu 4291 矩阵幂 循环节
http://acm.hdu.edu.cn/showproblem.php?pid=4291 凡是取模的都有循环节-----常数有,矩阵也有,并且矩阵的更奇妙: g(g(g(n))) mod 109 ...
- 2021.11.03 P2886 [USACO07NOV]Cow Relays G(矩阵+floyed)
2021.11.03 P2886 [USACO07NOV]Cow Relays G(矩阵+floyed) [P2886 USACO07NOV]Cow Relays G - 洛谷 | 计算机科学教育新生 ...
- 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 2807
http://acm.hdu.edu.cn/showproblem.php?pid=2807 把矩阵相乘放在第二重循环,第三重循环只进行比较可以水过,优化的方法不懂 主要用这题练习floyd的写法 # ...
- HDU 2855 (矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...
- HDU 4471 矩阵快速幂 Homework
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...
- HDU - 1575——矩阵快速幂问题
HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...
- hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...
随机推荐
- 学习python第四天 列表
模块的导入是使用 import sys#导入模块sysprint(sys.path)#打印环境变量,可能存在的目录print(sys.argv)#打印脚本的名字,相对路径 import os os.s ...
- B1051 复数乘法(15 分)
[PAT]B1051 复数乘法(15 分) - 路明天 - 博客园 https://www.cnblogs.com/hebust/p/9496809.html 在此对四舍五入输出结果做总结. 对于do ...
- poj 3273 分期问题 最大化最小值
题意:将N个账款分给城M个财务期,使得每个分期账款和的值最大? 思路: 每次mid为分期账款 如果分期次数小于m说明mid太大,减上限 反正 增下限 开始下限设为 最大值 上限设为和 解决问题的代码 ...
- 快速排序算法(C)
sort快排函数的基本版,效率n*logn,快排的完全版就是在递归之中夹杂对序列的预判断,最优的选择排序方法,快速排序算法只是其中之一. 简单的说明一下快速排序的思想,对于一个数列,首先选择一个基数( ...
- VBA连接到SQL2008需要加上端口号
VBA连接到SQL2008需要加上端口号1433,比如 conn = "server=XXXX.XXXX.XXXX.XXXX,1433;provider=SQLOLEDB.1;databas ...
- install golang plugin in webstrom
https://github.com/go-lang-plugin-org/go-lang-idea-plugin/wiki/Documentation
- Linux设置运行core dump
系统配置vim /etc/sysctl.conf kernel.core_uses_pid = kernel.core_pattern = %e-core-%p-%t sysctl -p检查有没有生效 ...
- koa2 + webpack 热更新
网上有很多express+webpack的热更新,但是koa2的很少,这两天研究了一下子,写一个简单的教程. 1.需要的包 webpack:用于构建项目 webpack-dev-middleware: ...
- OOP & DOM
OOP & DOM let Dom = Dom || {}; Dom = { checkValType(val) { let typeString = Object.prototype.toS ...
- 【bzoj4668】冷战 并查集按秩合并+朴素LCA
题目描述 1946 年 3 月 5 日,英国前首相温斯顿·丘吉尔在美国富尔顿发表“铁幕演说”,正式拉开了冷战序幕. 美国和苏联同为世界上的“超级大国”,为了争夺世界霸权,两国及其盟国展开了数十年的斗争 ...