标签:
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
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
不说了,其他的都是比较基础的。
转载请注明出处:http://www.cnblogs.com/yuyixingkong/p/4326818.html
题目来源:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1853
代码:
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; int N,M;struct matrix { double mat[210][210]; }; double num[210]; matrix multiply(matrix x,matrix y)//乘 { matrix temp; memset(temp.mat,0,sizeof(temp.mat)); for(int i=0; i<N; i++) { for(int j=0; j<N; j++) { if(x.mat[i][j]==0) continue; for(int k=0; k<N; k++) { if(y.mat[j][k]==0) continue; temp.mat[i][k]+=x.mat[i][j]*y.mat[j][k]; } } } return temp; } matrix quicklymod(matrix a,int n) { matrix res;//单位阵 memset(res.mat,0,sizeof(res.mat)); for(int i=0;i<N;i++) res.mat[i][i]=1; while(n) { if(n&1) res=multiply(res,a); n>>=1; a=multiply(a,a); } /*for(int i=0; i<N; i++) { for(int j=0; j<N; j++) printf("%5.2lf",a.mat[i][j]); printf("\n"); } printf("\n");*/ return res; } int main() { int i,j,T; double p,ans; matrix ant; while(scanf("%d%d",&N,&M),(N||M)) { for(i=0;i<N;i++) { scanf("%lf",&num[i]); for(j=0;j<N;j++) { if(i==j) ant.mat[i][j]=1; else ant.mat[i][j]=0; } } scanf("%d",&T); while(T--) { scanf("%d%d%lf",&i,&j,&p); ant.mat[i][j]+=p; ant.mat[i][i]-=p; } ant=quicklymod(ant,M); ans=0; for(i=0;i<N;i++) { ans+=num[i]*ant.mat[i][N-1]; } printf("%.0lf\n",ans); } return 0; } /* 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 */
标签:
原文地址:http://www.cnblogs.com/yuyixingkong/p/4326818.html