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 格式化
数字前面补0 字符型: print('23'.zfill(5)) 数字型: print('%011d' % 124) 日期与str互转: datetime 转 str str_date = datet ...
- 684. Redundant Connection
https://leetcode.com/problems/redundant-connection/description/ Use map to do Union Find. class Solu ...
- UVA:11297-Census(二维线段树)
Census Time Limit: 8 sec Description This year, there have been many problems with population calcul ...
- 6、python中的元组
元组(tuple)是python中有序.不可变的数据结构.元组还是python四种数据结构中唯一一种不可变的数据结构. 一.前言 元组在很多方面都变现得跟列表一样,除了列表储存得对象是可变得,而元组储 ...
- Linux命令之---find
命令简介 find明林用于查找目录下的文件,同时也可以调用其他命令执行相应的操作 命令格式 find pathname -options [-print -exec -ok ...] find [选项 ...
- 51、如何提取android代码中的字符串为系统资源文件 (I18N)
工具:android studio 步骤1:找到要转为资源文件的字符串并选中,同时按下option+enter,弹出菜单,我们选中extract string resource 步骤2:在弹窗中输入你 ...
- MD5碰撞
if ( $_POST['param1'] !==$_POST['param2'] && md5($_POST['param1']) === md5($_POST['param2']) ...
- [DM8168]Linux下控制GPIO控制12864液晶屏(ST7565控制器)
首先加载驱动模块,应用程序通过调用API实现GPIO控制功能. 驱动函数: /* * fileName: st7565_driver.c * just for LCD12864 driver * GP ...
- 关于mybitis的异常总结
由于原项目中系统登录用户表中新添加了字段来关联其他表,但原来的mapper和bean就得重新再逆向出来,逆向后,就参照着原来你mapper来添加一些自定义在mapper的方法,那么接下来就爆出异常了 ...
- 团队Alpha版本冲刺(一)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内 ...