Problem Description
Bob is very interested in the data structure of a tree. A tree is a directed graph in which a special node is singled out, called the “root” of the tree, and there is a unique path from the root to each of the other nodes.
Bob intends to color all the nodes of a tree with a pen. A tree has N nodes, these nodes are numbered 1, 2, …, N. Suppose coloring a node takes 1 unit of time, and after finishing coloring one node, he is allowed to color another. Additionally, he is allowed to color a node only when its father node has been colored. Obviously, Bob is only allowed to color the root in the first try.
Each node has a “coloring cost factor”, Ci. The coloring cost of each node depends both on Ci and the time at which Bob finishes the coloring of this node. At the beginning, the time is set to 0. If the finishing time of coloring node i is Fi, then the coloring cost of node i is Ci * Fi.
For example, a tree with five nodes is shown in Figure-1. The coloring cost factors of each node are 1, 2, 1, 2 and 4. Bob can color the tree in the order 1, 3, 5, 2, 4, with the minimum total coloring cost of 33.
Given a tree and the coloring cost factor of each node, please help Bob to find the minimum possible total coloring cost for coloring all the nodes.
Input
The input consists of several test cases. The first line of each case contains two integers N and R (1 <= N <= 1000, 1 <= R <= N), where N is the number of nodes in the tree and R is the node number of the root node. The second line contains N integers, the i-th of which is Ci (1 <= Ci <= 500), the coloring cost factor of node i. Each of the next N-1 lines contains two space-separated node numbers V1 and V2, which are the endpoints of an edge in the tree, denoting that V1 is the father node of V2. No edge will be listed twice, and all edges will be listed.
A test case of N = 0 and R = 0 indicates the end of input, and should not be processed.
Output
For each test case, output a line containing the minimum total coloring cost required for Bob to color all the nodes.
Sample Input
5 1
1 2 1 2 4
1 2
1 3
2 4
3 5
0 0
Sample Output
33
Source
Asia 2004, Beijing (Mainland China)
题目大意:给一颗树,要求选出所有点,选一个点的代价是该点点权乘该点被选的次序,第一个被选的点是根节点,选其余点必须在其父亲节点之后选。最后使总代价最小。
分析:
先提出另外一种等价的计算代价的方式:选一个点的代价等于这个点及剩下的点的点权之和。
如果没有树形的依赖关系,可以任意选点的话,那么很明显我们应该按照点权从大到小选出点来得到最优解。然而对于这道题,我们不妨先考虑:如果树上所有点中点权最大的点现在是可以选的,那么显然选它一定不会比选别的点差。因此我们有了一个贪心策略:设树上点权最大的点为u,当u的父亲fa被选后,我们应该立刻选择u。
当然这并不能解决所有的问题,因为最大点权的点不是立刻能选的。对于上面的贪心策略,我们知道了选fa后一定会立刻选u,那么我们不妨将fa和u“打包处理”——将fa和u的点集看做一个新的节点,如果之后选了这个节点便等价于选择了fa再选择u。
现在着重分析下如何定义点集的权值。
设有两个点集A,B,A中点权之和为sa,点数为ca,B则为sb和cb。当A,B都可选时,简单起见,我们设先选A再选B的代价为FA,先选B再选A的代价为FB,则FA-FB=sb*ca-sa*cb(先选A,会让B中所有点“等待”ca次选点,先选B则亦然,如果从前文所述另一种计算代价的方式分析较容易理解)。若要选择A更优,则有FA < FB即sb*ca < sa*cb,便得到了sb/cb < sa/ca,即A中点权的算术平均数要较大。那么我们的贪心策略便可以变为:树上所有点集中点权算术平均数最大的点集A的父亲F被选择后, 应该立刻选择A(单一的节点可看做只有一个点的点集)。
这样便得到了本题的算法:不断地寻找当前点权算术平均数最大的点集,并将其与其父亲合并,并记录决策,最终会的到一个确定的顺序并按照合并的顺序计算答案。由于只会进行合并n-1次,每次找寻点集的复杂度为O(n),总时间复杂度为O(n²)(当然,找点集可以用堆,那么总时间复杂度为O(nlogn),然而我懒得写了……)。
详情见代码。
#include<iostream>
#include<cstdio>
#include<cstring>
#define clr(a,b) memset(a,b,sizeof(a))
#define maxn 1000
using namespace std;
int da[maxn+10],fa[maxn+10];
int suf[maxn+10],ta[maxn+10],s[maxn+10],c[maxn+10];
/*suf[i]表示选i之后选的点;ta[i]表示第一个选的点为i的点集中最后一个被选的点,s[i]为点权之和,c[i]为点的个数,i含义同前。*/
int main(){
for(;;){
int n,ro;
scanf("%d%d",&n,&ro);
if(!n)break;
for(int i=1;i<=n;i++){
c[i]=1;
ta[i]=i;
scanf("%d",&da[i]);
s[i]=da[i];
}
for(int i=1;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
fa[b]=a;
}
clr(suf,0);
for(int i=1;i<n;i++){
double m=0;
int k;
//找点权算术平均数最大的点集,注意要转成double
for(int j=1;j<=n;j++)if(j!=ro){
if((double)s[j]/c[j]>m){
m=(double)s[j]/c[j];
k=j;
}
}
//合并
s[fa[k]]+=s[k];
c[fa[k]]+=c[k];
//合并之后以后便不再考虑k这个点,因此清零防止重复选择
s[k]=0;
//k的儿子也要处理
for(int j=1;j<=n;j++)if(fa[j]==k)fa[j]=fa[k];
suf[ta[fa[k]]]=k;
ta[fa[k]]=ta[k];
}
int ans=0;
for(int i=ro,cnt=1;i;cnt++,i=suf[i])ans+=cnt*da[i];
printf("%d\n",ans);
}
return 0;
}