ZOJ 2974 Just Pour the Water(矩阵快速幂)

经典的倒水问题!

转载请注明出处http://write.blog.csdn.net/postedit/21247667

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2974

----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋害羞害羞害羞害羞http://user.qzone.qq.com/593830943/main
 
 
----------------------------------------------------------------------------------------------------------------------------------------------------------



Description

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 forM 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.

题意:倒水记录任意时刻杯中容量,案例输入第四行开始为一号杯,第五行为二号杯

代码如下:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 60
struct Matrix
{
	double  m[Max][Max];
} ;
Matrix A,I;
int t,n,q,num;
Matrix matrixmul(Matrix a,Matrix b)
{
	int i,j,k;
	Matrix c;
	for (i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		{
			c.m[i][j] = 0;
			for(k=1;k<=n;k++)
				c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
		}
		return c;
}
Matrix quickpagow(long  n)
{
	Matrix m=A,b=I;
	while (n>=1)
	{
		if(n&1)
			b=matrixmul(b,m);
		n=n>>1;
		m=matrixmul(m,m);
	}
	return b;
}

int main()
{
	double a;
	Matrix f1,f2;
	int i,j;
	scanf("%d",&t);
	while(t--)
	{
		memset(f1.m,0,sizeof(f1.m));
		memset(I.m,0,sizeof(I.m));
		memset(A.m,0,sizeof(A.m));
		scanf("%d",&n);
		for(i=1;i<=n;i++)
			I.m[i][i]=1;
		for(i=1;i<=n;i++)
		{
			scanf("%lf",&a);
			f1.m[i][1]=a;
		}
		for(i=1;i<=n;i++)
		{
			scanf("%d",&num);
			if(num==0)
				A.m[i][i]=1;
			for(j=1;j<=num;j++)
			{
				scanf("%d",&q);
				A.m[q][i]=1.0*1/num;
			}
		}
		long k;
		scanf("%lld",&k);
		Matrix	ans=quickpagow(k);
		Matrix ans1=matrixmul(ans,f1);
		
		printf("%.2lf",ans1.m[1][1]);
		for(i=2;i<=n;i++)
		{
			printf(" %.2lf",ans1.m[i][1]);
		}
		printf("\n");
	}
	return 0;
}


你可能感兴趣的:(算法,ACM,ZOJ,Matrix)