SDOI2014 reconstruction

Topic links: poke me

Arguments matrix tree theorem, specifically to see konjac study notes in [the tree issue] this one finishing QAQ

Original formula is \ (\ sum_T \ prod _ { (u, v) \ in T} p_ {u, v} \ prod _ {(u, v) \ notin T} (1-p_ {u, v}) \)

\(=\prod_{(u,v)\in G}(1-p_{u,v})\sum_T\prod_{(u,v)\in T}\frac{p_{u,v}}{1-p_{u,v}}\)

So the argument directly to the matrix tree theorem of it.

But beware of us involved in the division operation, so remember to set the probability of 1 is set to be 1-eps, eps into probability 0

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 110
#define eps 1e-8
using namespace std;
int n,p;
double all=1.0;
double a[MAXN][MAXN];
inline double solve()
{
    double cur_ans=1.0;
    for(int i=2;i<=n;i++)
    {
        int pos=i;
        for(int j=i+1;j<=n;j++)
            if(fabs(a[j][i])>fabs(a[pos][i]))
            pos=j;
        if(pos!=i) swap(a[i],a[pos]),cur_ans*=-1.0;
        for(int j=i+1;j<=n;j++)
        {
            double t=a[j][i]/a[i][i];
            for(int k=i;k<=n;k++) a[j][k]-=t*a[i][k];
        }
    }
    for(int i=2;i<=n;i++)
    cur_ans*=a[i][i];
    return cur_ans;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("ce.in","r",stdin);
    #endif
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%lf",&a[i][j]);
            if(a[i][j]<eps) a[i][j]=eps;
            if(a[i][j]>1.0-eps) a[i][j]=1.0-eps;
            if(i<j) all*=(1.0-a[i][j]);
            a[i][j]=a[i][j]/(1.0-a[i][j]);
        }
    }
    /*
    for(int i=1;i<=n;i++)
    {
    for(int j=1;j<=n;j++)
        printf("%.3lf ",a[i][j]);
    printf("\n");
    }
    */
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(i!=j)
                a[i][i]-=a[i][j];
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
        a[i][j]=-a[i][j];
    //rintf("all=%.3lf\n",all);
    printf("%.6lf\n",     all*solve( )); 
    return 0;
}

Guess you like

Origin www.cnblogs.com/fengxunling/p/10947443.html