Description

windy在有向图中迷路了。 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1。 现在给出该有向图,你能告诉windy总共有多少种不同的路径吗? 注意:windy不能在某个节点逗留,且通过某有向边的时间严格为给定的时间。

Input

第一行包含两个整数,N T。 接下来有 N 行,每行一个长度为 N 的字符串。 第i行第j列为'0'表示从节点i到节点j没有边。 为'1'到'9'表示从节点i到节点j需要耗费的时间。

Output

包含一个整数,可能的路径数,这个数可能很大,只需输出这个数除以2009的余数。

Sample Input

【输入样例一】
2 2
11
00

【输入样例二】
5 30
12045
07105
47805
12024
12345

Sample Output

【输出样例一】
1

【样例解释一】
0->0->1

【输出样例二】
852

HINT

30%的数据,满足 2 <= N <= 5 ; 1 <= T <= 30 。 100%的数据,满足 2 <= N <= 10 ; 1 <= T <= 1000000000 。

Solution

矩阵快速幂

一开始看到以为还是板子...不过发现这题是有边权的。后来在hjw大佬的点醒下发现可以拆点

然后套板子就行了

新姿势++

#include <bits/stdc++.h>

#define ll long long
#define inf 0x3f3f3f3f
#define il inline namespace io { #define in(a) a=read()
#define out(a) write(a)
#define outn(a) out(a),putchar('\n') #define I_int int
inline I_int read() {
I_int x = , f = ; char c = getchar() ;
while( c < '' || c > '' ) { if( c == '-' ) f = - ; c = getchar() ; }
while( c >= '' && c <= '' ) { x = x * + c - '' ; c = getchar() ; }
return x * f ;
}
char F[ ] ;
inline void write( I_int x ) {
I_int tmp = x > ? x : -x ;
if( x < ) putchar( '-' ) ;
int cnt = ;
while( tmp > ) {
F[ cnt ++ ] = tmp % + '' ;
tmp /= ;
}
while( cnt > ) putchar( F[ -- cnt ] ) ;
}
#undef I_int }
using namespace io ; using namespace std ; #define N 500
const int mod = ; int n = read() , T = read() ;
struct matrix {
int m[ N ][ N ] ;
matrix() { memset( m , , sizeof( m ) ) ; }
int *operator[] ( int a ) { return m[ a ] ; }
matrix operator * ( matrix &x ) {
matrix ans ;
memset( ans.m , , sizeof( ans.m ) ) ;
for( int i = ; i <= n ; i ++ ) {
for( int j = ; j <= n ; j ++ ) {
for( int k = ; k <= n ; k ++ ) {
ans[ i ][ j ] = ( ans[ i ][ j ] + m[ i ][ k ] * x[ k ][ j ] % mod ) % mod ;
}
}
}
return ans ;
}
} a ; matrix power( matrix a , int b ) {
matrix ans , base = a ;
memset( ans.m , , sizeof( ans.m ) ) ;
for( int i = ; i <= n ; i ++ )
ans[ i ][ i ] = ;
while( b ) {
if( b & ) ans = ans * base ;
base = base * base ;
b >>= ;
}
return ans ;
} char ch[ ] ; int main() {
for( int i = ; i <= n ; i ++ ) {
scanf( "%s" , ch+ ) ;
for( int j = ; j <= n ; j ++ ) {
if( ch[ j ] > '' ) {
a[ * ( i - ) + (ch[ j ] - '' ) ][ * ( j - ) + ] = ;
}
}
}
for( int i = ; i <= n ; i ++ ) {
for( int j = ; j < ; j ++ ) {
a[ * ( i - ) + j ][ * ( i - ) + j + ] = ;
}
}
n *= ; //puts("233");
matrix ans = power( a , T ) ;
printf( "%d\n" , ans[ ][ n - ] ) ;
}

