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. Flask系列07--Flask中的CBV, 蓝图的CBV

    一.CBV使用 class base view 和django中类似 class Login(views.MethodView): # methods=["POST"," ...

  2. linux安装mysql详细步骤

    最近买了个腾讯云服务器,搭建环境. 该笔记用于系统上未装过mysql的干净系统第一次安装mysql.自己指定安装目录,指定数据文件目录. linux系统版本: CentOS 7.3 64位 安装源文件 ...

  3. C语言Socket-模拟远程CMD(客户端向服务器发送命令,服务器执行该命令)

    服务端(server) #include <stdio.h> #include <winsock2.h> #pragma comment(lib,"ws2_32.li ...

  4. mysql查询语句分析 explain/desc用法

    explain或desc显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. explain 数据表 或 desc 数据表 显示数据表各字段含义 ...

  5. js中的stopImmediatePropagation方法和stopPropagation方法的区别

    看到e.stopImmediatePropagation()这个方法时,记忆有点模糊了.特地回顾一下. 基本概念 stopImmediatePropagation方法:该方法作用在当前节点及事件链的所 ...

  6. lnmp平台搭设

    软件链接:https://pan.baidu.com/s/14gAZ67iXWhEdzvEXMiGfVg             提取码:ai1s 只是在一台服务器上搭设,为centos7.2环境 安 ...

  7. cStringIO 实现指定大小的字符串缓存

    StringIO经常被用来作为字符串的缓存,以下实现无论写入多少字符串,总能返回一个指定大小的缓存 from cStringIO import StringIO class CustomStringI ...

  8. python批量拷贝文件

    普通批量拷贝文件 import os import shutil import logging from logging import handlers from colorama import Fo ...

  9. Docker数据管理(五)

    一.什么是数据卷 生成环境中使用docker的过程中,往往需要对数据进行持久化,或者需要多个容器之间进行数据共享,这个就涉及到了容器数据管理 容器中管理数据主要有两种方式: 数据卷:容器内数据之间映射 ...

  10. 监督学习——决策树理论与实践(下):回归决策树(CART)

    介绍 决策树分为分类决策树和回归决策树: 上一篇介绍了分类决策树以及Python实现分类决策树: 监督学习——决策树理论与实践(上):分类决策树          决策树是一种依托决策而建立起来的一种 ...