Evolution +zoj+矩阵快速幂

Time Limit: 5000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu


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

解决方案:题意我就不说了,这题相当于矩阵快速密的模板题

code:

#include<iostream>
#include<cstdio>
#include<cstring>
#define MMAX 203
using namespace std;
int N;
double MM[203];
class Mat{
    public:
   double mat[MMAX][MMAX];

};
Mat one;
void init(){
for(int i=0;i<MMAX;i++)
    for(int j=0;j<MMAX;j++)
        one.mat[i][j]=(i==j)?1.0:0.0;
}

Mat mul(Mat a, Mat b) {
    Mat c;
    memset(c.mat, 0, sizeof(c.mat));
    int i, j, k;
    for(k = 0; k < N; ++k) {
        for(i = 0; i < N; ++i) {
            if(a.mat[i][k] <= 0)  continue;   ///剪枝很重要
            for(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 mulmul(Mat a, int k) {
    Mat c;
    int i, j;
    for(i = 0; i < N; ++i)
        for(j = 0; j < N; ++j)
            c.mat[i][j] = (i == j);    //初始化为单位矩阵

    for(; k; k >>= 1) {
        if(k&1) c = mul(a,c);

        a = mul(a,a);
    }
    return c;
}
int main(){
    int M;
    init();
    while(~scanf("%d%d",&N,&M)&&(N+M)){
        for(int i=0;i<N;i++){
            scanf("%lf",&MM[i]);
        }
        int t;
        scanf("%d",&t);
        Mat A=one;
        for(int i=0;i<t;i++){
            int a,b;
            double p;
            scanf("%d%d%lf",&a,&b,&p);
            A.mat[a][a]-=p;
            A.mat[b][a]+=p;
        }
        Mat c=mulmul(A,M);
        double sum=0.0;

            for(int k=0;k<N;k++){
                sum+=MM[k]*c.mat[N-1][k];

            }

        printf("%.0f\n",sum);
    }






    return 0;}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值