Color a Tree
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 7144 Accepted: 2458
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
题意:
有一颗树,需要给他每个点都染色。节点标号从1-n,每个节点的权值在输入的第二行给出。 要求必须父节点已经染色,子节点才可以染色,需要按拓扑顺序染色。然后每个节点需要一个单位的染色时间。时间从1开始,计算费用时,每个节点的费用是当前时间*节点的权值。
第三行开始 接下来的输入是树的父亲和儿子的关系。
做法:
贪心,开始把每个点各自看成一个团。然后那个团的权值和 以及 这个团有多少个点 都记录在这个团的最顶上的祖先点上。 然后每次贪心 找 出( 权值和/点数 ) 最大的那个团。然后用链表的记录方式 跟新 这个团的祖先 与 其父亲的关系。并把这个团 与那个父亲所在的团合并。
最后从root开始,历遍这个链表,时间从1开始,计算出总的花费。
为什么这么贪心,因为如果点多的团先连,就代表先走点多的团,花费的时间会更多,后面的花费会增加。 所以优先选择团是和点的数量成反比和团的总权值成正比的把。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream> #include <algorithm> using namespace std; #include <stack> #include <queue> #include <vector> #include <deque> #include <set> #include <map> int w[1010];//团的权值 int root,num[1010];//团的点数 int n,c[1010]; //点的权值 int nex[1010],pre[1010]; //链表的记录 int fa[1010],vis[1010]; //父节点 和 有无访问过 int find() { double maxx=-1; int id=-1; for(int i=1;i<=n;i++) { if(i!=root&&(1.0*w[i]/num[i])>maxx&&!vis[i]) { maxx=(1.0*w[i]/num[i]); id=i; } } vis[id]=1; return id; } void unit(int p) { int i; for(i=fa[p];~pre[i];i=pre[i]){} //找出父节点团的 祖先 也就是这个团中最先染色的点 w[i]+=w[p]; num[i]+=num[p]; for(i=fa[p];~nex[i];i=nex[i]){} //找出父节点团中 最后一个染色的点 pre[p]=i; nex[i]=p; } int main () { while(scanf("%d%d",&n,&root),n||root) { for(int i=1;i<=n;i++) { scanf("%d",&w[i]); c[i]=w[i]; num[i]=1; pre[i]=nex[i]=-1; vis[i]=0; } int u,v; for(int i=1;i<n;i++) { scanf("%d%d",&u,&v); fa[v]=u; } while(1) { int tem=find(); if(tem==-1) break; unit(tem); } int time=1; int ans=0; for(int i=root;~i;i=nex[i]) { ans+=time*c[i]; time++; } printf("%d\n",ans); } return 0; } /* 5 1 1 2 1 2 4 1 2 1 3 2 4 3 5 0 0 */