Evolution

Time Limit: 5 Seconds      Memory Limit: 32768 KB

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 NM. 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种物种,编号是0-----N-1,M次进化后,问你编号为N-1的物种有多少数量;

其中要注意的就是i物种进化到j物种的概率是p;(那么剩下的不要忘了);所以单位矩阵初始化对角线的值为1,

然后根据题目进化的概率进行加减;比如P(i, j) = 0.3,则mat[i][j] += 0.3,mat[i][i] -= 0.3;

把题目转化为数学模型,分析后就是 有一个初始序列, 有一个进化率矩阵,求的是初始序列 与进化率矩阵进

行 m 次运算后, 初始序列最后一位的答案,那么显然,可以对进化率矩阵进行快速幂计算

  这里,有一点是要注意的 由于矩阵的大小是200*200的但是一般的笔记本内存不够,至少我的笔记本不

行。。。。。以至于本人纠结了N久,,,谁让题目是服务器改的呢。。。。!<Memory Limit: 32768 KB>

内存就是大!

不说了,其他的都是比较基础的。

转载请注明出处:http://www.cnblogs.com/yuyixingkong/p/4326818.html

题目来源:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1853

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std; int N,M;struct matrix
{
double mat[210][210];
}; double num[]; matrix multiply(matrix x,matrix y)//乘
{
matrix temp;
memset(temp.mat,,sizeof(temp.mat));
for(int i=; i<N; i++)
{
for(int j=; j<N; j++)
{
if(x.mat[i][j]==) continue;
for(int k=; k<N; k++)
{
if(y.mat[j][k]==) continue;
temp.mat[i][k]+=x.mat[i][j]*y.mat[j][k];
}
}
}
return temp;
} matrix quicklymod(matrix a,int n)
{
matrix res;//单位阵
memset(res.mat,,sizeof(res.mat));
for(int i=;i<N;i++) res.mat[i][i]=;
while(n)
{
if(n&)
res=multiply(res,a);
n>>=;
a=multiply(a,a);
}
/*for(int i=0; i<N; i++)
{
for(int j=0; j<N; j++)
printf("%5.2lf",a.mat[i][j]);
printf("\n");
}
printf("\n");*/
return res;
}
int main()
{
int i,j,T;
double p,ans;
matrix ant;
while(scanf("%d%d",&N,&M),(N||M))
{
for(i=;i<N;i++)
{
scanf("%lf",&num[i]);
for(j=;j<N;j++)
{
if(i==j)
ant.mat[i][j]=;
else
ant.mat[i][j]=;
}
} scanf("%d",&T);
while(T--)
{
scanf("%d%d%lf",&i,&j,&p);
ant.mat[i][j]+=p;
ant.mat[i][i]-=p;
} ant=quicklymod(ant,M); ans=;
for(i=;i<N;i++)
{
ans+=num[i]*ant.mat[i][N-];
}
printf("%.0lf\n",ans);
}
return ;
} /*
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
*/

Evolution(矩阵快速幂)zoj2853的更多相关文章

  1. ZOJ - 2853 Evolution 线性变换变成矩阵快速幂

    题意:给你N个数,1~N分别为num[i],  以及T个 (i,j,P) 对于每组(i,j,P),让你将  num[i] 减去 P*num[i]  再把 P*num[i] 加到 num[j] 上.T个 ...

  2. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  3. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  4. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  5. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  6. HDU5950(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...

  7. 51nod 1126 矩阵快速幂 水

    有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...

  8. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

  9. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

  10. hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律

    http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...

随机推荐

  1. 使用WPF制作视频监控多画面切换

    前言 曾有做过一个产品,有一个功能是视频监控模块,视频监控首先想到的是视频多画面切换功能,由于前端是用WPF开发的,所以当时就做了一个多画面切换组件,效果如下: 功能设计前提: 由于要使用海康大华天地 ...

  2. 【vue】http-server开启本地服务

    在写前端页面中,经常会在浏览器运行HTML页面,从本地文件夹中直接打开的一般都是file协议,当代码中存在http或https的链接时,HTML页面就无法正常打开,为了解决这种情况,需要在在本地开启一 ...

  3. ceph 源码安装 configure: error: "Can't find boost spirit headers"

    问题:configure: error: "Can't find boost spirit headers" 解决: 推荐:sudo apt-get install libboos ...

  4. Linux之IRQ domain

    概述 Linux使用IRQ domain来描述一个中断控制器(IRQ Controller)所管理的中断源.换句话说,每个中断控制器都有自己的domain.我们可以将IRQ Domain看作是IRQ ...

  5. Spring Boot切换为APR模式

    Spring Boot内置了tomcat容器,直接运行Application就可以启动web服务器. 在tomcat中提供了三种方式:BIO.NIO.APR. BIO tomcat7以下的版本都是BI ...

  6. 分布式锁实现思路及开源项目集成到springmvc并使用

    分布式锁顾名思义就是在分布式系统下的锁,而使用锁的唯一目的就是为了防止多个请求同时对某一个资源进行竞争性读写 在使用多线程时,为了让某一资源某一时刻只能有一个操作者,经常使用synchronized, ...

  7. Html 常见meta

    html 的meta标签对网页渲染及SEO搜索引擎起着不可忽视的作用.详细的写法一段时间不写,容易忘,所以整理了一下,方便需要时查看. <!DOCTYPE html> <!-- 使用 ...

  8. 基于GTK+3 开发远程控制管理软件(C语言实现)系列 一 开篇

    近期趁公司没项目来,想学习一下C和GTK+3 ,顺道再学习一下Linux下有关网络编程方面的知识. 一.学习知识: 1.C基本语法 2.GTK+3 API学习 GUI相关知识学习 3.Glade使用及 ...

  9. MVC3学习:实现文章上一篇下一篇链接

    文章的显示都是通过id查询数据库来显示.但是文章会经常删除,因此id号可能不是连续的,所以上一篇下一篇文章,不能简单的做id加减法. 我的思路是:先将表格中所有文章的ID号全部放入一个数组中,如果文章 ...

  10. Kafka连接SparkStreaming的两种方式

    第一种方式代码: import org.apache.spark.storage.StorageLevel import org.apache.spark.{HashPartitioner, Spar ...