原以为是用搜索做的题,想了好久都无法想到一个高效正确的解法。

后面发现竟然这就是矩阵的应用! 碉堡!

给定一个有向图,问从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即可。

K - Mistwald

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 (MN). Estelle Bright and her friends are initially at Scene (1, 1), the entering scene. They should leave Mistwald from Scene (MN), 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 ≤ MN ≤ 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 (iN). 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(经典矩阵乘法)的更多相关文章

  1. poj3233之经典矩阵乘法

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 12346   Accepted:  ...

  2. hdu1588之经典矩阵乘法

    Gauss Fibonacci Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. 【矩阵乘法经典应用】【ZOJ3497】【Mistwa】

    题意:给定一个有向图(最多25个节点,每个节点的出度最多为4),给定起点和终点,然后从起点开始走,走到终点就停止,否则一直往下走,问能不能P步到达终点.也就是说从起点出发,走一条长度为P的路径,路径中 ...

  4. 学习心得:《十个利用矩阵乘法解决的经典题目》from Matrix67

    本文来自:http://www.matrix67.com/blog/archives/tag/poj大牛的博文学习学习 节选如下部分:矩阵乘法的两个重要性质:一,矩阵乘法不满足交换律:二,矩阵乘法满足 ...

  5. 【转】Matrix67:十个利用矩阵乘法解决的经典题目

    好像目前还没有这方面题目的总结.这几天连续看到四个问这类题目的人,今天在这里简单写一下.这里我们不介绍其它有关矩阵的知识,只介绍矩阵乘法和相关性质.    不要以为数学中的矩阵也是黑色屏幕上不断变化的 ...

  6. CH Round #30 摆花[矩阵乘法]

    摆花 CH Round #30 - 清明欢乐赛 背景及描述 艺术馆门前将摆出许多花,一共有n个位置排成一排,每个位置可以摆花也可以不摆花.有些花如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看 ...

  7. 【BZOJ-1898】Swamp 沼泽鳄鱼 矩阵乘法

    1898: [Zjoi2005]Swamp 沼泽鳄鱼 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1012  Solved: 566[Submit][S ...

  8. 【poj3070】矩阵乘法求斐波那契数列

    [题目描述] 我们知道斐波那契数列0 1 1 2 3 5 8 13…… 数列中的第i位为第i-1位和第i-2位的和(规定第0位为0,第一位为1). 求斐波那契数列中的第n位mod 10000的值. [ ...

  9. 如何使用矩阵乘法加速动态规划——以[SDOI2009]HH去散步为例

    对这个题目的最初理解 开始看到这个题,觉得很水,直接写了一个最简单地动态规划,就是定义 f[i][j]为到了i节点路径长度为j的路径总数, 转移的话使用Floyd算法的思想去转移,借助这个题目也理解了 ...

随机推荐

  1. 在DATASET中要是想添加进另一个表怎么办?

    问:sql="select * from banzhu_manage ";adapter=new SqlDataAdapter(sql,banzhu_conn);adapter.F ...

  2. linux c++ 文件获取md5

    当前在linux系统下,shell命令可以获取md5值,如下: 如果进行c++编程,在代码里执行shell命令可以获得,但是很不雅观,特别是了解了system或者popen函数的机制之后.现在介绍使用 ...

  3. 【转】GGTalk即时通讯系统(支持广域网)终于有移动端了!(技术原理、实现、源码)

    原文地址:http://www.cnblogs.com/justnow/p/4836636.html 首先要感谢大家一直以来对于GGTalk即时通讯系统的关注和支持!GGTalk即时通讯系统的不断完善 ...

  4. jQuery Accordion 插件用于创建折叠菜单

    jQuery Accordion 插件用于创建折叠菜单.它通常与嵌套的列表.定义列表或嵌套的 div 一起使用.选项用于指定结构.激活的元素和定制的动画. 后期完善

  5. Jekins部署.net站点

    前提 1.你需要一台windows服务 可以装vs的且有重启电脑权限的(具体vs版本根据你的团队决定) 2.下载jekins 安装包 地址:https://jenkins.io/download/   ...

  6. Handler源代码解析-有关Handler那些事

    Handler被成为异步处理大师.相信大家都会用,面试中也常常会问到Handler的底层原理.今天就来看一看Handler的机制. Android的消息处理有四个核心类:Handler.Looper. ...

  7. Shell编程二

    告警系统需求分析 1.(虽然之前我们学习了zabbix,但有时候也不能满足我们的需求,比如比较冷门的监控项目需要写自定义脚本,或者服务器网络有问题,没有办法将客户端的数据发送到服务端.) 程序架构: ...

  8. ConcurrentBag同线程元素的添加和删除

    https://www.mgenware.com/blog/?p=232 ConcurrentBag<T>对于同一个线程值的添加和删除是非常快的,因为ConcurrentBag内部将数据按 ...

  9. Oracle之批量生成数据

    一.引言 由于测试程序,需要大量的数据 二.方法 1.pl/sql的Generate Data,在tool菜单中可以找到,但是我这里不能用,老是出现错误,应该是软件的原因,但是没找到解决办法,如下图: ...

  10. FileStream常用的属性和方法 (转)

    对流进行操作时要引用 using System.IO; 命名空间 FileStream常用的属性和方法: 属性: CanRead 判断当前流是否支持读取,返回bool值,True表示可以读取 CanW ...