集训队专题(2)1007 Evolution
来源:互联网 发布:宾馆软件破解版 编辑:程序博客网 时间:2024/06/05 16:30
Evolution
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 52 Accepted Submission(s) : 18
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
此题也是比较基础的矩阵快速幂,注意浮点数的类型就好了
#include <cstdio>#include <cstring>#define MAX 203struct Matrix{double mat[MAX][MAX];}init,unit;double pop[MAX];int N,M,T;void Init(){memset(init.mat,0,sizeof(init.mat));memset(unit.mat,0,sizeof(unit.mat));for(int i=0; i<N; i++){init.mat[i][i] = 1;//开始默认全部由自己进化到自己unit.mat[i][i] = 1;//初始为单位矩阵}for(int i=0; i<N; i++)scanf("%lf",&pop[i]);scanf("%d",&T);int i,j;double p;while(T--){scanf("%d%d%lf",&i,&j,&p);init.mat[j][i] += p;init.mat[i][i] -= p;}}Matrix multi(Matrix a,Matrix b){Matrix c;for(int i=0; i<N; i++){for(int j=0; j<N; j++){c.mat[i][j] = 0;for(int k=0; k<N; k++){c.mat[i][j] += a.mat[i][k]*b.mat[k][j];}}}return c;}Matrix fast_pow(int x){Matrix i=init,ans=unit; while(x){ if(x&1) ans = multi(ans,i); i = multi(i,i); x >>= 1; } return ans;}int main(){double ans;while(scanf("%d%d",&N,&M)&&(N||M)){Init();Matrix result=fast_pow(M);ans = 0;for(int i=0; i<N; i++){ans += result.mat[N-1][i]*pop[i];}printf("%.0lf\n",ans);}return 0;}
- 集训队专题(2)1007 Evolution
- 集训队专题(3)1007 HDU Today
- 集训队专题(5.1)1007 All Friends
- 集训队专题(6)1007 Fire Net
- 集训队专题(8)1007 Remainder
- 集训队专题(2)1001 Fibonacci
- 集训队专题(2)1002 Tr A
- 集训队专题(2)1004 Queuing
- 集训队专题(2)1006 Mistwald
- 集训队专题(1)1007 Hat’s Words
- 集训队专题(7)1007 方格取数
- 集训队专题(2)1003 Matrix Power Series
- 集训队专题(2)1005 A Simple Math Problem
- 集训队专题(1)1001 Immediate Decodability
- 集训队专题(1)1002 统计难题
- 集训队专题(1)1003 Phone List
- 集训队专题(1)1005 Ancient Printer
- 集训队专题(1)1006 单词数
- C内存管理
- SACK信息
- 【Android】【笔记】《Android 入门经典》part 14 Service
- Codefroces 618D Hamiltonian Spanning Tree 树型动规
- TCP显示冲突通告(ECN)
- 集训队专题(2)1007 Evolution
- 【NYOJ】[463]九九乘法表
- JFinal+WebUploader实现图片的异步上传
- 第一讲、整数划分(回溯实现)
- 【JS】【笔记】JavaScript入门经典(第5版)第1章 概述
- [c++]TRACE 宏的利用
- 求一份 电力猫 QCA6410 原理图等
- 第一讲、整数划分(分治实现之二)
- 【JS】【笔记】JavaScript入门经典(第5版)第2章 简单的脚本