HDU 2157 How many ways??
http://acm.hdu.edu.cn/showproblem.php?pid=2157
题意 : 中文的、就不多说了~~~~
废话 :嗯,第一次使用矩阵这一看似很高端的东东~~,表示大一上学期的时候实在是水水地听线性代数的,现在才发现矩阵真的蛮重要的(默默的鄙视下线代老师——从来都没和我们说过矩阵的用处)。
这道题目使用矩阵,我是开三维数组的,保存每个状态,状态P(n) = P(n-1) * p(0)。然后Q次就直接虐好了,最后只要注意下 K == 0 的情况即可。
//Danceonly #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef long long LL; const double INF = 100000007; const double eps = 1e-9; const int maxn = 105; const int mod = 1000; #define MIN(a,b) (a > b ? b : a) #define MAX(a,b) (a > b ? a : b) int maze[30][maxn][maxn]; int N,M; void Fu(int a,int b) { for (int i=0;i<N;i++) for (int j=0;j<N;j++) maze[a][i][j] = maze[b][i][j]; } void Solve(int n) { for (int i=0;i<N;i++) for (int j=0;j<N;j++) { int sum = 0; for (int k=0;k<N;k++) sum += (maze[0][i][k] * maze[n][k][j]) % mod; maze[n+1][i][j] = sum % mod; } } int main() { while (scanf("%d%d",&N,&M) && (N || M)) { memset(maze,0,sizeof(maze)); for (int i=1;i<=M;i++) { int a,b; scanf("%d%d",&a,&b); for (int j=0;j<2;j++) maze[j][a][b] = 1; } for (int i=1;i<=20;i++) { Solve(i); } int Q; scanf("%d",&Q); while (Q--) { int A,B,K; scanf("%d%d%d",&A,&B,&K); if (K == 0) { printf("%d\n",A == B); continue ; } printf("%d\n",maze[K][A][B]); } } return 0; }