1. 程式人生 > >[USACO11MAR]樹裝飾Tree Decoration

[USACO11MAR]樹裝飾Tree Decoration

題目描述

Farmer John is decorating his Spring Equinox Tree (like a Christmas tree but popular about three months later). It can be modeled as a rooted mathematical tree with N (1 <= N <= 100,000) elements, labeled 1...N, with element 1 as the root of the tree. Each tree element e > 1 has a parent, P_e (1 <= P_e <= N). Element 1 has no parent (denoted '-1' in the input), of course, because it is the root of the tree.

Each element i has a corresponding subtree (potentially of size 1) rooted there. FJ would like to make sure that the subtree corresponding to element i has a total of at least C_i (0 <= C_i <= 10,000,000) ornaments scattered among its members. He would also like to minimize the total amount of time it takes him to place all the ornaments (it takes time K*T_i to place K ornaments at element i (1 <= T_i <= 100)).

Help FJ determine the minimum amount of time it takes to place ornaments that satisfy the constraints. Note that this answer might not fit into a 32-bit integer, but it will fit into a signed 64-bit integer.

For example, consider the tree below where nodes located higher on the display are parents of connected lower nodes (1 is the root):

For example, consider the tree below where nodes located higher on
the display are parents of connected lower nodes (1 is the root):

               1 
               |
               2
               |
               5
              / \
             4   3

Suppose that FJ has the following subtree constraints:

                  Minimum ornaments the subtree requires
                    |     Time to install an ornament
       Subtree      |       |
        root   |   C_i  |  T_i
       --------+--------+-------
          1    |    9   |   3
          2    |    2   |   2
          3    |    3   |   2
          4    |    1   |   4
          5    |    3   |   3

Then FJ can place all the ornaments as shown below, for a total
cost of 20:

            1 [0/9(0)]     legend: element# [ornaments here/
            |                      total ornaments in subtree(node install time)]
            2 [3/9(6)]
            |
            5 [0/6(0)]
           / \
 [1/1(4)] 4   3 [5/5(10)]

輸入輸出格式

輸入格式:

 

* Line 1: A single integer: N

* Lines 2..N+1: Line i+1 contains three space-separated integers: P_i, C_i, and T_i

 

輸出格式:

 

* Line 1: A single integer: The minimum time to place all the

ornaments

 

輸入輸出樣例

輸入樣例#1: 

5 
-1 9 3 
1 2 2 
5 3 2 
5 1 4 
2 3 3 

輸出樣例#1: 

20 

 

翻譯

約翰正在裝飾他家的聖誕樹。聖誕樹上有個 N 個結點,第一個結點是根,其餘結點都有唯一的

父親結點,第 i 個結點的父親是 P i 。由於根沒有父親,所以記 P 1 = −1。

約翰可以在每個結點上掛載裝飾物,但費用是變化。在第 i 個結點上掛載一個裝飾物需要花費 C i

元錢。奶牛對這個聖誕樹上每個結點都有特殊的裝飾需求,對於第 i 個結點,奶牛要求以它為根的子

樹上必須有 D i 個裝飾物。請問約翰在哪些結點上掛載裝飾物,才能滿足奶牛的要求,並且使得裝飾

費用最少?

輸入 • 第一行:單個整數 N,1 ≤ N ≤ 10 5

• 第二行到 N 行:第 i+1 行有三個整數:P i ,D i 和 C i ,1 ≤ P i ≤ N, 0 ≤ D i ≤ 10 6 , 1 ≤ C i ≤ 100

輸出 • 單個整數,表示完成所有裝飾要求的最少費用

樣例輸入

5 -1 9 3 1 2 2 5 3 2 5 1 4 2 3 3 樣例輸出

20 提示 在第四個結點上放一個,花 4 元;在第三個

結點上放五個,花 10 元;在第二個結點上放三

個,花 6 元;

 

 

題解:

記得開long long

註釋在程式碼裡面

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long LL;
const int N=1e5+10;
struct edge{//編目錄 
	int v,nxt;
}e[N];int cnt,last[N];
inline void add(int u,int v){//建邊 
	e[++cnt]=(edge){v,last[u]};
	last[u]=cnt;
}
struct trnode{//樹形結構 
	LL cost,minn,s;//cost表示滿足當前節點的要求所需的最小花費,minn表示i的子樹中單價最小的節點,s為以i為根的所以節點的裝飾總數, 
}tr[N];
LL d[N],c[N];
int n;
void dfs(int u){
	tr[u].minn=c[u];tr[u].s=0; 
	for(int i=last[u],v=e[i].v;i;i=e[i].nxt,v=e[i].v){//搜尋所有的兒子節點,求出cost,minn和s 
		dfs(v);
		tr[u].minn=min(tr[u].minn,tr[v].minn);
		tr[u].s+=tr[v].s;
		tr[u].cost+=tr[v].cost;
	}
	LL tt=d[u]-tr[u].s;//表示還差的裝飾數 
	if(tt<0)tt=0;//tt>=0 
	else tr[u].s=d[u];//不要忘記改變tr[u].s 
	tr[u].cost+=LL(tt*tr[u].minn);//增加當前節點的花費 
}
int main(){
	scanf("%lld",&n);int tt;
	for(int i=1;i<=n;i++){
		scanf("%d%lld%lld",&tt,&d[i],&c[i]);
		if(tt==-1)continue;
		add(tt,i);//建邊 
	}
	dfs(1);//搜尋根節點 
	printf("%lld\n",tr[1].cost);//輸出 
	return 0;
}

這一道題是真的很水