zoj3497(经典矩阵乘法)
原以为是用搜索做的题,想了好久都无法想到一个高效正确的解法。
后面发现竟然这就是矩阵的应用! 碉堡!
给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值 ——选自matrix67
把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j。令C=A*A,那么C(i,j)=ΣA(i,k)*A(k,j),实际上就等于从点i到点j恰好经过2条边的路径数(枚举k为中转点)。类似地,C*A的第i行第j列就表示从i到j经过3条边的路径数。同理,如果要求经过k步的路径数,我们只需要二分求出A^k即可。
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Description
In chapter 4 of the game Trails in the Sky SC, Estelle Bright and her friends are crossing Mistwald to meet their final enemy, Lucciola.
Mistwald is a mysterious place. It consists of M * N scenes, named Scene (1, 1) to Scene (M, N). Estelle Bright and her friends are initially at Scene (1, 1), the entering scene. They should leave Mistwald from Scene (M, N), the exiting scene. Note that once they reach the exiting scene, they leave Mistwald and cannot come back. A scene in Mistwald has four exits, north, west, south, and east ones. These exits are controlled by Lucciola. They may not lead to adjacent scenes. However, an exit can and must lead to one scene in Mistwald.
Estelle Bright and her friends walk very fast. It only takes them 1 second to cross an exit, leaving a scene and entering a new scene. Other time such as staying and resting can be ignored. It is obvious that the quicker they leave Mistwald, the better.
Now you are competing with your roommate for who uses less time to leave Mistwald. Your roommate says that he only uses P seconds. It is known that he lies from time to time. Thus, you may want to code and find out whether it is a lie.
Input
There are multiple test cases. The first line of input is an integer T ≈ 10 indicating the number of test cases.
Each test case begins with a line of two integers M and N (1 ≤ M, N ≤ 5), separated by a single space, indicating the size of Mistwald. In the next M lines, the ith line contains N pieces of scene information, separated by spaces, describing Scene (i, 1) to Scene (i, N). A scene description has the form "((x1,y1),(x2,y2),(x3,y3),(x4,y4))" (1 ≤ xk ≤ M; 1 ≤ yk ≤ N; 1 ≤ k ≤ 4) indicating the locations of new scenes the four exits lead to. The following line contains an integer Q (1 ≤ Q ≤ 100). In the next Q lines, each line contains an integer P (0 ≤ P ≤ 100,000,000), which is the time your roommate tells you.
Test cases are separated by a blank line.
Output
For each P, output one of the following strings in one line: "True" if it cannot be a lie; "Maybe" if it can be a lie; "False" if it must be a lie.
Print a blank line after each case.
Sample Input
2
3 2
((3,1),(3,2),(1,2),(2,1)) ((3,1),(3,1),(3,1),(3,1))
((2,1),(2,1),(2,1),(2,2)) ((3,2),(3,2),(3,2),(3,2))
((3,1),(3,1),(3,1),(3,1)) ((3,2),(3,2),(3,2),(1,1))
3
1
2
10 2 1
((2,1),(2,1),(2,1),(2,1))
((2,1),(2,1),(2,1),(2,1))
2
1
2
Sample Output
Maybe
False
Maybe True
False
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std; int n,m;
bool g[][];
int x[],y[];
bool tg[][]; void mul(bool s[][],bool t[][])
{
bool tmp[][];
int top=n*m;
memset(tmp,,sizeof(tmp));
for(int k=;k<top;k++)
for(int i=;i<top;i++)
for(int j=;j<top;j++)
{
tmp[i][j]|=(s[i][k]&t[k][j]);
} for(int i=;i<top;i++)
for(int j=;j<top;j++)
s[i][j]=tmp[i][j]; } int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(g,,sizeof(g)); scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
int id=i*m+j;
scanf(" ((%d,%d),(%d,%d),(%d,%d),(%d,%d))",&x[],&y[],&x[],&y[],&x[],&y[],&x[],&y[]);
if(i!=n-||j!=m-)
{
for(int k=;k<=;k++)
{
int tid=(x[k]-)*m+y[k]-;
g[id][tid]=;
}
}
}
int q;
scanf("%d",&q);
while(q--)
{
int tmp;
scanf("%d",&tmp);
bool sum[][];
for(int i=;i<n*m;i++)
for(int j=;j<n*m;j++)
{
if(i==j) sum[i][j]=;
else sum[i][j]=;
tg[i][j]=g[i][j];
}
while(tmp)
{
if((tmp&)) mul(sum,tg);
mul(tg,tg);
tmp>>=;
}
if(sum[][n*m-]==) printf("False\n");
else
{
int flag=;
for(int i=;i<n*m-;i++)
{
if(g[][i]!=)
{
flag=;
break;
}
}
if(flag) printf("Maybe\n");
else printf("True\n");
}
}
printf("\n");
}
return ;
}
zoj3497(经典矩阵乘法)的更多相关文章
- poj3233之经典矩阵乘法
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 12346 Accepted: ...
- hdu1588之经典矩阵乘法
Gauss Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【矩阵乘法经典应用】【ZOJ3497】【Mistwa】
题意:给定一个有向图(最多25个节点,每个节点的出度最多为4),给定起点和终点,然后从起点开始走,走到终点就停止,否则一直往下走,问能不能P步到达终点.也就是说从起点出发,走一条长度为P的路径,路径中 ...
- 学习心得:《十个利用矩阵乘法解决的经典题目》from Matrix67
本文来自:http://www.matrix67.com/blog/archives/tag/poj大牛的博文学习学习 节选如下部分:矩阵乘法的两个重要性质:一,矩阵乘法不满足交换律:二,矩阵乘法满足 ...
- 【转】Matrix67:十个利用矩阵乘法解决的经典题目
好像目前还没有这方面题目的总结.这几天连续看到四个问这类题目的人,今天在这里简单写一下.这里我们不介绍其它有关矩阵的知识,只介绍矩阵乘法和相关性质. 不要以为数学中的矩阵也是黑色屏幕上不断变化的 ...
- CH Round #30 摆花[矩阵乘法]
摆花 CH Round #30 - 清明欢乐赛 背景及描述 艺术馆门前将摆出许多花,一共有n个位置排成一排,每个位置可以摆花也可以不摆花.有些花如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看 ...
- 【BZOJ-1898】Swamp 沼泽鳄鱼 矩阵乘法
1898: [Zjoi2005]Swamp 沼泽鳄鱼 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1012 Solved: 566[Submit][S ...
- 【poj3070】矩阵乘法求斐波那契数列
[题目描述] 我们知道斐波那契数列0 1 1 2 3 5 8 13…… 数列中的第i位为第i-1位和第i-2位的和(规定第0位为0,第一位为1). 求斐波那契数列中的第n位mod 10000的值. [ ...
- 如何使用矩阵乘法加速动态规划——以[SDOI2009]HH去散步为例
对这个题目的最初理解 开始看到这个题,觉得很水,直接写了一个最简单地动态规划,就是定义 f[i][j]为到了i节点路径长度为j的路径总数, 转移的话使用Floyd算法的思想去转移,借助这个题目也理解了 ...
随机推荐
- Drupal启动阶段之一:配置
配置是Drupal启动过程中的第一个阶段,通过函数_drupal_bootstrap_configuration()实现: function _drupal_bootstrap_configurati ...
- Nginx常用配置整理
1.全局块:配置影响nginx全局的指令.一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等. worker_pro ...
- python-创建一个登录判断的函数
方法一def account_login(): password = input('Password:') if password == '12345': print('Login success!' ...
- MDK5.00中*** error 65: access violation at 0xFFFFFFFC : no 'write' permission的一种解决方法
http://blog.csdn.net/coderfun/article/details/9417289 这是在调试过程中的修改方法,所以在每次运行的时候,都要设置. 先进入调试模式(crtl+F5 ...
- Javascript 中使用Json的四种途径
1.jQuery插件支持的转换方式: 复制代码代码如下: $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 ...
- Lintcode---二叉树的前序、中序、后序遍历
给出一棵二叉树,返回其节点值的后序遍历. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 思路:二叉树的后序遍历,简单 ...
- C语言 文件操作
/** *@author cody *@date 2014-08-09 *@description copy text file * FILE *fopen(filename,openmode) * ...
- setTime
var getTime = function() { var _ = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09'], //补 ...
- 关于Qt半自动内存管理的思考及实验
一时兴起,对Qt感了兴趣,决心想要研究一下. 按网上资料配好环境,Windows 7 64bit + Qt 5.3.1 + VS2010. 根据<C++ GUI Qt4 编程>这本书,写出 ...
- 如何卸载Mysql
mysql安装的时候会出现各种问题,尤其对新手,卸载不干净,重新安装会受到影响.也不能有点问题就重装系统吧!现在提供一种通过删除注册表的方式卸载mysql! 运行regedit打开注册表,搜索mysq ...