日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ZOJ 2853 Evolution[ 矩阵快速幂 ]

發布時間:2024/3/26 编程问答 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ZOJ 2853 Evolution[ 矩阵快速幂 ] 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

傳送門:ZOJ 1853

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 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個物種,p(i,j)表示一輪進化中i->j轉化的比例,問裝化m輪后第n個物種的個數。


思路:因為每輪都是對整個物種的數組進行乘法,然后加起來,正好用矩陣乘法。然后用快速冪加速。


代碼:

#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib>using namespace std;const int N=250; int n; struct Mat{double mat[N][N]; };Mat operator *(Mat a,Mat b) {Mat c;memset(c.mat,0,sizeof(c.mat));for(int k=0;k<n;k++){for(int i=0;i<n;i++){if(a.mat[i][k]<=0) continue; //優化for(int 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 operator ^ (Mat a,int k) {Mat c;for(int i=0;i<n;i++){for(int j=0;j<n;j++){c.mat[i][j]=(i==j); //初始化為單位矩陣}}while(k){if(k&1) c=c*a;a=a*a;k>>=1;}return c;} int main() {int m;//freopen("in.txt","r",stdin);while(scanf("%d%d",&n,&m),n||m){Mat species,p;for(int i=0;i<n;i++)for(int j=0;j<n;j++)p.mat[i][j]=(i==j);for(int i=0;i<n;i++)scanf("%lf",&species.mat[i][0]);int nn;scanf("%d",&nn);int j,k;double v;for(int i=0;i<nn;i++){cin>>j>>k>>v;p.mat[k][j]+=v;p.mat[j][j]-=v;}p=p^m;species=p*species;printf("%.0f\n",species.mat[n-1][0]);//cout<<"----"<<endl;}return 0; }

總結

以上是生活随笔為你收集整理的ZOJ 2853 Evolution[ 矩阵快速幂 ]的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。