ZOJ2974 Just Pour the Water(5th 浙江省赛)矩阵快速幂

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

问题描述:

Just Pour the Water

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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

题目大意:有N个容器,里面装有一定量的水,求经过M分钟的倒水操作,最后N个容器中分别剩下多少水。

问题分析:

不难发现每分钟的操作都是一样的,所以下一分钟的水量是根据前一分钟的水量得到的,那么这就是一个递推的过程,理论上有一个递推公式,但是这是一个比较复杂的过程,由递推我想到了矩阵快速幂。

        |0          1|                 |1/2   1/2|

        |1/2  1/2  |                 |1/4    3/4|

M             1                                    2

A:100*(1/2+1/4) = 75;

B :100*(1/2+3/4) = 125;

代码实现:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
struct node{
	int n;
	double v[25][25];
};

struct node muti(struct node a,struct node b)
{
	struct node c;
	c.n = a.n;
	for(int i = 0; i < a.n; i++)
	{
		for(int j = 0; j < b.n; j++)
		{
			c.v[i][j] = 0;
			for(int k = 0; k < a.n; k++)
			{
				c.v[i][j] += a.v[i][k]*b.v[k][j];
			}
		}
	}
	return c;
}
int main()
{
	int T,n,k,m,i,j,x;
	double ans[25];
	cin>>T;
	while(T--)
	{
		struct node a,b,res;
		cin>>n;
		a.n = n;
		for(i = 0; i < n; i++)
		{
			cin>>a.v[0][i];
		}
		memset(b.v,0,sizeof(b.v));
		memset(res.v,0,sizeof(res.v));
		memset(ans,0,sizeof(ans));
		b.n = n;
		res.n = n;
		for(i = 0; i < n; i++)
		{
			b.v[i][i] = 1;
			res.v[i][i] = 1;
			cin>>k;
			b.v[i][i] -= 1;
			if(k==0)	b.v[i][i] += 1;
			for(j = 0; j < k; j++)
			{
				cin>>x;
				b.v[i][x-1] += 1.0*1/k;
			}
		}
		cin>>m;
		while(m)
		{
			if(m&1)	res = muti(res,b);
			b = muti(b,b);
			m>>=1;
		}
		for(i = 0; i < n; i++)
		{
			for(j = 0; j < n; j++)
			{
				ans[i] += a.v[0][j]*res.v[j][i];
			}
			if(i==0)	printf("%.2f",ans[i]);
			else		printf(" %.2f",ans[i]);
		}
		printf("\n");
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值