zoj 2853 Evolution

来源:互联网 发布:奔驰丰城市网络问 编辑:程序博客网 时间:2024/06/06 15:38

 

/*

第一次写的时候,因为没有注意导致栈溢出 

zoj 的这个关于矩阵的题目让我感觉自己对矩阵还不是很了解,感觉这个变形让人很想不到的

做了这么多算是把矩阵这个内容掌握了,接下。。。 

*/

#include <iostream>//22316032010-07-19 18:35:02 Accepted2853C++4460840悔惜晟

#include <cstdio>

#include <cstring>

using namespace std;

 

const int N = 205;//double  表示8位 小心超内存 

typedef struct node

{

double  num[N][N];

}Mat;

Mat init, unit;

/*

Mat mal(Mat a, Mat b, int n)

{

Mat r;

for(int i = 0; i < n; i++)

for(int j = 0; j < n; j++)

r.num[i][j] = 0;

for(int i = 0; i < n; i++)

for(int k = 0; k < n; k++)

{

if(a.num[i][k])

for(int j = 0; j < n; j++)

if(b.num[k][j])

r.num[i][j] = (r.num[i][j] + a.num[i][k] * b.num[k][j]);

}

return r;

}

*/

void mul(int m, int n)

{

Mat r;

for(int i = 0; i < n; i++)

for(int j = 0; j < n; j++)

r.num[i][j] = 0;

while(m)

{

if(m & 1)

{

for(int i = 0; i < n; i++)

for(int j = 0; j < n; j++)

r.num[i][j] = 0;

for(int i = 0; i < n; i++)

for(int k = 0; k < n; k++)

{

if(unit.num[i][k])

for(int j = 0; j < n; j++)

if(init.num[k][j])

r.num[i][j] = (r.num[i][j] + unit.num[i][k] * init.num[k][j]);

}

m--;

unit = r;

}

else

{

for(int i = 0; i < n; i++)

for(int j = 0; j < n; j++)

r.num[i][j] = 0;

for(int i = 0; i < n; i++)

for(int k = 0; k < n; k++)

{

if(init.num[i][k])

for(int j = 0; j < n; j++)

if(init.num[k][j])

r.num[i][j] = (r.num[i][j] + init.num[i][k] * init.num[k][j]);

}

init = r;

m >>= 1;

}

}

}

int main()

{

int n, m;

while(scanf("%d %d", &n, &m) != EOF)

{

if(n == 0 && m == 0 )

break;

double data[N];

for(int i = 0; i < n; i++)

scanf("%lf", &data[i]);

int t;

int x, y;

double p;

for(int i = 0; i < n; i++)

for(int j = 0; j < n; j++)

{

init.num[i][j] = (i == j);//相当于init刚开始默认全部由自己进化到自己 

unit.num[i][j] = (i == j);

}

scanf("%d", &t);

while(t--)

{

scanf("%d %d %lf", &x, &y, &p);

 

//init.num[y][x] += p;

init.num[x][y] += p;

init.num[x][x] -= p;

}

   mul( m, n);

double sum = 0;

for(int i = 0; i < n; i++)

{

//sum += unit.num[n - 1][i] * data[i];

sum += unit.num[i][n - 1] * data[i];

}

printf("%0.0lf/n", sum);

}

}

 

http://www.cnblogs.com/forever4444/archive/2009/05/14/1456595.html

 

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 NM. 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

 

原创粉丝点击