【矩阵快速幂】ZOJ 2974 Just Pour the Water

Source : ZOJ 2974 Just Pour the Water

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1973


Problem Description
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.

题意

有n个容器,指定某容器将其水分成k份,分别给其他k个容器倒水,问操作m次后n个容器剩余水量。

示例

Sample Input
1
2
100.00 100.00
1 2
2 1 2
2
Sample Output

75.00 125.00


思路

每次操作都可以看成之前的水量矩阵乘以一个操作矩阵,推倒一下很容易构建,m很大,矩阵快速幂,1A!

这里写图片描述


参考代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

typedef long long ll;
const int _max = 20 + 10;

int n,k,x;
ll m;

struct matrix{
  double m[_max][_max];
};

matrix p,ans,a,b;

matrix multi(matrix a,matrix b){
  matrix c;
  for(int i = 0; i < n; ++ i)
  for(int j = 0;j < n; ++ j){
    c.m[i][j] = 0;
    for(int k = 0; k < n; ++ k)
        c.m[i][j]+=a.m[i][k]*b.m[k][j];
  }
  return c;
}

matrix quickpow(matrix a,ll b){
  matrix ans = p;
  while(b){
    if(b&1) ans = multi(ans,a);
    b>>=1;
    a = multi(a,a);
  }
  return ans;
}

void init(){//初始化单位矩阵
  fill(p.m[0],p.m[_max],0);
  for(int i = 0; i < _max; ++ i) p.m[i][i] = 1;
}

int main(){
  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  #endif // ONLINE_JUDGE
  int T;cin>>T;
  init();
  while(T--){
    scanf("%d",&n);
    fill(a.m[0],a.m[n],0);
    for(int i = 0; i < n; ++ i){
        scanf("%lf",&a.m[0][i]);
    }
    fill(b.m[0],b.m[n],0);
    for(int i = 0; i < n; ++ i){
       scanf("%d",&k);
       if(k == 0) b.m[i][i] = 1;//k==0要处理的,保证每行之和为1
       for(int j = 0; j < k; ++ j){
         scanf("%d",&x);
         b.m[i][x-1] = 1.0/k;
       }
    }
    scanf("%lld",&m);
    b = quickpow(b,m);
    ans = multi(a,b);
    printf("%.2f",ans.m[0][0]);
    for(int i = 1; i < n; ++ i) printf(" %.2f",ans.m[0][i]);
    printf("\n");
  }
  return 0;
}
  • 加粗 Ctrl + B
  • 斜体 Ctrl + I
  • 引用 Ctrl + Q
  • 插入链接 Ctrl + L
  • 插入代码 Ctrl + K
  • 插入图片 Ctrl + G
  • 提升标题 Ctrl + H
  • 有序列表 Ctrl + O
  • 无序列表 Ctrl + U
  • 横线 Ctrl + R
  • 撤销 Ctrl + Z
  • 重做 Ctrl + Y
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值