J - Just Pour the Water ZOJ - 2974

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.

#include<cstdio>
#include<cstring>
using namespace std;

typedef long long ll;
int n, m;

const int maxn=50;
struct Martix{
	double mar[maxn][maxn];
}E,st;

double val[maxn][1],ed[maxn][1]; 

void init(){
	for(int i=0;i<maxn;i++){
		E.mar[i][i]=1.0;
	}	
}

Martix mul(Martix a,Martix b){
	Martix ans;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			ans.mar[i][j]=0.0;
			for(int k=1;k<=n;k++){
				ans.mar[i][j]+=a.mar[i][k]*b.mar[k][j];
			}
		}
	}
	return ans;
}

Martix pow_(int n){
	Martix ans=E;
	while(n){
		if(n&1)
			ans=mul(ans,st);
		st=mul(st,st);
		n/=2;
	}
	return ans;
}
/*
这道题真的是蛮裸的一道快速幂,但是自己有点太自傲了,没有和队友讲,其实
我觉得讲和不讲,没有什么必要,他又不会,但是我们可能在和别人沟通的
过程中,感觉到自己的错误,这样就可以避免这样的悲剧 
主要是忘了倒水的时候,除数如果是0,应该怎么办,想歪了,直接略过了
哎 
*/

double get_ans(int x){
	double ans=0.0;
	for(int i=1;i<=n;i++){
		ans+=st.mar[x][i]*val[i][0];
	}
	return ans;
}



int main(){
	init();
	int T;
	scanf("%d", &T);
	while(T--){
		memset(st.mar,0,sizeof(st.mar));
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%lf",&val[i][0]);
		}
		for(int i=1;i<=n;i++){
			double k;
			scanf("%lf",&k);
			if(k==0){
				st.mar[i][i]=1.0;
				continue;
			}
			double tmp=1.0/k;
			for(int j=1;j<=k;j++){
				int x;
				scanf("%d",&x);
				st.mar[x][i]=tmp;
			}
		}
		scanf("%d",&m);
		st=pow_(m);		
		for(int i=1;i<=n;i++){
			double  ans=get_ans(i);
			if(i==1) printf("%.2f",ans);
			else printf(" %.2f",ans);
		}
		printf("\n");
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值