传送门:ZOJ 1853

Description

Evolution is a long, long process with extreme complexity and involves many species. Dr. C. P. Lottery is currently investigating a simplified model of evolution: consider that we have N (2 <= N <= 200) species in the whole process of evolution, indexed from 0 to N -1, and there is exactly one ultimate species indexed as N-1. In addition, Dr. Lottery divides the whole evolution process into M (2 <= M <= 100000) sub-processes. Dr. Lottery also gives an 'evolution rate' P(i, j) for 2 species i and j, where i and j are not the same, which means that in an evolution sub-process, P(i, j) of the population of species i will transform to species j, while the other part remains unchanged.

Given the initial population of all species, write a program for Dr. Lottery to determine the population of the ultimate species after the evolution process. Round your final result to an integer.

Input

The input contains multiple test cases!

Each test case begins with a line with two integers N, M. After that, there will be a line with N numbers, indicating the initial population of each species, then there will be a number T and T lines follow, each line is in format "i j P(i,j)" (0 <= P(i,j) <=1).

A line with N = 0 and M = 0 signals the end of the input, which should not be proceed.

Output

For each test case, output the rounded-to-integer population of the ultimate species after the whole evolution process. Write your answer to each test case in a single line.

Notes

  • There will be no 'circle's in the evolution process.
  • E.g. for each species i, there will never be a path i, s1, s2, ..., st, i, such that P(i,s1) <> 0, P(sx,sx+1) <> 0 and P(st, i) <> 0.
  • The initial population of each species will not exceed 100,000,000.
  • There're totally about 5 large (N >= 150) test cases in the input.

Example

Let's assume that P(0, 1) = P(1, 2) = 1, and at the beginning of a sub-process, the populations of 0, 1, 2 are 40, 20 and 10 respectively, then at the end of the sub-process, the populations are 0, 40 and 30 respectively.

Sample Input

2 3
100 20
1
0 1 1.0
4 100
1000 2000 3000 0
3
0 1 0.19
1 2 0.05
0 2 0.67
0 0

Sample Output

120
0

题目大意:总体是一个进化的过程,共有n个物种,p(i,j)表示一轮进化中i->j转化的比例,问装化m轮后第n个物种的个数。

