贪心。。。。

                   Color a Tree
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 6647   Accepted: 2249

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

Beijing 2004

贪心原则应该是Ci大的尽量先染色,但是由于父节点染了才能染子节点的限制使得问题不好解决了,但是Ci大的一定是在其父节点染色后立即被染色,这时大牛们的思路我也没有看明白如何证明的,但仔细一想就明白了。于是我们根据这个条件就可以将Ci大的点与其父节点合并在一起组成一个集合。这样就可以将问题规模减小。

合并后的点(即集合)的属性如何变化呢?假如设fact[i]表示集合的Ci和,iNum[i]表示i所属集合的结点个数;那么把fact[i]/iNum[i]作为贪心原则,其值大者先合并到其父节点,最终合并成一个集合。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4
 5 using namespace std;
 6
 7 struct Edge
 8 {
 9     int to,next;
10 }e[1111];
11
12 int n,root,Size,Adj[1111],c[1111],num[1111],father[1111];
13 bool vis[1111];
14
15 void Init()
16 {
17     Size=0;
18     memset(Adj,-1,sizeof(Adj));
19     memset(vis,false,sizeof(vis));
20 }
21
22 void Add_Edge(int u,int v)
23 {
24     ///u-->v
25     e[Size].to=v;
26     e[Size].next=Adj[u];
27     Adj[u]=Size++;
28 }
29
30 int Find()
31 {
32     int k=-1;
33     double maxn=-0x3f3f3f3f;
34     for(int i=1;i<=n;i++)
35     {
36         if(!vis[i]&&i!=root&&maxn<(double)c[i]/num[i])
37         {
38             maxn=(double)c[i]/num[i];
39             k=i;
40         }
41     }
42     return k;
43 }
44
45 void Union(int a,int b)
46 {
47     /// a to b
48     num[b]+=num[a];
49     c[b]+=c[a];
50     father[a]=b;
51     for(int i=Adj[a];~i;i=e[i].next)
52     {
53         int v=e[i].to;
54         father[v]=b;
55     }
56 }
57
58 int solve()
59 {
60     int ans=0;
61     for(int i=0;i<n-1;i++)
62     {
63         int k=Find();
64         vis[k]=true;
65         int p=father[k];
66         while(vis[p]) p=father[p];
67         ans+=c[k]*num[p];
68         Union(k,p);
69     }
70     ans+=c[root];
71     return ans;
72 }
73
74 int main()
75 {
76     while(scanf("%d%d",&n,&root)!=EOF)
77     {
78         if(n==0&&root==0) break;
79         Init();
80         for(int i=1;i<=n;i++)
81         {
82             scanf("%d",c+i);
83             num[i]=1;
84         }
85         for(int i=0;i<n-1;i++)
86         {
87             int u,v;
88             scanf("%d%d",&u,&v);
89             Add_Edge(u,v);
90             father[v]=u;
91         }
92         printf("%d\n",solve());
93     }
94     return 0;
95 }

转载于:https://www.cnblogs.com/CKboss/p/3361809.html

POJ 2054 Color a Tree相关推荐

  1. POJ 2054 Color a Tree (贪心)

    $ POJ~2054~Color~a~Tree $ $ solution: $ 我们先从题中抽取信息,因为每个点的费用和染色的次数有关,所以我们可以很自然的想到先给权值大的节点染色.但是题目还说每个节 ...

  2. POJ 2054 Color a Tree解题报告

    题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...

  3. PKU/POJ 2054 Color a Tree

    关于树的着色. 要求从根节点出发, 遍历整棵树, 要求代价最小. 访问每个节点的代价V[i] = F[i] * C[i], C[i]为已知的值, F[i]则为访问该节点的时间. 每一步只能从已访问的节 ...

  4. HDU 6241 Color a Tree

    Color a Tree 题目大意:一棵树,根为1.某些点有一些限制.限制A: 该$x_i$点子树染色点至少$y_i$个. 限制B: 该$x_i$点子树外染色至少$y_i$个.求最少染色点数. 首先是 ...

  5. poj 3013 Big Christmas Tree(最短路变形)

    传送门:POJ 3013 Big Christmas Tree 描述: Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K To ...

  6. poj 2777Count Color

    这道题,做的太悲催了,开始想到每个节点的涂色情况用数组来存,1代表有,0代表没有,提交上去果断RE了好几次,原因是here A, B, C are integers, and A may be lar ...

  7. 【POJ - 2486】Apple Tree (树形背包,dp)

    题干: Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There ...

  8. 【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)

    题干: There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the t ...

  9. POJ 1046 Color Me Less(浅水)

    一.Description A color reduction is a mapping from a set of discrete colors to a smaller one. The sol ...

最新文章

  1. 【计算理论】下推自动机 PDA 及 计算示例
  2. 为什么刹车热了会失灵_汽车为什么要换刹车油?
  3. django之jquery完成ajax
  4. 这是最好的时代,也是最坏的时代
  5. 使用transform和transition制作CSS3动画
  6. Lombok(1.14.8) - @Log
  7. 2019年7月前CSDN最新排名
  8. for语句 2017-03-17
  9. 我只是bug的搬运工之idea的Cannot run program git.exe: CreateProcess error=2
  10. Numpy系列(一)array对象以及创建array的方法总结
  11. 极限学习机Python的代码实现
  12. 求解带时间窗的车辆路径问题(matlab实现)
  13. Qt中配置OpenCV
  14. repo+manifests+git方式管理安卓代码
  15. android基础 [超级详细android Activity组件解析(Activity综述,Activity生命周期,Activity启动--携带参数启动)]
  16. 三言|格局决定结局 态度决定高度
  17. python3实现百度翻译
  18. office2007安装找不到文件问题
  19. XSS是什么?如何防御?手摸手教你Springboot配置XSS防御,深入代码解析!
  20. 软件工程导论——需求分析总结

热门文章

  1. java中什么是 伪共享_【Java】聊聊多线程中的伪共享现象
  2. mysql多数据源切换_CI 多数据库操作 切换数据库
  3. android 菜单 功能键,Android交互体验必知:功能按键事件
  4. v8声卡怎么录制唱歌_V8声卡坑爹?想买的看完再决定,买了的看如何调试声卡...
  5. 前端性能优化篇——浏览器同域名并发请求对限制
  6. java爬取单张图片
  7. 数据增强_imgaug图像数据增强必备工具
  8. php中reset函数,PHP reset()函数
  9. SPSS操作(四):系统聚类分析
  10. mysql 如果存在修改_mysql如存在并发修改可能,一定要注意保证数据一致性