FZU 1627 Revival's road【DP(矩阵快速幂)】
2017-12-01 22:01
330 查看
Oaiei is idle, and recently he wants to travel around the country. In his country there are N cities, they are numbered from 1 to N. There are roads between some cities, but some are not directly connected with each other. Oaiei lives in the city 1, and he wants to go to city N. Now, he has a question. In k steps, he would like to know there are how many ways from city 1 to city N. You should note that althought there is a road between city A and city B, there is not necessarily a road between city B and city A.
Input
There are multiple tests. For each test, the first line contains three integers N、M and k(2<=N<=100,1<=M<=N*N,1<=k<=10^9), N denotes the number of cities, M denotes the number of roads between the N cities. Following M lines, each line contains two integers A and B, denoting there is a road between city A and city B.
Output
There are how many ways from city 1 to city N. Becase the answer can be very large, you should output the answer MOD 10000.
Sample Input
4 5 9
1 2
2 3
3 4
4 1
1 3
4 5 1
1 2
2 3
3 4
4 1
1 3
Sample Output
3
0
题意:给你M条有向边,总共需要走K步,求1走到N的方案数。
这是一道图论的动态规划求方案数的问题,可以用矩阵的计算进行优化,对于矩阵再用快速幂。
分析:每一次矩阵乘法 相当于对每个点累积了一下从别的点走过来的和
感觉这种思路十分巧妙。
Input
There are multiple tests. For each test, the first line contains three integers N、M and k(2<=N<=100,1<=M<=N*N,1<=k<=10^9), N denotes the number of cities, M denotes the number of roads between the N cities. Following M lines, each line contains two integers A and B, denoting there is a road between city A and city B.
Output
There are how many ways from city 1 to city N. Becase the answer can be very large, you should output the answer MOD 10000.
Sample Input
4 5 9
1 2
2 3
3 4
4 1
1 3
4 5 1
1 2
2 3
3 4
4 1
1 3
Sample Output
3
0
题意:给你M条有向边,总共需要走K步,求1走到N的方案数。
这是一道图论的动态规划求方案数的问题,可以用矩阵的计算进行优化,对于矩阵再用快速幂。
分析:每一次矩阵乘法 相当于对每个点累积了一下从别的点走过来的和
感觉这种思路十分巧妙。
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; const int maxn = 1e2 + 5; int N, M, K; int a[105][105]; int tmp[maxn][maxn]; int res[maxn][maxn]; const int mod = 10000; void multi(int a[][maxn], int b[][maxn], int n) { memset(tmp, 0, sizeof tmp); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { (tmp[i][j] += a[i][k] * b[k][j])%=mod; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { (a[i][j] = tmp[i][j])%=mod; } } } void pow_multi(int a[][maxn], int n) { memset(res, 0, sizeof res); for (int i = 1; i <= N; i++)//dwjz { res[i][i] = 1; } while (n) { if (n & 1) { multi(res, a, N); } multi(a, a, N); n >>= 1; } } int main() { while (~scanf("%d%d%d", &N, &M, &K)) { memset(a, 0, sizeof a); for (int i = 0; i < M; i++) { int u, v; scanf("%d%d", &u, &v); a[u][v] = 1; //a[v][u] = 1; } pow_multi(a, K); printf("%d\n", res[1] ); //printf("%d\n", res [1]); } return 0; }
相关文章推荐
- FZU - 1627 - Revival's road (矩阵快速幂)
- FZU 1627 - Revival's road(矩阵快速幂)
- FZU 1627 Revival's road
- Clarke and digits(矩阵快速幂+dp)
- [矩阵快速幂 优化DP] 51Nod 1311 转换机
- [DP 矩阵快速幂] BZOJ 1875 [SDOI2009]HH去散步
- BZOJ.1009.[HNOI2008]GT考试(KMP DP 矩阵快速幂)
- [KMP DP 矩阵快速幂加速] BZOJ 1009 [HNOI2008]GT考试
- Codeforces-821E Okabe and El Psy Kongroo(矩阵快速幂+dp)
- [矩阵快速幂加速DP] Codeforces 717D Bubble Cup 9 - Finals D. Dexterina’s Lab
- 2016.8.26 动态规划及杂题选讲 [树形dp] [数论] [矩阵快速幂]
- POJ 3744 Scout YYF I 矩阵快速幂+概率dp
- POJ3744——概率DP 矩阵快速幂优化——Scout YYF I
- Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】
- Codeforces 514E. Darth Vader and Tree DP+矩阵快速幂
- [矩阵快速幂 优化DP] 51Nod 1311 转换机
- poj3744之矩阵快速幂+概率DP
- (矩阵快速幂)fzu1911 Construct a Matrix
- BZOJ1009 单模板自动机 矩阵快速幂优化DP
- POJ-3744 Scout YYF I [概率DP][矩阵快速幂]