传送门

题目大意:

输入n(点的数量),t(时间),和一个n*n的矩阵,第i行第j列表示第i个节点到第j个节点有一条matrix[i]j时间的边,若为0则没有边,问从1到n恰好经过t时间的方案数有多少种?

题目分析:

矩阵的幂与路径的联系:若i到j有一条边权为1的边,那么matrix[i][j]=1,\(matrix^k\)中的[1][n]即代表1到n距离恰好k的方案数(同样也可以表示:i到j有一条边,1到n的经过边数恰好为k的方案数。)

这道题目简化后是:知道i到j有边权为1的边,求x到y的距离为k的方案总数,正是上面提到的。

加上边权后不能套用上边直接求解,因为上面的做法只针对权值为1。但由于一条边的权值只从0~9,那么可以将一个点拆成9个点,若i到j有一条边权为k的边,就相当于从i拆出的第k个节点向j拆出的第1个点连边,i拆出的点之间连边权为1的边,这样\(matrix^t\)的[getkth(i, 1)][getkth(n, 1)]即表示方案数。(getkth表示i拆出的第j个点)

code

#include<bits/stdc++.h>
using namespace std; namespace IO{
inline int read(){
int i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
return i * f;
}
inline void wr(int x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
}using namespace IO; const int N = 15, Mod = 2009;
int n, T;
struct node{
int b[N*10][N*10]; //每个点拆成9个
node(){}
inline void init(){
memset(b, 0, sizeof b);
}
inline void set(int p, int q, int z){b[p][q] = z;}
inline void I(){
//初始化为单位矩阵
memset(b, 0, sizeof b);
for(int i = 1; i <= n*9; i++)
set(i, i, 1);
}
inline node operator * (const node &p) const{
node ret;
for(int i = 1; i <= n*9; i++)
for(int j = 1; j <= n*9; j++){
int sum = 0;
for(int k = 1; k <= n*9; k++) sum = (sum + b[i][k] * p.b[k][j]) % Mod;
ret.set(i, j, sum);
}
return ret;
}
inline node operator ^ (int tt){
node ret; ret.I();
node tmp = *this;
for(; tt; tt >>= 1, tmp = tmp * tmp) if(tt & 1) ret = ret * tmp;
return ret;
}
}matrix, ret; inline int getkth(int x, int k){
return (x-1) * 9 + k;
} int main(){
n = read(), T = read();
matrix.init(), ret.init();
for(int i = 1; i <= n; i++)
for(int j = 1; j <= 8; j++){
matrix.set(getkth(i, j), getkth(i, j + 1), 1);
}
for(int i = 1; i <= n; i++){
char c[N]; scanf("%s", c + 1);
for(int j = 1; j <= n; j++){
int x = c[j] - '0';
if(x) matrix.set(getkth(i, x), getkth(j, 1), 1);
}
}
ret = matrix ^ T;
wr(ret.b[getkth(1, 1)][getkth(n, 1)]);
return 0;
}

BZOJ1297 迷路 - 矩阵的幂的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. 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 ...

  9. bzoj1297 [SCOI2009]迷路——拆点+矩阵快速幂

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1297 一看感觉是矩阵快速幂之类的,但边权不好处理啊: 普通的矩阵快速幂只能处理边权为1的,所 ...

随机推荐

  1. Flask项目之手机端租房网站的实战开发(四)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...

  2. Dotfuscator use for .net4.0 error solve

    在混淆的时候报错了,错误描述大致如下: Could not find a compatible version of ildasm to run on assembly C:\xxx.dll This ...

  3. 【CS Round #43 D】Bad Triplet

    [链接]点击打开链接 [题意] 给你n个点m条边的无权无向联通图; 让你找3个点A,B,C 使得A->B=B->C=A->C 这里X->Y表示点X到点Y的最短路长度. [题解] ...

  4. [RxJS] How To get the results of two HTTP requests made in sequence

    switchMap can chain two HTTP requests together, creating one request based on the results of the fir ...

  5. 9 abstract 和 Virtual 之间的差别

    (1) abstract方法没有详细的实现.同一时候必须被覆写 (2) 虚(Virtual)方法能够没有详细的实现,也不一定必须覆写(虚方法定义时,能够没有详细的实现代码,可是必须创建方法体:即必须有 ...

  6. Codeforces #144 (Div. 1) B. Table (组合数学+dp)

    题目链接: B.Table 题意: \(n*m\)的矩阵使每个\(n*n\)矩阵里面准确包含\(k\)个点,问你有多少种放法. \((1 ≤ n ≤ 100; n ≤ m ≤ 10^{18}; 0 ≤ ...

  7. RAC RMAN 备份 RMAN-03009 ORA-19504 ORA-27040 RMAN-06012 channel c3 not allocated 错误分析

    备份Shell 脚本如下: ######################################################################## ## RAC_hot_da ...

  8. bootstrap课程4 bootstrap的css样式有哪些内容需要注意

    bootstrap课程4 bootstrap的css样式有哪些内容需要注意 一.总结 一句话总结: 1.如何选择产品(框架)的版本? 大版本下的最后一个版本,但是同时又要选择稳定的版本,也就是如果做产 ...

  9. POJ 1270 Following Orders 拓扑排序

    http://poj.org/problem?id=1270 题目大意: 给你一串序列,然后再给你他们部分的大小,要求你输出他们从小到大的所有排列. 如a b f g 然后 a<b ,b< ...

  10. 博客已迁移至http://blog.csdn.net/lujinhong2/

    http://blog.csdn.net/lujinhong2/ 请继续关注