zoj 2974 Just Pour the Water矩阵快速幂
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.
题目大意:有N个容器,编号为i的容器可以把自己的水分成k份放进k个容器里,每次把1—N依次操作,求M次后各个容器里的水量。
解题思路:矩阵快速幂(注意K=0的情况,我被坑了一发)。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn=;
double f[maxn]; struct Matrix
{
double a[maxn][maxn];
int n;
}; Matrix Matrix_mult(Matrix A,Matrix B)
{
Matrix C;C.n=A.n;
int i,j,k;
for(i=;i<=A.n;i++)
{
for(j=;j<=A.n;j++)
{
double sum=;
for(k=;k<=A.n;k++)
sum+=A.a[i][k]*B.a[k][j];
C.a[i][j]=sum;
}
}
return C;
} Matrix Matrix_pow(Matrix A,int m)
{
Matrix ret;ret.n=A.n;
memset(ret.a,,sizeof(ret.a));
for(int i=;i<=A.n;i++) ret.a[i][i]=1.0;
while(m)
{
if(m&) ret=Matrix_mult(A,ret);
A=Matrix_mult(A,A);m>>=;
}
return ret;
} void Printf(Matrix A)
{
for(int i=;i<=A.n;i++)
{
double ans=;
for(int j=;j<=A.n;j++)
ans+=A.a[i][j]*f[j];
printf(i==?"%.2lf":" %.2lf",ans);
}
printf("\n");
} int main()
{
int t,i,j,k,p,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=;i<=n;i++) scanf("%lf",f+i);
Matrix A;A.n=n;
for(i=;i<=n;i++)
{
scanf("%d",&k);
for(j=;j<=n;j++) A.a[j][i]=;
if(k==)
{
A.a[i][i]=1.0;continue;
}
double c=1.0/k;
for(j=;j<=k;j++)
{
scanf("%d",&p);A.a[p][i]=c;
}
}
scanf("%d",&m);
Matrix B=Matrix_pow(A,m);
Printf(B);
}
return ;
}
zoj 2974 Just Pour the Water矩阵快速幂的更多相关文章
- zoj 2974 Just Pour the Water (矩阵快速幂,简单)
题目 对于案例的解释请见下图: 这道要变动提取一下矩阵,之后就简单了 具体解释可看代码: #include <string.h> #include <stdio.h> #inc ...
- ZOJ 2974 Just Pour the Water
矩阵快速幂. 构造一个矩阵,$a[i][j]$表示一次操作后,$j$会从$i$那里得到水的比例.注意$k=0$的时候,要将$a[i][j]$置为$1$. #pragma comment(linker, ...
- ACM-ICPC 2018 焦作赛区网络预赛 L:Poor God Water(矩阵快速幂)
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- ZOJ 2794 Just Pour the Water 【矩阵快速幂】
给你n个杯子,每次有特定的到水规则,倒m次请问最后每个被子里还有多少水 我们很容易发现每次变化的规则相同,那么可以set 一个矩阵存放 然后多次倒水就相当于矩阵相乘,在m 范围达到(1<= M ...
- bnuoj 16493 Just Pour the Water(矩阵快速幂)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=16493 [题解]:矩阵快速幂 [code]: #include <cstdlib> #i ...
- ACM-ICPC 2018 焦作赛区网络预赛- L:Poor God Water(BM模板/矩阵快速幂)
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- 焦作网络赛L-Poor God Water【矩阵快速幂】
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- dutacm.club Water Problem(矩阵快速幂)
Water Problem Time Limit:3000/1000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/Others)Tot ...
- ACM-ICPC 2018 焦作赛区网络预赛 L Poor God Water(矩阵快速幂,BM)
https://nanti.jisuanke.com/t/31721 题意 有肉,鱼,巧克力三种食物,有几种禁忌,对于连续的三个食物:1.这三个食物不能都相同:2.若三种食物都有的情况,巧克力不能在中 ...
随机推荐
- Python——函数入门(一)
一.理解函数 举一个例子,当我们需要重复使用一个功能的时候,不可能每次都去复制一次代码,这个时候就需要用到函数了,所谓的函数,简单来说就是给函数取一个名字,当需要用到这个功能的时候,就可以通过这个名字 ...
- java基础—对象转型
一.对象转型介绍 对象转型分为两种:一种叫向上转型(父类对象的引用或者叫基类对象的引用指向子类对象,这就是向上转型),另一种叫向下转型.转型的意思是:如把float类型转成int类型,把double类 ...
- SQL学习总结笔记
SQL语句的效率不仅是sql语句的设计还有一些其他的原因比如网络 .是否有视图.是否有索引等等.这里主要描述的是我个人对于sql设计方面优化的一些见解: 首先要说明一下的是数据库SQL解析顺序: (1 ...
- iOS开发之MVVM在项目中的应用
今天写这篇博客是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇博客的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...
- NOIP模拟赛 路面修整
[题目描述] FJ打算好好修一下农场中某条凹凸不平的土路.按奶牛们的要求,修好后的路面高度应当单调上升或单调下降,也就是说,高度上升与高度下降的路段不能同时出现在修好的路中. 整条路被分成了N段,N个 ...
- 【Mysql】Mysql主从库搭建过程(爬完坑后整理所得)
Mysql主从数据库搭建流程 新手开始学习mysql主从库,遇到一些问题,总结后写出以下流程 下面以5.7.23版本为例介绍 第一步:去官网下载5.7.23版本的免安装压缩包形式的mysql文件,贴上 ...
- leetcode-20-Dynamic Programming
303. Range Sum Query - Immutable 解题思路: Note里说sumRange会被调用很多次..所以简直强烈暗示要做cache啊...所以刚开始,虽然用每次都去遍历数组求和 ...
- Tomcat Bug记录
1.问题:org.apache.tiles.request.render.CannotRenderException: ServletException including path '/WEB-IN ...
- 【原创】Mysql中事务ACID实现原理
引言 照例,我们先来一个场景~ 面试官:"知道事务的四大特性么?" 你:"懂,ACID嘛,原子性(Atomicity).一致性(Consistency).隔离性(Isol ...
- JAVA 基础--开发环境Sublime Text 3 搭建
方法一 打开Sublime Text 3,依次点击Preference, Browse Packages,在打开的窗口中双击User文件夹,新建文件JavaC.sublime-build,用记事本打 ...