ZOJ 2974Just Pour the Water
来源:互联网 发布:c语言左移运算符 编辑:程序博客网 时间:2024/06/05 05:37
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).
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.
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.
12100.00 100.001 22 1 22
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.
矩阵快速幂,直接按要求建立矩阵即可,注意k=0的问题
#include<map>#include<ctime>#include<cmath> #include<queue> #include<string>#include<vector>#include<cstdio> #include<cstring> #include<iostream>#include<algorithm> using namespace std;#define ms(x,y) memset(x,y,sizeof(x)) #define rep(i,j,k) for(int i=j;i<=k;i++) #define per(i,j,k) for(int i=j;i>=k;i--) #define loop(i,j,k) for (int i=j;i!=-1;i=k[i]) #define inone(x) scanf("%d",&x) #define intwo(x,y) scanf("%d%d",&x,&y) #define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z) typedef long long LL;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int N = 30;int T, n, m, x;double a[N][N], b[N][N], c[N];int main(){for (inone(T); T--;){inone(n);rep(i, 1, n) scanf("%lf", &c[i]);ms(a, 0);rep(i, 1, n){inone(m);rep(j, 1, m) inone(x), a[i][x] = 1.0 / m;if (!m) a[i][i] = 1;}for (inone(m); m; m >>= 1){if (m & 1){rep(i, 1, n){b[1][i] = 0;rep(j, 1, n){b[1][i] += c[j] * a[j][i];}}rep(i, 1, n) c[i] = b[1][i];}rep(i, 1, n) rep(j, 1, n){b[i][j] = 0;rep(k, 1, n) b[i][j] += a[i][k] * a[k][j];}rep(i, 1, n) rep(j, 1, n) a[i][j] = b[i][j];}rep(i, 1, n) printf("%.2lf%s", c[i], i == n ? "\n" : " ");}return 0;}
- Zoj Just Pour the Water 2974
- ZOJ - 2974 Just Pour the Water
- zoj 2974 Just Pour the Water
- zoj 2974 Just Pour the Water 矩阵
- ZOJ - 2974 Just Pour the Water
- ZOJ 2974Just Pour the Water
- [矩阵幂运算] ZOJ 2974 Just Pour the Water
- 【矩阵快速幂】ZOJ 2974 Just Pour the Water
- ZOJ 2974 Just Pour the Water(矩阵快速幂)
- ZOJ 2974-Just Pour the Water(矩阵快速幂)
- 【矩阵快速幂】ZOJ 2974 Just Pour the Water
- ZOJ 2974Just Pour the Water(矩阵快速幂)
- ZOJ 1973 Just Pour the Water(矩阵快速幂)
- ZOJ2974-Just Pour the Water
- ZOJ 2974 Just Pour the Water【5th浙江省赛】【矩阵乘法】
- POJ-2974 Just Pour the Water (矩阵快速冥)
- J - Just Pour the Water 解题报告
- ZOJ 3913 Bob wants to pour water 二分
- 高校邦计算机二级通关之office必杀技答案【2017春】
- [矩阵树定理 模板题] BZOJ 1016 [JSOI2008]最小生成树计数 & HDU 4408 Minimum Spanning Tree
- 我们为什么要拥抱Linq?
- Java编程题练习2017-02-17
- 虚拟机安装
- ZOJ 2974Just Pour the Water
- HDU 3347 Calculate the expression(模拟 + map)
- Qt连接信号到子类槽函数提示父类无相应槽函数
- 107个基础常用JS语法大集合
- 数据库表的连接查询、子查询
- 安卓技能点说法
- ZOJ 2975Kinds of Fuwas
- 架构之美
- HDU1495:非常可乐