zoukankan      html  css  js  c++  java
  • zoj 2853 Evolution

    ZOJ Problem Set - 2853
    Evolution

    Time Limit: 5 Seconds      Memory Limit: 32768 KB

    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 N, M. 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

    Author: JIANG, Jiefeng


    Source: Zhejiang Provincial Programming Contest 2007
    Submit    Status
    //1868165 2009-05-14 09:43:35 Wrong Answer  2853 C++ 3880 1472 Wpl
    //1868191 2009-05-14 09:56:25 Wrong Answer  2853 C++ 3870 1472 Wpl  
    //1868298 2009-05-14 10:59:02 Accepted  2853 C++ 3900 1472 Wpl 
    #include <iostream>
    #define MAX 203
    using namespace std;
    typedef 
    struct node
    {
        
    double matrix[MAX][MAX];
    }Matrix;
    Matrix unit,init,result,c;
    int n,m,t;
    double data[MAX];
    void Init()
    {
        
    int i,j;
        
    double p;
        
    for(i=0;i<n;i++)
            
    for(j=0;j<n;j++)
            {
                init.matrix[i][j]
    =(i==j);  //相当于init刚开始默认全部由自己进化到自己
                unit.matrix[i][j]=(i==j);  //单位矩阵
            }
            
    for(i=0;i<n;i++)
                scanf(
    "%lf",&data[i]);
            scanf(
    "%d",&t);
            
    while(t--)
            {
                scanf(
    "%d%d%lf",&i,&j,&p);
                
    //init.matrix[i][j]+=p;   //why wrong?????
                
    //init.matrix[i][i]-=p;
                init.matrix[j][i]+=p;
                init.matrix[i][i]
    -=p;
            }
    }
    void Cal(int exp)
    {
        
    int i,j,k;
        
    if(exp==1)
            result
    =init;
        
    while(exp!=1)
        {
            
    if(exp&1)
            {
                exp
    --;
                
    for(i=0;i<n;i++)
                    
    for(j=0;j<n;j++)
                    {
                        c.matrix[i][j]
    =0;
                        
    for(k=0;k<n;k++)
                            c.matrix[i][j]
    +=init.matrix[i][k]*unit.matrix[k][j];
                    }
                unit
    =c;
            }
            
    else
            {
                exp
    >>=1;
                
    for(i=0;i<n;i++)
                    
    for(j=0;j<n;j++)
                    {
                        c.matrix[i][j]
    =0;
                        
    for(k=0;k<n;k++)
                            c.matrix[i][j]
    +=init.matrix[i][k]*init.matrix[k][j];
                    }
                init
    =c;
            }
        }
        
    for(i=0;i<n;i++)
            
    for(j=0;j<n;j++)
            {
                result.matrix[i][j]
    =0;
                
    for(k=0;k<n;k++)
                    result.matrix[i][j]
    +=unit.matrix[i][k]*init.matrix[k][j];
            }
    }
    int main()
    {
        
    int i,j;
        
    double r;
        
    while(scanf("%d%d",&n,&m)!=EOF)
        {
            
    if(n==0&&m==0)
                
    break;
            Init();
            Cal(m);  
    //求初始矩阵的m次幂
            r=0;
            
    for(j=0;j<n;j++)
                r
    +=result.matrix[n-1][j]*data[j];
            printf(
    "%.0lf\n",r);
        }
        
    return 0;
    }
  • 相关阅读:
    一个页面通过iframe,获取另一个页面的form
    framework7 点取消后还提交表单解决方案
    sys模块
    logging模块
    MongoDB
    os.path模块
    Redis 复制、Sentinel的搭建和原理说明
    Linux环境下虚拟环境virtualenv安装和使用
    centos7 下通过nginx+uwsgi部署django应用
    NGINX实现负载均衡的几种方式
  • 原文地址:https://www.cnblogs.com/forever4444/p/1456595.html
Copyright © 2011-2022 走看看