`
Simone_chou
  • 浏览: 192637 次
  • 性别: 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;
}

 

 

分享到:
评论

相关推荐

    C Programming - Just the FAQS

    The C Language This chapter focuses on some basic elements of the C programming language. When you begin programming in C, you probably will find yourself coming up with basic questions regarding the ...

    C Programming - Just the FAQs

    ### C Programming - Just the FAQs #### 一、书籍概述与背景 《C Programming - Just the FAQs》是一本由多位作者合作完成的专业性书籍,其中包括了Paul S. R. Chisholm、David Hanley、Michael Jones、Michael ...

    just-the-docs:一种现代的,高度可定制的,响应Swift的Jekyll主题,用于通过内置搜索进行记录

    gem "just-the-docs" 并将此行添加到您的Jekyll网站的_config.yml : theme : just-the-docs 然后执行: $ bundle 或自己安装为: $ gem install just-the-docs 或者,您可以在开发站点时在Docker中运行它 $ ...

    VHDL.2008.Just.the.New.Stuff

    ### VHDL 2008: Just the New Stuff #### 概述 《VHDL 2008: Just the New Stuff》是一本专注于介绍VHDL 2008标准中新特性的书籍。VHDL(Very High-Speed Integrated Circuit Hardware Description Language)是一...

    C Programming - Just the FAQS - PDF.rar

    《C Programming - Just the FAQS》是一份针对C语言编程的常见问题解答集,它包含了在C语言编程过程中可能会遇到的各种问题以及相应的解决方案。这份文档的重要性在于,它为程序员提供了快速解决问题的参考,有助于...

    IR.ZIP_The Just

    WORK BOTF OR APPLE IRESERVE AS WOLARD CAN YOU JUST AMENDED THE DII FILE TO SAVE THE WORKD A BOT IS GOOD FOR YOUR DEVOLEOPMENT

    fyzqt.zip_JNE_The Just

    【标题】"fyzqt.zip_JNE_The Just" 提供的资料似乎是一个软件或应用程序的源代码包,其中包含了多个编程文件。"JNE_The Just" 可能是项目名称或者是特定功能的标识符,但具体含义需要进一步的上下文才能明确。 ...

    ffg.rar_The Just_crawler_ffg

    【标题】"ffg.rar_The Just_crawler_ffg" 提供的是一个名为 "The Just_crawler" 的网络爬虫程序,它被封装在 "ffg.rar" 这个压缩包内。这个爬虫专为特定网站设计,用于抓取和收集数据。 【描述】描述中提到,该爬虫...

    mr.rar_The Just

    标题中的"mr.rar_The Just"可能是指一个名为"mr"的项目或软件包,它与"The Just"有关。在描述中提到的"IndigoDJ has no ASIC. Just do nothing."意味着IndigoDJ这个设备或软件没有应用特定集成电路(ASIC),暗示它...

    xmlparser.rar_The Just

    outside the clip. So we can t just fill where the user bits are 0. We also need to check that the clip bit is set.

    posix_types_x32.rar_The Just

    We stash a pointer to the block header, just before the allocated space, so that we can decrement the live count on delete in constant time.

    ASE.zip_The Just

    ASE.zip_The Just 是一个与IT相关的压缩文件,其标题暗示了这可能是一个关于软件工程或者特定编程语言的资源包,尤其是"Unlock the feature to download"这部分,这通常指的是访问或启用某个软件功能的过程,可能是...

    C Programming Just the FAQs

    ### C Programming Just the FAQs #### 书籍概述 《C Programming: Just the FAQs》是一本针对C程序员的基础性书籍,由Paul S.R. Chisholm、David Hanley、Michael Jones、Michael Lindner 和 Lloyd Work共同编写...

    man.rar_The Just

    很抱歉,但根据您给出的信息,"man.rar_The Just" 和 "the_just" 标签以及压缩包中的 "www.pudn.com.txt" 和 "Man" 文件名,并没有直接指向任何特定的IT知识点。描述中提到的是“学习关于游戏”的内容,但这仍然非常...

    pandawa-lima.zip_The Just

    【标题】"pandawa-lima.zip_The Just" 提示我们这可能是一个与解压缩相关的任务,其中涉及一个名为 "pandawa-lima.zip" 的压缩文件,它似乎有一个特定的密码,密码是 "the_just"。在IT领域,压缩文件常用于存储和...

    nrs.rar_The Just

    XXX: This is just for liblustre. Remove the if defined directive when the cfs_ prefix is dropped cfs_list_head.

    Watercss一个justaddcss集合的样式使简单的网站更好一点

    在实际应用中,Water.css可以极大地提高开发效率,特别是在创建个人博客、小型项目或者进行快速原型设计时。开发者无需从零开始编写CSS,而是可以直接利用Water.css提供的预设样式,快速打造出具有专业感的网站。 ...

    async-thread.rar_The Just

    This is similar to a workqueue, but it is meant to spread the operations across all available cpus instead of just the CPU that was used to queue the work.

    lguest_device.rar_The Just

    Lguest guests use a very simple method to describe devices. It s a series of device descriptors contained just above the top of normal Guest memory.

Global site tag (gtag.js) - Google Analytics