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

Zhejiang Provincial Programming Contest 2007 Evolution (矩阵快速幂)

时间:2020-07-12 22:30:56      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:any   nes   mode   ring   mat   lin   ultimate   imp   cti   

题面

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

思路

题目的意思就是,给你一些物种和其数量,与他们之间的进化关系。求n轮之后的第n-1个物种的数量。每次的物种之间的进化我们可以用一个矩阵表示,然后对这个矩阵进行快速幂运算,最后和数量矩阵相乘求和就是答案了。这个题目要注意数组过大爆栈的问题,我们直接把数组开在全局就好了;

代码实现

#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
const int inf =0x3f3f3f3f;
const int  maxn=202;
struct Matrix {
    double maze[maxn][maxn];
    Matrix () {
        memset (maze,0,sizeof (maze));
    }
}b,ans;
int n,m;
Matrix mul (Matrix a,Matrix b) {
    Matrix ans;
    for (int i=0;i<n;i++)
     for (int j=0;j<n;j++) {
         for (int k=0;k<n;k++) {
             ans.maze[i][j]=(ans.maze[i][j]+a.maze[i][k]*b.maze[k][j]);
         }
     }
      return ans;
}
Matrix fast_pow (Matrix a,int k) {
    Matrix ans;
    for (int i=0;i<n;i++) ans.maze[i][i]=1.0;
    while (k) {
        if (k&1) ans=mul (ans,a);
        a=mul (a,a);
        k>>=1;
    }
    return ans;
}
inline void solve () {
   double a[maxn];
   int x,y,k;
   double z;
   for (int i=0;i<n;i++) cin>>a[i];
   cin>>k;
   for (int i=0;i<n;i++) b.maze[i][i]=1.0;
   while (k--) {
       cin>>x>>y>>z;
       b.maze[x][x]-=z;
       b.maze[y][x]+=z;
   }
   double sum=0.0;
   ans=fast_pow (b,m);
   for (int i=0;i<n;i++) {
       sum+=ans.maze[n-1][i]*a[i];
   }
   printf ("%.0lf\n",sum);
}
int main () { 
    while (cin>>n>>m) {
        if (n==0&&m==0) break;
        solve ();
    }  
    return 0;
}

Zhejiang Provincial Programming Contest 2007 Evolution (矩阵快速幂)

标签:any   nes   mode   ring   mat   lin   ultimate   imp   cti   

原文地址:https://www.cnblogs.com/hhlya/p/13290071.html

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