ZOJ 1973 Just Pour the Water(矩阵快速幂)
来源:互联网 发布:java cp 加参数 d 编辑:程序博客网 时间:2024/06/05 08:58
题目链接: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
12100.00 100.001 22 1 22
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 longconst 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 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
- POJ-2974 Just Pour the Water (矩阵快速冥)
- 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 2974Just Pour the Water
- ZOJ 2974 Just Pour the Water【5th浙江省赛】【矩阵乘法】
- ZOJ2974-Just Pour the Water
- J - Just Pour the Water 解题报告
- ZOJ 3913 Bob wants to pour water(二分)
- 黑马程序员 集合 3
- DM9161AEP常见问题,除错指南
- 多图片上传插件webuploader
- bzoj2891: 匹配难题 压状dp
- H264编码器性能测试
- ZOJ 1973 Just Pour the Water(矩阵快速幂)
- http协议详解
- 如何在OpenWRT的非交换网卡/单口网卡(OpenWRT x86)下配置802.1Q VLAN
- eclipse中javascript报错问题处理:
- IOS企业版安装包分发“无法连接到...”
- 高仿微信5.2.1主界面架构 包含消息通知
- 颐和园终于上CSDN头条了
- 开发中url传参,特殊字符作为参数传递!
- 使用QUnit对JS进行单元测试