ZOJ2974-Just Pour the Water

本文介绍了一个关于多个容器间水量转移的编程问题。通过使用矩阵快速幂的方法,解决了多个容器间复杂水量分配的问题,并提供了一个完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.


Author: ZHOU, Kai
Source: The 5th Zhejiang Provincial Collegiate Programming Contest


题目大意:有N个容器,编号为i的容器可以把自己的水分成k份放进k个容器里,每次把1—N依次操作,求M次后各个容器里的水量。
解题思路:矩阵快速幂(注意K=0的情况)。


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>

using namespace std;
#define LL long long

double a[30];
struct Matrix
{
    double x[30][30];
    Matrix()
    {
        memset(x,0,sizeof x);
    }
}K;
int n;

Matrix Mul(Matrix p,Matrix q)
{
    Matrix sum;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            for(int k=1; k<=n; k++)
                sum.x[i][j]+=p.x[i][k]*q.x[k][j];
        }
    }
    return sum;
}

Matrix mypow(Matrix p,int m)
{
    Matrix sum=K;
    while(m)
    {
        if(m&1) sum=Mul(sum,p);
        p=Mul(p,p);
        m>>=1;
    }
    return sum;
}

int main()
{
    int t,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%lf",&a[i]),K.x[i][i]=1;
        Matrix p;
        int q,k;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&k);
            if(k==0) {p.x[i][i]=1;continue;}
            for(int j=1; j<=k; j++)
            {
                scanf("%d",&q);
                p.x[i][q]=1.0/k;
            }
        }
        scanf("%d",&m);
        Matrix sum=mypow(p,m);
        for(int i=1; i<=n; i++)
        {
            double ans=0;
            for(int j=1;j<=n;j++)
                ans+=sum.x[j][i]*a[j];
            if(i==1) printf("%.2lf",ans);
            else printf(" %.2lf",ans);
        }
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值