BZOJ1297: [SCOI2009]迷路 矩阵快速幂的更多相关文章

  1. BZOJ 1297: [SCOI2009]迷路 [矩阵快速幂]

    Description windy在有向图中迷路了. 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1. 现在给出该有向图,你能告诉windy总共有多少种不同 ...

  2. Luogu P4159 [SCOI2009]迷路 矩阵快速幂+精巧转化

    大致就是矩阵快速幂吧.. 这个时候会发现这些边权$\le 9$,然后瞬间想到上回一道题:是不是可以建一堆转移矩阵再建一个$lcm(1,2,3,4,5,6,7,8,9)$的矩阵?...后来发现十分的慢q ...

  3. [SCOI2009]迷路(矩阵快速幂) 题解

    Description windy在有向图中迷路了. 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1. 现在给出该有向图,你能告诉windy总共有多少种不同 ...

  4. BZOJ 1297 迷路(矩阵快速幂)

    很容易想到记忆化搜索的算法. 令dp[n][T]为到达n点时时间为T的路径条数.则dp[n][T]=sigma(dp[i][T-G[i][n]]); 但是空间复杂度为O(n*T),时间复杂度O(n*n ...

  5. BZOJ1297 [SCOI2009]迷路 矩阵乘法

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1297 题意概括 有向图有 N 个节点,从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1. ...

  6. bzoj1297: [SCOI2009]迷路(矩阵乘法+拆点)

    题目大意:有向图里10个点,点与点之间距离不超过9,问从1刚好走过T距离到达n的方案数. 当时看到这题就想到了某道奶牛题(戳我).这两道题的区别就是奶牛题问的是走T条边,这道题是每条边都有一个边权求走 ...

  7. 【BZOJ1297】[SCOI2009]迷路(矩阵快速幂)

    [BZOJ1297][SCOI2009]迷路(矩阵快速幂) 题面 BZOJ 洛谷 题解 因为边权最大为\(9\),所以记录往前记录\(9\)个单位时间前的.到达每个点的方案数就好了,那么矩阵大小就是\ ...

  8. 【矩阵快速幂】bzoj1297 [SCOI2009]迷路

    1297: [SCOI2009]迷路 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1407  Solved: 1007[Submit][Status ...

  9. 2018.10.23 bzoj1297: [SCOI2009]迷路(矩阵快速幂优化dp)

    传送门 矩阵快速幂优化dp简单题. 考虑状态转移方程: f[time][u]=∑f[time−1][v]f[time][u]=\sum f[time-1][v]f[time][u]=∑f[time−1 ...

随机推荐

  1. Intel 设计缺陷背后的原因是什么? | Linux 中国

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/F8qG7f9YD02Pe/article/details/79386769 wx_fmt=jpeg& ...

  2. leadJS初构建

    目录: 1. 面向对象篇 2. 数据结构篇 3. 全局函数篇 4. APICloud篇 1. 面向对象篇 JS原本无法进行程序员世界的面向对象编程,故此对JS封装成一种具有面向对象编程能力的JS. / ...

  3. [javascript]编码&i字符串格式化&nput历史记录&清空模态框

    js中编码问题 https://www.haorooms.com/post/js_escape_encodeURIComponent 我在前端js添加时候创建dom时候,有汉字,发现是乱码就研究了下 ...

  4. [py]requests+json模块处理api数据,flask前台展示

    需要处理接口json数据,过滤字段,处理字段等. 一大波json数据来了 参考: https://stedolan.github.io/jq/tutorial/ https://api.github. ...

  5. Linux文本编辑器快捷方式

  6. redis error It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. SocketFailure on PING

    应用redis出现如下错误 It was not possible to connect to the redis server(s); to create a disconnected multip ...

  7. mysql在windows下命令行启动与关闭服务

    一.命令行关闭与启动服务的命令: 二.运行cmd的命令行程序时,必须以管理员身份运行.否则会出现如下提示: 三.mysql5.7版本的服务名一般默认为mysql57,因此如果使用服务名mysql,那么 ...

  8. 概率检索模型:BIM+BM25+BM25F

    1. 概率排序原理 以往的向量空间模型是将query和文档使用向量表示然后计算其内容相似性来进行相关性估计的,而概率检索模型是一种直接对用户需求进行相关性的建模方法,一个query进来,将所有的文档分 ...

  9. Geometry

    uva1473 这题说的是 在空间中给了n个点 然后用体积最小的圆锥将这些点包含在内可以在表面上, 将这些点 映射到xoz平面上然后,然后枚举每个上凸包的边和每个点的极值进行判断求得最小的体积 我们会 ...

  10. OS X 下动态库的引用

    foo.c #include <stdio.h> void foo(void) { printf("foo.\n"); } main.c #include <st ...