BZOJ1297 迷路 - 矩阵的幂
题目大意:
输入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 迷路 - 矩阵的幂的更多相关文章
- BZOJ1297: [SCOI2009]迷路 矩阵快速幂
Description windy在有向图中迷路了. 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1. 现在给出该有向图,你能告诉windy总共有多少种不同 ...
- BZOJ 1297: [SCOI2009]迷路 [矩阵快速幂]
Description windy在有向图中迷路了. 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1. 现在给出该有向图,你能告诉windy总共有多少种不同 ...
- BZOJ 1297 迷路(矩阵快速幂)
很容易想到记忆化搜索的算法. 令dp[n][T]为到达n点时时间为T的路径条数.则dp[n][T]=sigma(dp[i][T-G[i][n]]); 但是空间复杂度为O(n*T),时间复杂度O(n*n ...
- Luogu P4159 [SCOI2009]迷路 矩阵快速幂+精巧转化
大致就是矩阵快速幂吧.. 这个时候会发现这些边权$\le 9$,然后瞬间想到上回一道题:是不是可以建一堆转移矩阵再建一个$lcm(1,2,3,4,5,6,7,8,9)$的矩阵?...后来发现十分的慢q ...
- [SCOI2009]迷路(矩阵快速幂) 题解
Description windy在有向图中迷路了. 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1. 现在给出该有向图,你能告诉windy总共有多少种不同 ...
- 【BZOJ1297】[SCOI2009]迷路(矩阵快速幂)
[BZOJ1297][SCOI2009]迷路(矩阵快速幂) 题面 BZOJ 洛谷 题解 因为边权最大为\(9\),所以记录往前记录\(9\)个单位时间前的.到达每个点的方案数就好了,那么矩阵大小就是\ ...
- 【矩阵快速幂】bzoj1297 [SCOI2009]迷路
1297: [SCOI2009]迷路 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1407 Solved: 1007[Submit][Status ...
- 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 ...
- bzoj1297 [SCOI2009]迷路——拆点+矩阵快速幂
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1297 一看感觉是矩阵快速幂之类的,但边权不好处理啊: 普通的矩阵快速幂只能处理边权为1的,所 ...
随机推荐
- 用jquery获取单选按钮选中的内容 和 获取select下拉列表选中的值
1.<label><input name='reason' type='radio' value='您的评论内容涉嫌谣言' />您的评论内容涉嫌谣言</label> ...
- Android 监听软键盘点击回车及换行事件
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean ...
- (转)30 IMP-00019: row rejected due to ORACLE error 12899
IMP: row rejected due IMP: ORACLE error encountered ORA: value too large , maximum: )导入日志报 IMP: 由于 O ...
- 安装配置Rancher管理docker
原文:安装配置Rancher管理docker 版权声明:本文为博主原创文章,转载请注明地址http://blog.csdn.net/tianyaleixiaowu. https://blog.csdn ...
- [D3] SVG Graphics Containers and Text Elements in D3 v4
SVG is a great output format for data visualizations because of its scalability, but it comes with s ...
- [React] Style the body element with styled-components and "injectGlobal"
In this lesson, we see how we can apply styles globally with the "injectGlobal" helper met ...
- JavaScript系列--JavaScript数组高阶函数reduce()方法详解及奇淫技巧
一.前言 reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值. reduce() 可以作为一个高阶函数,用于函数的 compose. reduce()方 ...
- [WASM] Compile C Code into WebAssembly
We use the C language instead of pure WAST to create a square root function using WASM Fiddle (https ...
- 部分城市关于.Net招聘数量
2016-12-09更新统计数据 上海 10730 北京 6322 广州 4157 深圳 3548 成都 2291 重庆 706 厦门 285 2015-01-30日,前程无忧搜索".Net ...
- 你说你会C++? —— 智能指针
智能指针的设计初衷是: C++中没有提供自己主动回收内存的机制,每次new对象之后都须要手动delete.稍不注意就memory leak. 智能指针能够解决上面遇到的问题. C++中常见的 ...