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

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的情况,我被坑了一发)。

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn=;
double f[maxn]; struct Matrix
{
double a[maxn][maxn];
int n;
}; Matrix Matrix_mult(Matrix A,Matrix B)
{
Matrix C;C.n=A.n;
int i,j,k;
for(i=;i<=A.n;i++)
{
for(j=;j<=A.n;j++)
{
double sum=;
for(k=;k<=A.n;k++)
sum+=A.a[i][k]*B.a[k][j];
C.a[i][j]=sum;
}
}
return C;
} Matrix Matrix_pow(Matrix A,int m)
{
Matrix ret;ret.n=A.n;
memset(ret.a,,sizeof(ret.a));
for(int i=;i<=A.n;i++) ret.a[i][i]=1.0;
while(m)
{
if(m&) ret=Matrix_mult(A,ret);
A=Matrix_mult(A,A);m>>=;
}
return ret;
} void Printf(Matrix A)
{
for(int i=;i<=A.n;i++)
{
double ans=;
for(int j=;j<=A.n;j++)
ans+=A.a[i][j]*f[j];
printf(i==?"%.2lf":" %.2lf",ans);
}
printf("\n");
} int main()
{
int t,i,j,k,p,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=;i<=n;i++) scanf("%lf",f+i);
Matrix A;A.n=n;
for(i=;i<=n;i++)
{
scanf("%d",&k);
for(j=;j<=n;j++) A.a[j][i]=;
if(k==)
{
A.a[i][i]=1.0;continue;
}
double c=1.0/k;
for(j=;j<=k;j++)
{
scanf("%d",&p);A.a[p][i]=c;
}
}
scanf("%d",&m);
Matrix B=Matrix_pow(A,m);
Printf(B);
}
return ;
}

zoj 2974 Just Pour the Water矩阵快速幂的更多相关文章

  1. zoj 2974 Just Pour the Water (矩阵快速幂,简单)

    题目 对于案例的解释请见下图: 这道要变动提取一下矩阵,之后就简单了 具体解释可看代码: #include <string.h> #include <stdio.h> #inc ...

  2. ZOJ 2974 Just Pour the Water

    矩阵快速幂. 构造一个矩阵,$a[i][j]$表示一次操作后,$j$会从$i$那里得到水的比例.注意$k=0$的时候,要将$a[i][j]$置为$1$. #pragma comment(linker, ...

  3. ACM-ICPC 2018 焦作赛区网络预赛 L:Poor God Water(矩阵快速幂)

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  4. ZOJ 2794 Just Pour the Water 【矩阵快速幂】

    给你n个杯子,每次有特定的到水规则,倒m次请问最后每个被子里还有多少水 我们很容易发现每次变化的规则相同,那么可以set 一个矩阵存放 然后多次倒水就相当于矩阵相乘,在m 范围达到(1<= M  ...

  5. bnuoj 16493 Just Pour the Water(矩阵快速幂)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=16493 [题解]:矩阵快速幂 [code]: #include <cstdlib> #i ...

  6. ACM-ICPC 2018 焦作赛区网络预赛- L:Poor God Water(BM模板/矩阵快速幂)

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  7. 焦作网络赛L-Poor God Water【矩阵快速幂】

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  8. dutacm.club Water Problem(矩阵快速幂)

    Water Problem Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Tot ...

  9. ACM-ICPC 2018 焦作赛区网络预赛 L Poor God Water(矩阵快速幂,BM)

    https://nanti.jisuanke.com/t/31721 题意 有肉,鱼,巧克力三种食物,有几种禁忌,对于连续的三个食物:1.这三个食物不能都相同:2.若三种食物都有的情况,巧克力不能在中 ...

随机推荐

  1. JavaScript操作DOM

    1.DOM对象简介: 什么是DOM:(Document Object Model) 译为文档对象模型,是 HTML 和 XML 文档的编程接口.   2.DOM HTML 节点树:指的是DOM中为操作 ...

  2. Object-C知识点 (六) 开发中的技巧

    本文主要介绍开发中的一些实用技巧 #pragma mark - 代码控制Home按键 [[UIApplication sharedApplication] performSelector:@selec ...

  3. bash编程之循环控制:

    bash编程之循环控制: for varName in LIST; do 循环体 done   while CONDITION; do 循环体 done   until CONDITION; do 循 ...

  4. 【linux】 服务器文件说明

    文件名 说明 /etc/resolv.conf  域名解析服务器地址文件 /etc/services 服务程序对应端口号文件 /etc/passwd 登录账号文件 /etc/hosts 本地IP域名解 ...

  5. 实验二 JSP基本动态元素的使用

    实验二  JSP基本动态元素的使用 实验性质:验证性          实验学时:  2学时      实验地点: 一 .实验目的与要求 1.掌握JSP中声明变量.定义方法.java程序片及表达式的使 ...

  6. Survey lists 10 most innovative cities

    From China Daily Beijing and Shanghai are among the 10 most innovative cities in the world, based on ...

  7. manjaro linux没有ll等命令的解决办法

    编辑~/.bashrc, 添加alias 如下 vim ~/.bashrc设置别名. 添加如下行 alias ll='ls -alF' alias la='ls -A' alias vi='vim' ...

  8. R-data.table

    data.table可以扩展和增强data.frame的功能,在分组操作和组合时访问速度更快. require(data.table) theDT = data.table(A=1:10, B=let ...

  9. Mysql之查看数据库版本

    Mysql版本: 登入数据库的时候: select @@version; select version(); mysql> select @@version; +-----------+ | @ ...

  10. CSS预处理器(less 和 sass)

    CSS预处理器 1.基于CSS的另一种语言 2.通过工具编译成CSS 3.添加了很多CSS不具备的特性 4.能提升CSS文件的组织 提供功能:1.嵌套 反映层级和约束 2.变量和计算,减少重复戴拿 3 ...