zoj 2853 Evolution(矩阵快速幂)

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 haveN (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 Tand 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

代码:

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

struct mat
{
    double t[201][201];
    void set()
    {
        memset(t,0,sizeof(t));
    }
} a,b;

mat multiple(mat a,mat b,int n)
{
    int i,j,k;
    mat temp;
    temp.set();
    for(i=0; i<n; i++)
        for(j=0; j<n; j++)
        {
            if(a.t[i][j]>0)
                for(k=0; k<n; k++)
                    temp.t[i][k]+=a.t[i][j]*b.t[j][k];
        }
    return temp;
}

mat quick_mod(mat b,int n,int m)
{
    mat t;
    t.set();
    for(int i=0; i<n; i++) t.t[i][i]=1.0;
    while(m)
    {
        if(m&1)
        {
            t=multiple(t,b,n);
        }
        m>>=1;
        b=multiple(b,b,n);
    }
    return t;
}
void init(int n,int m)
{
    int x,y,k;
    double z;
    double t[201];
    for(int i=0; i<n; i++)
        scanf("%lf",&t[i]);
    cin>>k;
    b.set();
    for(int i=0; i<n; i++) b.t[i][i]=1.0;
    while(k--)
    {
        scanf("%d%d%lf",&x,&y,&z);
        b.t[x][x]-=z;
        b.t[y][x]+=z;
    }
    a=quick_mod(b,n,m);
    double sum=0;
    for(int i=0;i<n;i++)
    {
        sum+=a.t[n-1][i]*t[i];
    }
    printf("%.0lf\n",sum);
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        if(!n&&!m) break;
        init(n,m);
    }
    return 0;
}

zoj 2853 Evolution(矩阵快速幂),布布扣,bubuko.com

时间: 2024-12-25 23:07:10

zoj 2853 Evolution(矩阵快速幂)的相关文章

zoj 2853 Evolution 矩阵快速幂

在我的电脑上code::blocks运行过不了,但代码是可以AC的,很是郁闷. 问了大神,知道了函数的参数是放在栈区,结构体太大的话,栈就爆了,如是后来就听从大神的意见用引用改写了. 传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1853 #include <stdio.h> #include <string.h> #include <stdlib.h> int Len_Matrix; in

zoj 3497 Mistwald 矩阵快速幂

Mistwald Time Limit: 2 Seconds      Memory Limit: 65536 KB In chapter 4 of the game Trails in the Sky SC, Estelle Bright and her friends are crossing Mistwald to meet their final enemy, Lucciola. Mistwald is a mysterious place. It consists of M * N s

zoj 2974 Just Pour the Water矩阵快速幂

Just Pour the Water Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

ZOJ 3690 &amp; HDU 3658 (矩阵快速幂+公式递推)

ZOJ 3690 题意: 有n个人和m个数和一个k,现在每个人可以选择一个数,如果相邻的两个人选择相同的数,那么这个数要大于k 求选择方案数. 思路: 打表推了很久的公式都没推出来什么可行解,好不容易有了想法结果WA到天荒地老也无法AC.. 于是学习了下正规的做法,恍然大悟. 这道题应该用递推 + 矩阵快速幂. 我们设F(n) = 有n个人,第n个人选择的数大于k的方案数: G(n) = 有n个人,第n个人选择的数小于等于k的方案数: 那么递推关系式即是: F(1)=m?k,G(1)=k F(n

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

给你n个杯子,每次有特定的到水规则,倒m次请问最后每个被子里还有多少水 我们很容易发现每次变化的规则相同,那么可以set 一个矩阵存放 然后多次倒水就相当于矩阵相乘,在m 范围达到(1<= M <= 1,000,000,000) 的情况下使用矩阵快速幂再好不过 这到题目注意的一点是,得使用Double 变量,如果使用FLoat会导致Wrong Answer Source Code: //#pragma comment(linker, "/STACK:16777216") /

zoj 3538 Arrange the Schedule(矩阵快速幂)

Arrange the Schedule Time Limit: 1 Second      Memory Limit: 65536 KB In Summer 2011, the ZJU-ICPC Team has a n-days training schedule. ZJU-ICPC Team has been divided into 4 Group: Akiba, BiliBili, CIA, Double(Group A, B, C, D). There is a group in c

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

传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2974 题意 给出n个杯子与初始水量同时进行操作 将其中的水同时平均分入所指定的杯子 进行x次后 输出杯子剩余水量 刚拿到这个题,第一反应是递推找规律,但是因为每个杯子的初始水量是未知的,所以能找的只是每个杯子水量与其余杯子水量的关系. 但是看到了操作次数巨大,而且最多只有20个杯子,感觉可以用快速幂去做. 我们假设矩阵a[i][j]代表第i个杯子的水有a[i][j

ZOJ 2105 Number Sequence(矩阵快速幂)

题意: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给定A,B,求f(n). 法一: 网上较多的题解都提到了寻找1 1循环节的方法,的确非常巧妙,每位0~6,共7种可能,相邻两位共49种可能,因此循环周期至多为49,一旦出现相同数对,那么其后必相同.但是,该方法只是简单提及了49,却并没有证明1 1循环节一定存在,没有排除可能前面一段不循环,后面一段开始周期性循环的可能性.(是我悟性太差吗,为什么大多数题解都只谈

ZOJ 3256 Tour in the Castle 矩阵快速幂加速

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3256 题意:给一个n*m的棋盘,求从左上到左下的经过所有格子的方案数 在左边加一列问题就变成了求回路 由于m很大,所以我们需要按列dp 构造出矩阵后,用矩阵快速幂加速 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include&l