$ Poj2054 \ Color \ a \ Tree \ $ greedy

$ $ Concepts

 

$Description$

$ $ N-tree has nodes, these nodes are numbered: $ 1,2,3 ... n, $ I $ $ each node has a weight value of $ A [i] $.

Now all dyeing, dyeing rules make this tree node is:

Root node R can always be stained; for other nodes before being dyed its parent node must have been infected with the color.

Every time the cost of dyeing is $ T * A [i] $, where $ T $ represents the current many times stained.

Seeking a minimum total cost of this tree staining.

 

$Sol$

 Greedy: the right tree in the maximum points, will stain immediately after its parent Point Dyer knot

May think so, assuming no constraint tree, but a sequence, it is clearly the optimal staining sorted in descending order, there are trees constraints, should make maximum weight of dyeing early as possible.

Because the point of maximum weights and staining its parent node is successively carried out, so we can merge them together.

Now have a value of $ x, y, z $ three points, and the known $ X $ $ $ Y staining was performed with successively, wherein Y $ $ $ X $ is the parent node. Then there are two kinds of decisions.

1. dyed $ x, y $, then transfected $ z $, $ x + 2y + 3z = y + (x + y) + 3z $

2. dyed $ z $, re-dyed $ x, y, z + 2x + 3y = z + y + 2 * (x + y) $

Found that both the above cases there is a practice which is common: $ Y $ accumulated into the first answer, then the node with $ Y $ $ X $ merge node, specifically, the right to change the value of $ X $ to $ (x + y) / 2 $, $ y $ delete nodes. Why is the right to change the value of $ (x + y) / 2 $ it?

There is a very important issue is how to judge the merits of these two approaches. The consolidation of the node is $ i $, this node contains the number of nodes (and its own merger go) is $ s [i ] $, ​​all weights involved and to $ a [i] $.

1.$a[i]+(s[i]+1)*z.$

2.$z+a[i]*2.$

The coefficient was found to be $ 1 $ formula becomes $ z $ $ $ 2, the two equations simultaneously adding $ (s [i] -1) * z $, and then with the addition $ s [i] $, becomes to make

1.$a[i]/s[i]+2*z$

2.$z+a[i]/s[i]$

It can be regarded as a weight of $ a [i] / s [i] $ a $ z $ nodes and two nodes, according to the beginning of said known greedy comparing their size can decide who give dyed , then the problem is solved.

I want to emphasize that the above given new weight value can only be used to compare with other nodes to determine the size of the order of dyeing, as to accumulate the answer, just two formulas for most of the initial analysis, "the $ y $ accumulated into the answer "this sentence. In particular node $ J $ $ I $ accumulated into the junction, $ ans + = a [j] * s [i] $.

 

$Code$

#include<iostream>
#include<cstdio>
#define il inline
#define Rg register
#define go(i,a,b) for(Rg int i=a;i<=b;i++)
#define yes(i,a,b) for(Rg int i=a;i>=b;i++)
#define db double
#define ll long long
using namespace std;
il int read()
{
    int x=0,y=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    return x*y;
}
int n,rt,a[1001],fa[1001],s[1001];
ll as;
int main()
{
    //while(1)
    {
        n=read(),rt=read();as=0;
        if(!n && !rt)return 0;
        go(i,1,n)a[i]=read(),s[i]=1;
        go(i,1,n-1){int x=read(),y=read();fa[y]=x;}
        go(T,1,n-1)
        {
            db maxs=0;int pos;
            go(i,1,n)
                if(i!=rt && 1.0*a[i]/s[i]>=maxs)maxs=1.0*a[i]/s[i],pos=i;
            go(i,1,n)
                if(fa[i]==pos)fa[i]=fa[pos];
            as+=a[pos]*s[fa[pos]];
            s[fa[pos]]+=s[pos];
            a[fa[pos]]+=a[pos];
            a[pos]=0;
        }
        as+=a[rt];
        printf("%lld\n",as);
    }
    return 0;
}
View Code

 

 

 

Guess you like

Origin www.cnblogs.com/forward777/p/11242305.html