zoj 2853 Evolution
?
/*
第一次寫的時候,因為沒有注意導致棧溢出?
zoj 的這個關于矩陣的題目讓我感覺自己對矩陣還不是很了解,感覺這個變形讓人很想不到的
做了這么多算是把矩陣這個內容掌握了,接下。。。?
*/
#include <iostream>//2231603 2010-07-19 18:35:02 Accepted 2853 C++ 4460 840 悔惜晟
#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
?
EvolutionTime 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?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
?
總結
以上是生活随笔為你收集整理的zoj 2853 Evolution的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅谈C++中memset函数的用法
- 下一篇: Windows Defender Ant