思路:因为每轮都是对整个物种的数组进行乘法,然后加起来,正好用矩阵乘法。然后用快速幂加速。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>using namespace std;const int N=250;
int n;
struct Mat{double mat[N][N];
};Mat operator *(Mat a,Mat b)
{Mat c;memset(c.mat,0,sizeof(c.mat));for(int k=0;k<n;k++){for(int i=0;i<n;i++){if(a.mat[i][k]<=0) continue;  //优化for(int j=0;j<n;j++){if(b.mat[k][j]<=0) continue;c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];}}}return c;
}Mat operator ^ (Mat a,int k)
{Mat c;for(int i=0;i<n;i++){for(int j=0;j<n;j++){c.mat[i][j]=(i==j);  //初始化为单位矩阵}}while(k){if(k&1) c=c*a;a=a*a;k>>=1;}return c;}
int main()
{int m;//freopen("in.txt","r",stdin);while(scanf("%d%d",&n,&m),n||m){Mat species,p;for(int i=0;i<n;i++)for(int j=0;j<n;j++)p.mat[i][j]=(i==j);for(int i=0;i<n;i++)scanf("%lf",&species.mat[i][0]);int nn;scanf("%d",&nn);int j,k;double v;for(int i=0;i<nn;i++){cin>>j>>k>>v;p.mat[k][j]+=v;p.mat[j][j]-=v;}p=p^m;species=p*species;printf("%.0f\n",species.mat[n-1][0]);//cout<<"----"<<endl;}return 0;
}

ZOJ 2853 Evolution[ 矩阵快速幂 ]相关推荐

  1. ZOJ 2317 Nice Patterns Strike Back(矩阵快速幂)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2317 题意:给你两种颜色,黑色和白色,填充n*m的方格,每个格子一种颜色, ...

  2. 【ZOJ 2974】Just Pour the Water(矩阵快速幂)

    传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2974 题意 给出n个杯子与初始水量同时进行操作 将其中的水同时平均 ...

  3. 矩阵快速幂 学习笔记

    据说,矩阵快速幂在递推式优化上相当神奇,而且效率很高... 两矩阵相乘,朴素算法的复杂度是O(N^3).如果求一次矩阵的M次幂,按朴素的写法就是O(N^3*M).既然是求幂,不免想到快速幂取模的算法, ...

  4. 矩阵快速幂+构造方法

    与快速幂一样,可以将递推式通过二进制的方式来进行优化,这个学了快速幂就是十分容易理解 大概的板子如下: struct mat///自己定义大小的矩阵 {ll m[11][11]; }; mat mul ...

  5. 【做题】SRM701 Div1 Hard - FibonacciStringSum——数学和式&矩阵快速幂

    原文链接 https://www.cnblogs.com/cly-none/p/SRM701Div1C.html 题意:定义"Fibonacci string"为没有连续1的01串 ...

  6. 快速幂 + 矩阵快速幂

    快速幂 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #define LL lo ...

  7. HDU4549(矩阵快速幂+快速幂)

    f(n)=a^f(n-1) + b^f(n-2):计算矩阵部分用矩阵快速幂:计算a的幂次和b的幂次用快速幂. #include<iostream> #include<algorith ...

  8. [HNOI2008]GT考试[矩阵快速幂+kmp优化的dp]

    解题思路:假如说我们用f[i]表示长度为i的串能组合成无不吉利数字的组合的个数的话我们无法找到f[i]和f[i+1]的关系,就是我们下一位填某个数字会不会出现不吉利串,这就和你前面的串末尾于不吉利串重 ...

  9. I-Matrix Power Series POJ - 3233 矩阵快速幂+分治

    I-Matrix Power Series POJ - 3233 矩阵快速幂+分治 Problem Description Given a n × n matrix A and a positive ...

最新文章

  1. 技术项目 - Linux Swap
  2. 求伯君:向暴雪学习 金山不求一夜暴富
  3. Keras: 多输入及混合数据输入的神经网络模型
  4. 记录element-ui级联选择器,二级三级列表无法显示的解决办法
  5. opencv6-调整图像亮度和对比度
  6. Spring5 新特性
  7. wifi抓包解读(实战教程)
  8. Java JavaEE JavaSE JavaME JavaWEB 之间的区别与联系
  9. 使用XMAPP启动MySQL出现Error MySQL shutdown unexpectedly 的解决办法
  10. 团队项目(六)- 事后诸葛亮分析(江山代有才人秃)
  11. Matlab_R2016a 中文破解版 安装教程
  12. 旧金山犯罪预测与可视化分析
  13. Python # 扫描端口功能 # 获取网卡的Mac地址 # 局域网扫描器IP地址和MAC地址,获取网卡名称和其ip地址
  14. mongoDB常用查询更新删除语句
  15. VLC WebPlugin中文
  16. python索引取值_对pandas的层次索引与取值的新方法详解
  17. 在深夜加油站遇见哈利波特
  18. 【学术相关】科研工作者最容易出现的10种错觉,即使跌入谷底也要奋力前行...
  19. Ewebeditor使用说明(转载)
  20. 黑马JAVA P54 方法常见问题

热门文章

  1. 使用Python实现所有算法!Github 标星 3w+,热榜第一
  2. 项目-OOP版电子词典
  3. CSDN---Markdown:换行、空格、目录、复杂表格和注脚
  4. 3d饼图 vue_3D 饼图在 VUE 中的实现
  5. 杰里之ESD 测试方式【篇】
  6. 阿里云服务器(Centos6.9)上设置可远端访问的jupyter notebook
  7. 官网快速下载VScode
  8. 【ELT.ZIP】啃论文俱乐部——学术科研方法论沉淀辑
  9. 2023最新可用的代挂网系统源码/一键安装+对接的LoginSystem
  10. 【问底】徐汉彬:大规模网站架构的缓存机制和几何分形学