http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1973
//1864877 2009-05-11 19:06:51 Accepted 2974 C++ 0 188 aslys //烦了点,却也无心改进了~ 懒惰 #include<iostream> using namespace std; const int N = 22; int n; double matrix[N][N]; void fun(int k,double m[][N]) { int i,j,p,t; double x[N][N],y[N][N],sum; if(k == 1) //矩阵的一次方 { for(i=1;i<=n;i++) for(j=1;j<=n;j++) m[i][j] = matrix[i][j]; } else if(k == 2) //矩阵的二次方 { for(i=1;i<=n;i++) for(j=1;j<=n;j++) { sum = matrix[i][1] * matrix[1][j]; for(p=2;p<=n;p++) sum += matrix[i][p] * matrix[p][j]; m[i][j] = sum; } } else //矩阵的 k 次方 ,先计算出 k/2 次方 ,加快计算速度 { t = k/2; fun(t,x); //递归调用 for(i=1;i<=n;i++) for(j=1;j<=n;j++) { sum = x[i][1] * x[1][j]; for(p=2;p<=n;p++) sum += x[i][p] * x[p][j]; y[i][j] = sum; } if( k % 2 == 1) // 比如对矩阵A :A^9 = A^4 * A^4 * A { for(i=1;i<=n;i++) for(j=1;j<=n;j++) { sum = matrix[i][1] * y[1][j]; for(p=2;p<=n;p++) sum += matrix[i][p] * y[p][j];; m[i][j] = sum; } } else { for(i=1;i<=n;i++) for(j=1;j<=n;j++) m[i][j] = y[i][j]; } } }
int main() { int t; cin>>t; while(t--) { int i,j,k; int m; double x[N][N],water[N],r; scanf("%d",&n); for(i=1;i<=n;i++)//init() for(j=1;j<=n;j++) matrix[i][j] = 0.0;
for(i=1;i<=n;i++) scanf("%lf",&water[i]);
for(i=1;i<=n;i++) { scanf("%d",&k); if( k == 0 ) /**////除数不能为零,切记切记 { matrix[i][i] = 1.0; continue; } r = 1.0/(double)k; while(k--) { scanf("%d",&j); matrix[j][i] = r; //构建初始矩阵 } } scanf("%d",&m);
fun(m,x); //求矩阵的m次方,结果放在 x 数组中
for(i=1;i<=n;i++) { r = 0.0; for(j=1;j<=n;j++) r += x[i][j] * water[j]; if(i == 1) printf("%.2lf",r); else printf(" %.2lf",r); } printf("\n"); } return 0; }
|