Shirly is a very clever girl. Now she has two containers (A and B), each with some water. Every minute, she pours half of the water in A into B, and simultaneous pours half of the water in B into A. As the pouring continues, she finds it is very easy to calculate the amount of water in A and B at any time. It is really an easy job :).
But now Shirly wants to know how to calculate the amount of water in each container if there are more than two containers. Then the problem becomes challenging.
Now Shirly has N (2 <= N <= 20) containers (numbered from 1 to N). Every minute, each container is supposed to pour water into another K containers (K may vary for different containers). Then the water will be evenly divided into K portions and accordingly poured into anther K containers. Now the question is: how much water exists in each container at some specified time?
For example, container 1 is specified to pour its water into container 1, 2, 3. Then in every minute, container 1 will pour its 1/3 of its water into container 1, 2, 3 separately (actually, 1/3 is poured back to itself, this is allowed by the rule of the game).
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 10) which is the number of test cases. And it will be followed by T consecutive test cases.
Each test case starts with a line containing an integer N, the number of containers. The second line contains N floating numbers, denoting the initial water in each container. The following N lines describe the relations that one container(from 1 to N) will pour water into the others. Each line starts with an integer K (0 <= K <= N) followed by K integers. Each integer ([1, N]) represents a container that should pour water into by the current container. The last line is an integer M (1<= M <= 1,000,000,000) denoting the pouring will continue for M minutes.
Output
For each test case, output contains N floating numbers to two decimal places, the amount of water remaining in each container after the pouring in one line separated by one space. There is no space at the end of the line.
Sample Input
1 2 100.00 100.00 1 2 2 1 2 2
Sample Output
75.00 125.00
Note: the capacity of the container is not limited and all the pouring at every minute is processed at the same time.
题目大意:有N个容器,编号为i的容器可以把自己的水分成k份放进k个容器里,每次把1—N依次操作,求M次后各个容器里的水量。
解题思路:矩阵快速幂(注意K=0的情况,我被坑了一发)。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn=25; 7 double f[maxn]; 8 9 struct Matrix 10 { 11 double a[maxn][maxn]; 12 int n; 13 }; 14 15 Matrix Matrix_mult(Matrix A,Matrix B) 16 { 17 Matrix C;C.n=A.n; 18 int i,j,k; 19 for(i=1;i<=A.n;i++) 20 { 21 for(j=1;j<=A.n;j++) 22 { 23 double sum=0; 24 for(k=1;k<=A.n;k++) 25 sum+=A.a[i][k]*B.a[k][j]; 26 C.a[i][j]=sum; 27 } 28 } 29 return C; 30 } 31 32 Matrix Matrix_pow(Matrix A,int m) 33 { 34 Matrix ret;ret.n=A.n; 35 memset(ret.a,0,sizeof(ret.a)); 36 for(int i=1;i<=A.n;i++) ret.a[i][i]=1.0; 37 while(m) 38 { 39 if(m&1) ret=Matrix_mult(A,ret); 40 A=Matrix_mult(A,A);m>>=1; 41 } 42 return ret; 43 } 44 45 void Printf(Matrix A) 46 { 47 for(int i=1;i<=A.n;i++) 48 { 49 double ans=0; 50 for(int j=1;j<=A.n;j++) 51 ans+=A.a[i][j]*f[j]; 52 printf(i==1?"%.2lf":" %.2lf",ans); 53 } 54 printf(" "); 55 } 56 57 int main() 58 { 59 int t,i,j,k,p,n,m; 60 scanf("%d",&t); 61 while(t--) 62 { 63 scanf("%d",&n); 64 for(i=1;i<=n;i++) scanf("%lf",f+i); 65 Matrix A;A.n=n; 66 for(i=1;i<=n;i++) 67 { 68 scanf("%d",&k); 69 for(j=1;j<=n;j++) A.a[j][i]=0; 70 if(k==0) 71 { 72 A.a[i][i]=1.0;continue; 73 } 74 double c=1.0/k; 75 for(j=1;j<=k;j++) 76 { 77 scanf("%d",&p);A.a[p][i]=c; 78 } 79 } 80 scanf("%d",&m); 81 Matrix B=Matrix_pow(A,m); 82 Printf(B); 83 } 84 return 0; 85 }