`
Simone_chou
  • 浏览: 184823 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Just Pour the Water(矩阵快速幂)

    博客分类:
  • ZOJ
 
阅读更多
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

<!-- Problem text file for ZheJiang University Online Judge Created by LiuYaoting -->

Note: the capacity of the container is not limited and all the pouring at every minute is processed at the same time.

 

       题意:

       给出 T 组样例,再给出 N 个水杯,后给出 N 个水杯的容量,后给出 N 行,首先第一行是 K,代表要讲这个杯里面的水要分给一下 K 个水杯,后给出 K 个水杯的编号。输出 M 时间后各个水杯剩下的水容量。输出两位小数。

 

       思路:

       矩阵快速幂。构成一个 N X N 的矩阵,对这个矩阵就 M 次方最后乘以 N X 1 的矩阵,输出这个 N X 1 的矩阵即为答案。至于这个矩阵是啥就不说明了。比赛的时候漏到了 k == 0 的情况了, K == 0 说明自己倒回自己那里,即不变,所以 fn = fn-1。所以赋值的时候应该 A [ i ] [ i ] = 1。

 

       AC:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

typedef vector<double> vec;
typedef vector<vec> mat;

mat mul (mat a, mat b) {
    mat c(a.size(), vec(b[0].size()));

    for (int i = 0; i < a.size(); ++i) {
        for (int j = 0; j < b[0].size(); ++j) {
            for (int k = 0; k < b.size(); ++k) {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }

    return c;
}

mat pow (mat a, int n) {
    mat b(a.size(), vec(a[0].size()));
    for (int i = 0; i < a.size(); ++i)
        b[i][i] = 1.0;

    while (n > 0) {
        if (n & 1) b = mul(b, a);
        a = mul(a, a);
        n >>= 1;
    }

    return b;
}

int main() {

    int t;
    scanf("%d", &t);
    while (t--) {
        int n;
        scanf("%d", &n);

        mat a(n, vec(n));
        mat b(n, vec(1));

        for (int i = 0; i < n; ++i) {
            scanf("%lf", &b[i][0]);
        }

        for (int i = 0; i < n; ++i) {
            int ans;
            double num;
            scanf("%d", &ans);
            num = 1.0 / (double)ans;
            if (!ans) a[i][i] = 1;
            else {
                while(ans--) {
                    int k;
                    scanf("%d", &k);
                    --k;
                    a[k][i] = num;
                }
            }
        }

        int m;
        scanf("%d", &m);

        a = pow(a, m);

        b = mul(a, b);
        for (int i = 0; i < n; ++i) {
            printf("%.2lf", b[i][0]);
            i == n - 1 ? printf("\n") : printf(" ");
        }
    }

    return 0;
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics