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.
题目链接:ZOJ-2974
题目大意:按固定的规则互相倒水,m次之后问每个人最后剩余水量。
题目思路:后一次的水量是根据前一次推出,类似于递推式。于是我们可以想到用矩阵乘法来实现。
根据分配的水量所占比例写出矩阵 , 然后套用矩阵乘法模板即可。:
A : ( 1/2 + 1/4 ) * 100 = 75
B: (1/2 + 3/4 ) * 100 = 125
以下是代码:
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
typedef vector<double> vec;
typedef vector<vec> mat;
typedef long long ll;
//const int M 10000;
//计算A * B
mat mul(mat &A, mat &B)
{
mat C(A.size(), vec(B[0].size()));
for (int i = 0; i < A.size(); i++)
{
for (int k = 0; k < B.size(); k++)
{
for (int j = 0; j < B[0].size(); j++)
{
C[i][j] = (C[i][j] + A[i][k] * B[k][j]);// % M;
}
}
}
return C;
}
//计算A^n
mat pow(mat A, ll n)
{
mat B(A.size(), vec(A.size()));
for (int i = 0; i < A.size(); i++) B[i][i] = 1;
while(n > 0)
{
if (n & 1) B = mul(B, A);
A = mul(A, A);
n >>= 1;
}
return B;
}
double water[1000];
int main(){
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
mat A(n, vec(n));
for (int i = 0; i < n; i++)
{
cin >> water[i];
}
for (int i = 0; i < n; i++)
{
int m;
cin >> m;
if (m == 0) A[i][i] = 1;
else
{
for (int j = 0; j < m; j++)
{
int ret;
cin >> ret;
A[i][ret - 1] = 1.0 / m; //根据所占比例写出矩阵规则
}
}
}
int time;
cin >> time;
A = pow(A,time);
for (int i = 0; i < n; i++)
{
double per = 0;
for (int j = 0; j < n; j++)
{
per += A[j][i] * water[j];
}
if (i == 0) printf("%.2f",per);
else printf(" %.2f",per);
}
cout << endl;
}
return 0;
}
附上矩阵乘法模板:
typedef vector<int> vec;
typedef vector<vec> mat;
typedef long long ll;
const int M 10000;
//计算A * B
mat mul(mat &A, mat &B)
{
mat C(A.size(), vec(B[0].size()));
for (int i = 0; i < A.size(); i++)
{
for (int k = 0; k < B.size(); k++)
{
for (int j = 0; j < B[0].size(); j++)
{
C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % M;
}
}
}
return C;
}
//计算A^n
mat pow(mat A, ll n)
{
mat B(A.size(), vec(A.size()));
for (int i = 0; i < A.size(); i++) B[i][i] = 1;
while(n > 0)
{
if (n & 1) B = mul(B, A);
A = mul(A, A);
n >>= 1;
}
return B;
}
//输入,放入自己的矩阵规则
ll n;
void solve()
{
mat A(2, vec(2));
A[0][0] = 1; A[0][1] = 1;
A[1][0] = 1; A[1][1] = 0;
A = pow(A, n);
printf("%d\n",A[1][0]);
}