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 * ...
随机推荐
- 748. Shortest Completing Word
https://leetcode.com/problems/shortest-completing-word/description/ class Solution { public: string ...
- AutoMapper教程
http://www.cnblogs.com/gc2013/p/4487567.html http://www.qeefee.com/article/automapper
- [netty4][netty-transpot]Channel体系分析
Channel体系分析 接口与类结构体系 -- [I]AttributeMap, ChannelOutboundInvoker, Comparable -- [I]AttributeMap ---- ...
- putty的基本使用
1.输入你要连接的目标的IP地址,输入你要给它取的名字,点击保存 2.选中你保存的会话,点击打开,即可打开会话 3.输入你连接的目标的用户账号,回车,再输入密码.即可正常使用. 4.编码的设置 在出现 ...
- 我对于js注入的理解
资料:http://blog.csdn.net/gisredevelopment/article/details/41778671 js注入就是在前端利用使用js的地方 在这其中注入你写的js代码 使 ...
- Android TextWatcher的使用方法(监听ExitText的方法)
我做了一个查询单词的简单app, 当在EditText中输入单词的时候,点击lookup,则在TextView区域显示出该单词的意思,当EditText中没有任何字符时,显示"word de ...
- 数据结构设计 Stack Queue
之前在简书上初步总结过几个有关栈和队列的数据结构设计的题目.http://www.jianshu.com/p/d43f93661631 1.线性数据结构 Array Stack Queue Hash ...
- windows下命令行
创建文件夹 mkdir 文件夹名字 创建文件 echo >文件名字 输入文件内容
- httpClient get方式抓取数据
/* * 爬取网页信息 */ private static String pickData(String url) { CloseableHttpClient ht ...
- hihoCoder #1783 又一个重复计数
题目大意 给定一个长度为 $n$ 的字符串 $S$,定义函数 $f(S)$ 表示 $S$ 的不同回文子串的个数.对于 $1\le l \le r \le n$,定义 $S[l,r]$ 为字符串 $S$ ...