poj 2054 Color a Tree 据说是贪心

Color a Tree

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 7112   Accepted: 2438

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
	做完这道题之后,我是不得不佩服以为神牛,地址链接  ,在做这道题之前,,先把这篇博客看看,这里面证明贪心的可行性和怎么贪心。哎,,渣渣完全是被虐啊!!
	我得先平息一下自己心情,你们还是看这篇解析吧,点击打开链接,,说的不错!
	贪心始终是我内心无法言明的痛
下面是我的代码:
#include <stdio.h>
#define MAX 1010
struct Tree{
	int color,father,time;
	double weight;
}t[MAX];

int root;

int findMax(int n)
{
	double max = -1.0 ;
	int pos=0 ;
	for(int i = 1 ; i <= n ; ++i)
	{
		if(t[i].weight>max && i != root)
		{
			max = t[i].weight;
			pos = i;
		}
	}
	return pos ;
}

int main()
{
	int n;
	while(~scanf("%d%d",&n,&root) && (n||root))
	{
		int ans = 0 ;
		for(int i = 1 ; i <= n ; ++i)
		{
			scanf("%d",&t[i].color) ;
			t[i].time = 1 ;
			t[i].weight = t[i].color ;
			ans += t[i].color ;
		}
		for(int i = 1 ; i < n ; ++i)
		{
			int father,son;
			scanf("%d%d",&father,&son);
			t[son].father = father ;
		}
		for(int i = 1 ; i < n ; ++i)
		{
			int max = findMax(n) ;
			t[max].weight = 0.0;
			int f = t[max].father ;
			ans += t[f].time*t[max].color ;
			for(int j = 1 ; j <= n ; ++j)
			{
				if(t[j].father == max)
				{
					t[j].father = f ;
				}
			}
			t[f].color += t[max].color ;
			t[f].time += t[max].time ;
			t[f].weight = 1.0*t[f].color/t[f].time ;
		}
		printf("%d\n",ans) ;
	}
	return 0;
}

时间: 2024-10-19 05:19:20

poj 2054 Color a Tree 据说是贪心的相关文章

hdu 1055 &amp; poj 2054 Color a Tree 树&amp;贪心 找最大费用点和父节点合并

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"

POJ 2054 Color a Tree#贪心(难,好题)

题目链接 代码借鉴此博:http://www.cnblogs.com/vongang/archive/2011/08/19/2146070.html 其中关于max{c[fa]/t[fa]}贪心原则,此博有很好的解释:http://www.cnblogs.com/rainydays/p/3271277.html 在此引用其中几段话: 试想,如果没有父节点排在节点之前的限制,那么这个题目非常简单,只需要将结点按照权值从大到小排列即可.加上了这个限制之后,如果权值最大的那个节点一旦满足了条件(父节点

poj 2054 Color a Tree(贪心)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int father[1010]; int next[1010];//当前集合的下个元素(包括i) int pre[1010];//当前集合的上个元素(包括i) int num[1010];//num[i]当前集合储存的点的个数(包括i) int vis[1010]; int sum[1010];//当前

poj 2054 Color a Tree

很有意思的一道贪心题,自己没想出来,理解了一下别人的思路 参考:http://www.cnblogs.com/yu-chao/archive/2012/02/19/2358565.html http://my.oschina.net/locusxt/blog/210536 题目大意:给你一颗树,树上每个节点都有自己的权值,现在要把这棵树上的所有节点染上颜色,每染一个节点需要一个单位的时间,染一个点的花费是该点的权值乘以当前时间(时间从1开始).规定在染当前点之前,必须先染他的父亲节点,求最小的花

poj 2054 Color a Tree(贪婪)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int father[1010]; int next[1010];//当前集合的下个元素(包含i) int pre[1010];//当前集合的上个元素(包含i) int num[1010];//num[i]当前集合储存的点的个数(包含i) int vis[1010]; int sum[1010];//当前

HDU 1055 Color a Tree

题目:Color a Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=1055 题意:给一棵树,要给树上每一个结点染色,第i 个结点染色需要代价为:t * w[i] (t 表示i 结点是第几个染色的),还有一个前提是:要给i 结点染色,必须先给i 结点的父结点染色(根结点随便染). 思路: 贪心. 假定当前未染色的结点中权值最大的是A结点,如果A结点上方都染完色,那么现在一定是对A结点进行染色. 理解如下: 假设在上述情况下先对其他结点染色,在

hdu 4603 Color the Tree 2013多校1-4

这道题细节真的很多 首先可以想到a和b的最优策略一定是沿着a和b在树上的链走,走到某个点停止,然后再依次占领和这个点邻接的边 所以,解决这道题的步骤如下: 预处理阶段: step 1:取任意一个点为根节点,找出父子关系并且对这个树进行dp,求出从某个节点出发往下所包含的所有边的权值总和  复杂度O(n) step 2:从tree dp 的结果中计算对于某个节点,从某条边出发所包含的边的综合,并且对其从大到小进行排序 复杂度O(n*logn) step 3:dfs求出这颗树的欧拉回路,以及每个点的

Poj 1328(雷达安装)几何+贪心

[题目描述]: 给定n个小岛以及这些小岛的位置,并输入雷达的辐射面积,问最少需要多少个雷达站才能覆盖所有小岛? [思路分析]: 本题首先想到的是运用贪心算法,但是算法想到了如何贪心?这道题我自己开始做之时只有一点思路,就是让每一个雷达覆盖较多的点,但是如何较多覆盖,这就是典型的数学问题了,自己没有思索出来,最后在网上看了题解才明白如何做.下面我们看看如何建图: 我们通过这个图首先运用了一个数学知识,就是以小岛为圆心,雷达辐射范围为圆心建立一个圆,该圆与x轴有一个交点,以该交点作为雷达站铺设点那么

[ACM] POJ 2154 Color (Polya计数优化,欧拉函数)

Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7630   Accepted: 2507 Description Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of th