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

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

给定一个有向图,问从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. redis可视化工具的安装和调试

    Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  2. :not() 选择器选取除了指定元素以外的所有元素

    :not() 选择器选取除了指定元素以外的所有元素; 最近有人那上图问:如果触碰li时背景色和字体都变化,但是已经被选中的(现在是第一行)不变怎么办?:not是一个非常简单方便的办法:加入我给已经被选 ...

  3. 哈希key个数

    $length = keys %hashname; 则$length中得到的直接是该hash的key的个数.

  4. sqlite 小刀 初试

    SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌入式的,而且目前已经在很多嵌入式产 ...

  5. 设置mvc路由映射

    routes.MapRoute( "chapter", "{action}/{bookId}/{pageindex}", new { controller = ...

  6. Entity Framework Code First使用者的福音 --- EF Power Tool使用记之一(转载)

    好像蛮长时间没有新文章带给大家了.前几天出差再加上忙着公司里的活儿,几乎都没时间上博客园了.今天正好有些时间,为大家简单介绍EF产品组新发布的一个牛逼的小工具——EF Power Tool(翻译的话, ...

  7. 集成讯飞听写iOS sdk到unity遇到的问题:weak成员和strong成员

    在unity里集成讯飞语音听写iOS sdk的过程中,遇到一个问题,官方的demo中可以将多次onResults回调返回的结果累积拼接起来组成一个完整的结果,而我集成过来以后就不能累积了,只拿到最后一 ...

  8. memcache操作实例

    实例一: <?php //使用memcache类来操作 $mm = new Memcache(); $mm->addServer("192.168.70.114",11 ...

  9. atitit.验证码识别step4--------图形二值化 灰度化

    atitit.验证码识别step4--------图形二值化 灰度化 1. 常见二值化的方法原理总结 1 1.1. 方法一:该方法非常简单,对RGB彩色图像灰度化以后,扫描图像的每个像素值,值小于12 ...

  10. Codeforces Round #238 (Div. 2) D. Toy Sum

    D. Toy Sum   time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...