zoukankan      html  css  js  c++  java
  • zoj 2974 Just Pour the Water

    ZOJ Problem Set - 2974
    Just Pour the Water

    Time Limit: 1 Second      Memory Limit: 32768 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
    Submit    Status
    //1866226 2009-05-12 15:49:33 Wrong Answer  2974 C++ 0 196 Wpl 
    //1866233 2009-05-12 15:54:23 Wrong Answer  2974 C++ 0 196 Wpl
    //1866276 2009-05-12 16:19:23 Wrong Answer  2974 C++ 0 196 Wpl  
    //1866413 2009-05-12 17:34:59 Accepted  2974 C++ 0 196 Wpl 
    #include <iostream>
    #define MAX 22
    using namespace std;
    typedef 
    struct node
    {
         
    double matrix[MAX][MAX]; 
    }Matrix;
    Matrix unit,init,result;
    int t,n,k;
    double data[MAX];
    void Init()
    {
        
    int i,j;
        scanf(
    "%d",&n);
        
    for(i=1;i<=n;i++)   //输入初始值
            scanf("%lf",&data[i]);
        
    for(i=1;i<=n;i++)
            
    for(j=1;j<=n;j++)
            {
                init.matrix[i][j]
    =0;
                unit.matrix[i][j]
    =(i==j);  //单位矩阵
            }
        
    int num;
        
    double p;
        
    for(i=1;i<=n;i++)
        {
            scanf(
    "%d",&num);
            
    if(num==0//否则除0错误
            {
                init.matrix[i][i]
    =1.0;   //因为没有分东西给别人,故东西全部给自己,第一错误
                continue;
            }
            p
    =1.0/num;
            
    while(num--)
            {
                scanf(
    "%d",&j);
                init.matrix[i][j]
    =p;
            }
        }
        scanf(
    "%d",&k);   //经过k秒
    }
    Matrix Mul(Matrix a,Matrix b)  
    //矩阵乘法
    {
        Matrix c;
        
    int i,j,p;
        
    for(i=1;i<=n;i++)
            
    for(j=1;j<=n;j++)
            {
                c.matrix[i][j]
    =0.0;
                
    for(p=1;p<=n;p++)
                {
                    c.matrix[i][j]
    +=a.matrix[i][p]*b.matrix[p][j];
                }
            }
        
    return c;
    }
    Matrix Cal(
    int exp)  //矩阵幂
    {
        Matrix p,q,res;
        p
    =unit;  //单位矩阵
        q=init;  //初始矩阵
        while(exp!=1)
        {
            
    if(exp&1)//幂是奇数
            {
                exp
    --;
                p
    =Mul(p,q);  //这里忘记写乘,导致很多次错误,要细心
            }
            
    else
            {
                exp
    >>=1;
                q
    =Mul(q,q);
            }    
        }
        res
    =Mul(p,q);
        
    return res;
    }
    int main()
    {
        
    int i,j;
        
    double r;
        scanf(
    "%d",&t);
        
    while(t--)
        {
            Init();
            result
    =Cal(k);
            
    for(i=1;i<=n;i++)
            {
                r
    =0;
                
    for(j=1;j<=n;j++)
                    r
    +=result.matrix[j][i]*data[j];
                
    if((i-1))  //格式控制
                    printf(" ");
                printf(
    "%0.2lf",r);
            }
            printf(
    "\n");
        }
        
    return 0;
    }
  • 相关阅读:
    成功连上数据库顿感世界美好许多
    MySQL数据库基本命令
    杭电1004 Let the Balloon Rise
    大数学习笔记
    安卓传感器开发之指南针
    java Class文件内部结构解析
    mysq数据库实战小型管理系统
    JSplitPane分隔线的用法
    swing 菜单+右键菜单+二级菜单实现
    input type=file 上传文件,同一个文件第二次上传无反应
  • 原文地址:https://www.cnblogs.com/forever4444/p/1455113.html
Copyright © 2011-2022 走看看