ZOJ 2974 Just Pour the Water

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.


矩阵快速幂,直接按要求建立矩阵即可,注意k=0的问题

#include<map>
#include<ctime>
#include<cmath>    
#include<queue> 
#include<string>
#include<vector>
#include<cstdio>    
#include<cstring>  
#include<iostream>
#include<algorithm>    
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))    
#define rep(i,j,k) for(int i=j;i<=k;i++)    
#define per(i,j,k) for(int i=j;i>=k;i--)    
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])    
#define inone(x) scanf("%d",&x)    
#define intwo(x,y) scanf("%d%d",&x,&y)    
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)    
typedef long long LL;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 30;
int T, n, m, x;
double a[N][N], b[N][N], c[N];

int main()
{
	for (inone(T); T--;)
	{
		inone(n);
		rep(i, 1, n) scanf("%lf", &c[i]);
		ms(a, 0);
		rep(i, 1, n)
		{
			inone(m);
			rep(j, 1, m) inone(x), a[i][x] = 1.0 / m;
			if (!m) a[i][i] = 1;
		}
		for (inone(m); m; m >>= 1)
		{
			if (m & 1)
			{
				rep(i, 1, n)
				{
					b[1][i] = 0;
					rep(j, 1, n)
					{
						b[1][i] += c[j] * a[j][i];
					}
				}
				rep(i, 1, n) c[i] = b[1][i];
			}
			rep(i, 1, n) rep(j, 1, n)
			{
				b[i][j] = 0;
				rep(k, 1, n) b[i][j] += a[i][k] * a[k][j];
			}
			rep(i, 1, n) rep(j, 1, n) a[i][j] = b[i][j];
		}
		rep(i, 1, n) printf("%.2lf%s", c[i], i == n ? "\n" : " ");
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值