码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-13 10:17:50      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:矩阵快速幂   zoj   

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1973


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 byT 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


题意:

第一行:案例数;

第二行:水桶个数;

第三行:每个水桶初始水的体积;

第四行:第一个是数字k:表示第i个水桶需要倒入k个水桶(后面的k个数字)1/k的水量!

一共n行!

最后一个数字表次倒的次数!

PS:

matrix[i][j]表示第i个水桶要倒matrox[i][j](比例)的水到第j个水桶!

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
const double eps = 1e-14;
struct Matrix
{
    double  m[27][27];
} I,A,B,T;

LL a,b,n, mod;
int ssize;

Matrix Mul(Matrix a,Matrix b)
{
    int i, j, k;
    Matrix c;
    for(i = 1; i <= ssize; i++)
    {
        for(j = 1; j <= ssize; j++)
        {
            c.m[i][j]=0;
            for(k = 1; k <= ssize; k++)
            {
                c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
                //c.m[i][j]%=mod;
            }
        }
    }
    return c;
}

Matrix quickpagow(LL n)
{
    Matrix m = A, b = I;
    while(n)
    {
        if(n & 1)
            b = Mul(b,m);
        n = n >> 1;
        m = Mul(m,m);
    }
    return b;
}
int main()
{
    LL c;
    int t;
    double a[27];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        ssize = n;
        for(int i = 1; i <= n; i++)
        {
            scanf("%lf",&a[i]);
        }
        memset(I.m,0,sizeof(I.m));
        memset(A.m,0,sizeof(A.m));
        memset(B.m,0,sizeof(B.m));
        for(int i = 0; i <= n; i++)
        {
            //单位矩阵
            I.m[i][i]=1;
        }
        int k, x, M;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&k);
            if(k == 0)
                A.m[i][i] = 1;
            for(int j = 1; j <= k; j++)
            {
                scanf("%d",&x);
                A.m[i][x] = 1.0/k;
            }
        }
        scanf("%d",&M);
        T = quickpagow(M);
        double ans[27];
        for(int i = 1; i <= n; i++)
        {
            ans[i] = 0;
            for(int j = 1; j <= n; j++)
            {
                ans[i]+=a[j]*T.m[j][i];
            }
        }
        printf("%.2lf",ans[1]);
        for(int i = 2; i <= n; i++)
        {
            printf(" %.2lf",ans[i]);
        }
        printf("\n");
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:矩阵快速幂   zoj   

原文地址:http://blog.csdn.net/u012860063/article/details/46858577

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!