# vijos1144 (Xiao Fat Shou royal palace)

Source: Internet
Author: User

and ural1039 .
Description

After the Huyichen Prince incident, Xuzhenyi became the Emperor distinguished a product bodyguard.

The palace begins with the Meridian Gate, until the harem concubines's chambers, in the shape of a tree, and some palaces can be seen in each other. The fortified, three-step, five-step, each palace to be guarded around the clock, in different palaces to arrange the cost of the guards are different.

But Xuzhenyi's money is not enough, in any case can not be placed in every palace of the guards.

Help Xuzhenyi decorate the guards, under the premise of guarding all palaces, make the cost of the least money.

Format input Format

The data in the input file represents a tree, which is described as follows:

Line 1th N, which represents the number of nodes in the tree.

Line 2nd to No.`n+1n+1`Row, each row describes the node information for each palace, in turn: the Palace node label I (`0<i≤n0<i≤n`), the cost of placing a bodyguard in the Palace K, the number of the son of the point M, followed by the number of M, respectively, the node's M-son label`r1,R2,?,rm R1,R2,?, RM`

For an n (`0<n≤15000<n≤1500`Nodes are numbered between 1 and N, and the labels are not duplicated. The sum of the guaranteed funds does not exceed`231−1231−1`

Output format

The output file contains only one number, which is the minimum amount of money to be asked.

Example 1 sample input 1[copy]
`61 30 3 2 3 42 16 2 5 63 5 04 4 05 11 06 5 0`
Sample output 1[Copy]
`25`
Restrictions Tips

Select 3,4,2 Fee Minimum of 25

Source

Huyichen

Exercises

There is a tree, each point or the adjacent point must be stationed, the total cost of the cantonment is minimal, typical tree-shaped motion rules

Analysis

For node I, there are three states: 1, self-guarding 2, child guard 3, parent

Equation:

F[i,1]:=σ (min{f[son,1],f[son,2],f[son,3]}) +a[i]

f[i,2]:=σ (min{f[son,1],f[son,2]}) +m //m means that all son's self-guarding and the child of the difference between the minimum value, if greater than 0 to add this means that no son is self-observing, does not conform to the definition, it must be added m, counted as this son to keep

f[i,3]:=σ (f[son,2]) //son's self-guarding situation has been included in the above situation

The 2nd situation should pay special attention to the need for its sub-node must have a 1 condition, so make m=min{f[son[j],1]-f[son[j],2]}, if m>0 description in the decision-making time, its sub-node is not a 1 condition, it is necessary to add m, otherwise make m= 0.

This equation took a long afternoon to understand.

AC Code

`#include <iostream>#include<cstdio>#defineMAX 1000000000using namespacestd;introot,n,tot=0, Num,c,m,kk;intcost[1505];inthead[1505];int  from[1504];intto[1504];intf[1505][4];//1 Self-guarding, 2 child guards, 3 fathersvoidAddintAintb) {Tot++; To[tot]=b;  from[tot]=Head[a]; Head[a]=tot;}int_min (intAintb) {    returnA<b?a:b;}voidTREEDP (intx) {    if(head[x]==0)//leaf node{f[x][1]=f[x][2]=Cost[x]; return; }    intm,t; f[x][1]=Cost[x]; f[x][2]=f[x][3]=0; M=MAX;  while(head[x]>0) {T=To[head[x]];        TREEDP (t); f[x][1]=f[x][1]+_min (f[t][1],_min (f[t][2],f[t][3])); f[x][2]=f[x][2]+_min (f[t][2],f[t][1]); f[x][3]=f[x][3]+f[t][2]; if(m>f[t][1]-f[t][2]) m=f[t][1]-f[t][2]; HEAD[X]= from[Head[x]]; }    if(m>0) f[x][2]+=m;}intMain () {scanf ("%d",&N); Root=n* (n+1)/2;  for(intI=1; i<=n;i++) {scanf ("%d%d%d",&num,&c,&m); Cost[num]=C;  for(intj=1; j<=m;j++) {scanf ("%d",&KK); Root-=KK;        Add (NUM,KK);    }} TREEDP (root); cout<<_min (f[root][1],f[root][2]); return 0;}`
View Code

vijos1144 (Xiao Fat Shou royal palace)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.