您的位置:首页 > 其它

ZOJ 2974 Just Pour the Water

2017-02-17 21:31 399 查看
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

, b

, c
;

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: