Not difficult to think of the problem. First set F[I][J] f[i][j] to indicate the blood volume is I I, the position of the expected probability of J J. The
introduced the equation and found that when a point a[i]=0 a[i]=0, it could not be delivered. So to each layer for elimination.
the coefficients matrix for each elimination is found to be the same, but the constants are different. You can think of the constant as a polynomial, first N3 n^3 pretreatment, and then each layer directly N2 n^2 brought into the good.
Complexity O (n3+n2∗hp) O (n^3+n^2*hp)
#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int maxn=155,maxe=10005,maxhp=20005;
int N,m,n,hp,a[maxn],fir[maxn],nxt[maxe],son[maxe],tot;
struct data{double A[MAXN];
Data () {memset (a,0,sizeof (a));}
Data operator-(const data &b) const{data C; for (int i=0;i<=n;i++) c.a[i]=a[i]-b.a[i];
Data operator * (const double &val) const{data C; for (int i=0;i<=n;i++) C.a[i]=a[i]*val;
Data operator/(const double &val) const{data C; for (int i=0;i<=n;i++) c.a[i]=a[i]/val; return c;
}} _K[MAXN],ANS[MAXN];
Double K[MAXN][MAXN]; void Gs () {for (int i=1;i<=n;i++) {int, where; double _max=0 for (int j=i;j<=n;j++) if (K[j][i]>_max) WH
Ere=j, _max=k[j][i]; for (int j=1;j<=n;j++) swap (k[i][j],k[where][j]);
Swap (_k[i],_k[where]);
for (int j=i+1;j<=n;j++) {double t=k[j][i]/k[i][i]; for (int k=1;k<=n;k++) k[j][k]-=t*k[i][k];
_k[j]=_k[j]-_k[i]*t;
for (int i=n;i>=1;i--) {ans[i]=_k[i]* (-1);
for (int j=i+1;j<=n;j++) ANS[I]=ANS[I]-ANS[J]*K[I][J];
Ans[i]=ans[i]/k[i][i];
} int D[MAXN];
Double ANS,F[MAXHP][MAXN]; void Add (int x,int y) {son[++tot]=y; nxt[tot]=fir[x]; Fir[x]=tot} int main () {scanf ("%d%d%d",&n,&m,&
HP);
if (hp==0) return printf ("0.00000000"), 0;
for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
for (int i=1;i<=m;i++) {int x,y; scanf ("%d%d", &x,&y); Add (x,y); d[x]++;
if (x!=y) Add (y,x), d[y]++;
for (int i=1;i<=n;i++) {k[i][i]+=1; if (a[i)) continue;
if (i==1) _k[1].a[0]-=1;
for (int j=fir[i];j;j=nxt[j]) if (son[j]!=n) k[i][son[j]]-=1.0/d[son[j]];
Gs ();
for (int i=1;i<=n;i++) f[hp][i]=ans[i].a[0];
Ans=f[hp][n]; memset (k,0,sizeof (K)); memset (Ans) (ans,0,sizeof);
memset (_k,0,sizeof (_k)); for (int i=1;i< =n;i++) {k[i][i]+=1;
if (A[i]) {_k[i].a[i]-=1; continue}
for (int j=fir[i];j;j=nxt[j]) if (son[j]!=n) k[i][son[j]]-=1.0/d[son[j]];
Gs ();
for (int i=hp-1;i>=1;i--) {for (int j=1;j<=n;j++) if (A[j]) {if (I+A[J]>HP) continue;
for (int k=fir[j];k;k=nxt[k]) if (son[k]!=n) f[i][j]+=f[i+a[j]][son[k]]/d[son[k]];
for (int j=1;j<=n;j++) if (!a[j]) {for (int k=1;k<=n;k++) f[i][j]+=ans[j].a[k]*f[i][k];
} Ans+=f[i][n];
printf ("%.8lf\n", ans);
for (int i=hp;i>=1;i--) {//for (int j=1;j<=n;j++) printf ("%.3lf", f[i][j));
printf ("\ n");
return 0; }