Just Pour the Water ZOJ - 2974 (矩阵快速幂)

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.

题意 给出n个杯子与初始水量同时进行操作 将其中的水同时平均分入所指定的杯子 进行x次后 输出杯子剩余水量

刚拿到这个题,第一反应是递推找规律,但是因为每个杯子的初始水量是未知的,所以能找的只是每个杯子水量与其余杯子水量的关系。

但是看到了操作次数巨大,而且最多只有20个杯子,感觉可以用快速幂去做。

我们假设矩阵a[i][j]代表第i个杯子的水有a[i][j]来自第j个杯子,这样用init矩阵表示初始水量,unit矩阵表示每次操作后的变化,最后init*pow(unit,k)即为所求。

注意当某一个杯子不用向其他杯子操作时,要将相应位置填为1

例如所给样例

这里写图片描述

A : ( 1/2 + 1/4 ) * 100 = 75
B: (1/2 + 3/4 ) * 100 = 125


code:

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <vector>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    #define LL long long
    const double eps=1e-14;
    int n;
    struct matrix{
        double f[22][22];//矩阵每列代表每个杯子,每行代表水的来自每个杯子
    }c;
    void mul(matrix &a,matrix b)//矩阵乘法
    {
        int i,j,k;
        memset(c.f,0,sizeof(c.f));
        for(k=0;k<n;k++)
        {
            for(i=0;i<n;i++)
            {
                if(a.f[i][k]<eps)continue;
                for(j=0;j<n;j++)
                {
                    if(b.f[k][j]<eps)continue;
                    c.f[i][j]+=a.f[i][k]*b.f[k][j];
                }
            }
        }
        a=c;
    }
    matrix pow_mod(matrix a,int b)//矩阵快速幂
    {
        matrix s;
        memset(s.f,0,sizeof(s.f));
        for(int i=0;i<n;i++)
            s.f[i][i]=1;
        while(b)
        {
            if(b&1)
                mul(s,a);
            mul(a,a);
            b=b>>1;
        }
        return s;
    }
    int main()
    {
        int m,T;
        cin>>T;
        while(T--)
        {
            cin>>n;
            double a[22],p,s;
            int i,j,k,x,y;
            for(i=0;i<n;i++)
                cin>>a[i];
            matrix e;
            memset(e.f,0,sizeof(e.f));
            for(i=0;i<n;i++)
            {
                cin>>k;
                if(k==0)    // 如果是0,这个杯子里的水不会倒给别的杯子它自己始终是1
                    e.f[i][i]=1;
                for(j=0;j<k;j++)
                {
                    cin>>x;
                    e.f[i][x-1]=1.0/k;//否则记录给他杯子倒水的比例
                }
            }
            cin>>m;
            e=pow_mod(e,m);//求得最终每个杯子来自其他杯子的比例
            double ans[22];
            for(i=0;i<n;i++)
            {
                ans[i]=0;
                for(j=0;j<n;j++)
                ans[i]+=a[j]*e.f[j][i];//用原来每个杯子的水量乘比例求和即最终每个杯子水量
            }
            printf("%.2f",ans[0]);
            for(i=1;i<n;i++)
                printf(" %.2f",ans[i]);
            printf("\n");
        }
        return 0;
    }
    /*
        矩阵:
        f[i][j]表示i倒水到j里的概率
    */


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值