题意就是有编号为0~n-1的物种,总共要经过m次的进化,再给出t组从i到j的概率为z的进化方式,以及原来这n种物种各有几个,问经过m次进化以后,编号为n-1的物种有几个?
A为进化率的矩阵,A^m得到m次进化以后物种的变化。进化率矩阵的设计尤为重要,一开始假设所有物种都将进化为它本身,然后再根据输入的进化方式调整得到真正的进化率矩阵。
#include<cstdio>
#include<cstring>
int n;
struct matrix{
double a[200][200];
}ans, base;
matrix multiply(matrix x, matrix y)
{
matrix tmp;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
tmp.a[i][j] = 0;
for (int k = 0; k < n; k++) tmp.a[i][j] += x.a[i][k] * y.a[k][j];
}
return tmp;
}
void fast_mod(int k)
{
while (k)
{
if (k & 1) ans = multiply(ans, base);
base = multiply(base, base);
k >>= 1;
}
}
int main()
{
int m, t, xi, yi;
double s, zi, fin;
int num[205];
while (~scanf("%d%d", &n, &m) && !(n == 0 && m == 0))
{
for (int i = 0; i < n; i++) scanf("%d", &num[i]);
scanf("%d", &t);
memset(base.a, 0, sizeof(base.a));
for (int i = 0; i < n; i++) base.a[i][i] = 1;
while (t--)
{
scanf("%d%d%lf", &xi, &yi, &zi);
base.a[xi][yi] += zi;
base.a[xi][xi] -= zi;
}
memset(ans.a, 0, sizeof(ans.a));
for (int i = 0; i < n; i++) ans.a[i][i] = 1;
fast_mod(m);
fin = 0;
for (int i = 0; i < n; i++)
fin += num[i] * ans.a[i][n-1];
printf("%.0f\n", fin);
}
return 0;
}