码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 2853 Evolution(矩阵快速幂)

时间:2014-05-21 13:43:27      阅读:444      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   c   code   

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

zoj 2853 Evolution(矩阵快速幂)

标签:des   style   blog   class   c   code   

原文地址:http://blog.csdn.net/knight_kaka/article/details/26439763

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!