#P3089. [USACO23FEB] Fertilizing Pastures G
[USACO23FEB] Fertilizing Pastures G
题目描述
There are pastures , connected by roads, such that they form a tree. Every road takes second to cross. Each pasture starts out with grass, and the ith pasture's grass grows at a rate of units per second. Farmer John is in pasture at the start, and needs to drive around and fertilize the grass in every pasture. If he visits a pasture with units of grass, it will need amount of fertilizer. A pasture only needs to be fertilized the first time it is visited, and fertilizing a pasture takes time.
The input contains an additional parameter .
- If , Farmer John must end at pasture .
- If , Farmer John may end at any pasture.
Compute the minimum amount of time it will take to fertilize every pasture and the minimum amount of fertilizer needed to finish in that amount of time.
输入格式
The first line contains and .
Then for each from to , there is a line containing and , meaning that there is a road connecting pastures and . It is guaranteed that .
输出格式
The minimum amount of time and the minimum amount of fertilizer, separated by spaces.
题目大意
有N个顶点的树,经过节点之间的每一条边都需要1s。每个顶点一开始的权值均为0,第i个点的权值的增长速率为a[i]/s。FJ从1号顶点出发遍历整棵树。当FJ走到某个节点时,若该节点的权值为x,则需要支出大小为x的费用。(当然,只需在第一次经过该节点时需要支出。)
给出一个参数T:
(i)若T=0,FJ必须回到1号节点。
(ii)若T=1,FJ可以在任意节点结束他的遍历。
求遍历所有节点的最小时间和此时需要付出的费用。
输入格式
第一行包括N和T。
第2行到第N行,包含两个整数p[i]和a[i],a[i]的含义见上文。p[i]则表示节点i和p[i]之间有一条边相连。
输出格式
两个整数:遍历所有节点的最小时间和此时需要付出的费用。
取值范围自行参考原文。
5 0
1 1
1 2
3 1
3 4
8 21
5 1
1 1
1 2
3 1
3 4
6 29
提示
Explanation for Sample 1
The optimal route for Farmer John is as follows:
- At time , move to node , which now has grass and so needs fertilizer.
- At time , move to node , which now has grass and so needs fertilizer.
- At time , move back to node , which we already fertilized and so don't need to fertilize again.
- At time , move to node , which now has grass and so needs fertilizer.
- At time , move back to node , which we already fertilized.
- At time , move back to node .
- At time , move to node , which now has grass and so needs fertilizer.
- At time , return to node .
This route takes time and uses fertilizer. It can be shown that is the least possible amount of time for any route that returns to node at the end and is the least possible fertilizer used for any route that returns to node and takes time.
Explanation for Sample 2
The optimal route for Farmer John is as follows:
- At time , move to node , which now has grass and so needs fertilizer.
- At time , move back to node .
- At time , move to node , which now has grass and so needs fertilizer.
- At time , move to node , which now has grass and so needs fertilizer.
- At time , move back to node , which we already fertilized and so don't need to fertilize again.
- At time , move to node , which now has grass and so needs fertilizer.
This route takes time and uses fertilizer. It can be shown that is the least possible amount of time for any route and is the least possible fertilizer used for any route that takes time.
SCORING
- Inputs :
- Inputs :
- Inputs and : No pasture is adjacent to more than three roads.