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算法的思想去转移,借助这个题目也理解了 ...
随机推荐
- springmvc管理资源开放
关于web.xml的url映射的小知识:<url-pattern>/</url-pattern> 会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀 ...
- 物联网通信协议——比较-MQTT、 DDS、 AMQP、XMPP、 JMS、 REST、 CoAP
物联网通信协议——比较-MQTT. DDS. AMQP.XMPP. JMS. REST. CoAP AMQP & MQTT & DDS (https://www.youtube.c ...
- Hyper-V 虚拟机无法上网的解决方法
创建一个虚拟机网络交换机 2 创建一个 外部网络如下图所示: 3 添加一个旧版的网络适配器: 4 设置旧版网络适配器: END XP系统的情况 1 启动虚拟机,设置虚拟机IP: 2 输入http:// ...
- NGUI ScrollView中MoveRelative,Scroll的区别
Scroll会计算边界,和直接拖拽的效果类似 MoveRelative不计算边界,超出边界了也不会管,也不会应用缓动效果
- [svc][op]从历险压缩日志里网站pv uv统计
http://myhoop.blog.51cto.com/5556534/1367523 tomcat日志格式: http://www.cnblogs.com/anic/archive/2012/12 ...
- matplotlib 添加注释的方式
matplotlib 添加注释的方式 matplotlib.pyplot.annotate Annotate the point xy with text s s : str The text of ...
- 有了 tldr,妈妈再也不用担心我记不住命令了
引言 有一次我在培训时说「程序员要善于使用 Terminal 以提高开发效率」,一位程序员反驳道:「这是 21 世纪,我们为什么要用落后的命令行,而不是先进的 GUI?」 是的,在一些人眼里,这个黑黑 ...
- spring 集成 redis -- pub/sub
redis除了常用的当做缓存外,还可以当做简单的消息中间件,实现消息发布订阅 spring集成redis,可以使用spring-data-redis 首先引入相关maven依赖(此处我spring相关 ...
- hdu3667 Transportation 费用与流量平方成正比的最小流 拆边法+最小费用最大流
/** 题目:hdu3667 Transportation 拆边法+最小费用最大流 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 题意:n个城市由 ...
- tf命令总结
$/AutoBuild/WpfApp/WpfApp/MainWindow.xaml 发现tf workspaces /collection:http://192.168.175.117:8080/tf ...