POJ 2054 Color a Tree
贪心。。。。
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 6647 | Accepted: 2249 |
Description
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
A test case of N = 0 and R = 0 indicates the end of input, and should not be processed.
Output
Sample Input
5 1
1 2 1 2 4
1 2
1 3
2 4
3 5
0 0
Sample Output
33
Source
贪心原则应该是Ci大的尽量先染色,但是由于父节点染了才能染子节点的限制使得问题不好解决了,但是Ci大的一定是在其父节点染色后立即被染色,这时大牛们的思路我也没有看明白如何证明的,但仔细一想就明白了。于是我们根据这个条件就可以将Ci大的点与其父节点合并在一起组成一个集合。这样就可以将问题规模减小。
合并后的点(即集合)的属性如何变化呢?假如设fact[i]表示集合的Ci和,iNum[i]表示i所属集合的结点个数;那么把fact[i]/iNum[i]作为贪心原则,其值大者先合并到其父节点,最终合并成一个集合。
#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; struct Edge
{
int to,next;
}e[]; int n,root,Size,Adj[],c[],num[],father[];
bool vis[]; void Init()
{
Size=;
memset(Adj,-,sizeof(Adj));
memset(vis,false,sizeof(vis));
} void Add_Edge(int u,int v)
{
///u-->v
e[Size].to=v;
e[Size].next=Adj[u];
Adj[u]=Size++;
} int Find()
{
int k=-;
double maxn=-0x3f3f3f3f;
for(int i=;i<=n;i++)
{
if(!vis[i]&&i!=root&&maxn<(double)c[i]/num[i])
{
maxn=(double)c[i]/num[i];
k=i;
}
}
return k;
} void Union(int a,int b)
{
/// a to b
num[b]+=num[a];
c[b]+=c[a];
father[a]=b;
for(int i=Adj[a];~i;i=e[i].next)
{
int v=e[i].to;
father[v]=b;
}
} int solve()
{
int ans=;
for(int i=;i<n-;i++)
{
int k=Find();
vis[k]=true;
int p=father[k];
while(vis[p]) p=father[p];
ans+=c[k]*num[p];
Union(k,p);
}
ans+=c[root];
return ans;
} int main()
{
while(scanf("%d%d",&n,&root)!=EOF)
{
if(n==&&root==) break;
Init();
for(int i=;i<=n;i++)
{
scanf("%d",c+i);
num[i]=;
}
for(int i=;i<n-;i++)
{
int u,v;
scanf("%d%d",&u,&v);
Add_Edge(u,v);
father[v]=u;
}
printf("%d\n",solve());
}
return ;
}
POJ 2054 Color a Tree的更多相关文章
- 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 ...
- poj 2054 Color a Tree(贪婪)
# include <stdio.h> # include <algorithm> # include <string.h> using namespace std ...
- POJ 2054 Color a Tree#贪心(难,好题)
题目链接 代码借鉴此博:http://www.cnblogs.com/vongang/archive/2011/08/19/2146070.html 其中关于max{c[fa]/t[fa]}贪心原则, ...
- POJ 2054 Color a Tree (贪心)
$ POJ~2054~Color~a~Tree $ $ solution: $ 我们先从题中抽取信息,因为每个点的费用和染色的次数有关,所以我们可以很自然的想到先给权值大的节点染色.但是题目还说每个节 ...
- Color a Tree[HDU1055]
Color a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- POJ 3013 Big Christmas Tree(最短Dijkstra+优先级队列优化,SPFA)
POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意: 圣诞树是由n个节点和e个边构成的,点编号1-n. ...
- poj2054 Color a Tree
神题.这题是巨毒瘤... 自己写真可谓是: 排空驭气奔如电,上天入地求之遍 上穷碧落下黄泉,两处茫茫皆不见 由于我们知道:不是树形时,不停选值最大的节点可以得到最小代价. 那么我们就能想出一个错误的贪 ...
- Color a Tree HDU - 6241
/* 十分巧妙的二分 题意选最少的点涂色 使得满足输入信息: 1 x的子树涂色数不少于y 2 x的子树外面涂色数不少于y 我们若是把2转化到子树内最多涂色多少 就可以维护这个最小和最大 如果我们二分出 ...
- 【POJ 2486】 Apple Tree(树型dp)
[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8981 Acce ...
随机推荐
- C#调试方法
单步执行 有三种, 一种是每次执行一行(F10): 一种是每次执行一行,但遇到函数调用就会跳到被调用的函数里(F11): 一种是直接执行当前函数里剩下的指令,返回上一级函数(Shift+F11). 还 ...
- jpa findOne()用法
findOne(Interger id) 如果实体类没有id这个属性的话是会报错的 改别人的代码神烦...
- ( 译、持续更新 ) JavaScript 上分小技巧(三)
最近家里杂事较多,自学时间实在少的可怜,所以都在空闲时间看看老外写的内容,学习之外顺便翻译分享~等学习的时间充足些再写写自己的一些学习内容和知识点分析(最近有在接触的:复习(C#,SQL).(学习)T ...
- AngularJs $interval 和 $timeout
$interval window.setInterval的Angular包装形式.Fn是每次延迟时间后被执行的函数. 间隔函数的返回值是一个承诺.这个承诺将在每个间隔刻度被通知,并且到达规定迭代次数后 ...
- Instant Python 中文缩减版
前言 本文主要来自<Python基础教程(第2版)>([挪]Magnus Lie Hetland著,司维 曾军崴 谭颖华译 人民邮电出版社) 中的“附录A 简明版本”,对于其中的有问题之处 ...
- mongodb嵌套查询
db.dbModel.find({'Missions.Rewards.PrizeType':21} )
- sed,grep,awk命令常用法
查看当天nginx访问日志中2016:03:25到2016:05点passport.mingxiao.com域名访问量最多的url,可以查看网站是否被刷. 法一: sed -n '/2016:03:2 ...
- JAVA第三周课后作业
JAVA课后作业 一.枚举类型 代码: enum Size{SMALL,MEDIUM,LARGE}; public cl ass EnumTest { public static void main( ...
- Deep Learning in a Nutshell: History and Training
Deep Learning in a Nutshell: History and Training This series of blog posts aims to provide an intui ...
- 获取ip的ip138.com
代码: function get_onlineip() { $ch = curl_init('http://iframe.ip138.com/ic.asp'); curl_setopt($ch, CU ...