HDU oj 1055(Color a Tree 贪心好题)

Color a Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1872    Accepted Submission(s): 654


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

http://acm.hdu.edu.cn/showproblem.php?pid=1055

题意:给定一个拓扑图(n个结点n-1条边 ),每个节点有一定的权重,按照拓扑顺序得到一个全序,使得到的全序的代价最小。结点代价的计算为其在全序中的顺序乘以其本身的权重。

策略: 贪心。

选取当前权重最大的一个点,将其和其父节点合并成一个点,并在其和父亲中连一条有向边确定其偏序(父亲指向儿子)。合并的同时更新拓扑图和权重。对于权重的更新,有以下的证明:
首先对于每一次的选取,都可以简化成一个结点和一条链或一个结点和一个结点的权重比较。设当前已选(t-1)次,即在选全序的t号元素时,


1,2,3点的权重分别为a, b, c, 先选取1的总代价为a*t+b*(t+1)+c*(t+2), 先选取2,3的代价为b*t+c*(t+1)+a*(t+2),若b*t+c*(t+1)+a*(t+2)a*t+b*(t+1)+c*(t+2), 则
  a < (b+c)/2;
类似的递推可以得到结论:任意一条链的权重可以等价为链上结点权重的算术平均值。那么贪心的策略就是先取出拓扑图中权重最大的链。
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;

const int maxn = 1010;

struct node{
    double weight;
    int id;
    node(){}
    node(double _w, int _id):weight(_w), id(_id){}
    bool operator < (const node &n) const{
        return weight < n.weight;
    }
};

double c[maxn], ww[maxn];
int cnt[maxn];
int fa[maxn], G[maxn][maxn];
int path[maxn], nex[maxn];

void init(){
    memset(fa, -1, sizeof fa);
    memset(path, -1, sizeof path);
    memset(G, 0, sizeof G);
    for(int i = 0; i < maxn; i++) nex[i] = i, cnt[i] = 1;
}

int getnext(int id){  
    int i;
    for(i = id; i != nex[i]; i = nex[id]);
    return i;
}

int main(){
    int n, r;
    while(~scanf("%d%d", &n, &r) &&(n||r)){
        init();
        priority_queue<node> q;
        for(int i = 0; i < n; i++){
            scanf("%lf", c+i+1);
            ww[i+1] = c[i+1];
            q.push(node(c[i+1], i+1));
        }
        for(int i = 1; i < n; i++){
            int u, v;
            scanf("%d%d", &u, &v);
            fa[v] = u;
            G[u][++G[u][0]] = v;  
        }
        int cc = 0, ans = 0;
        while(!q.empty()){
            node u = q.top(); q.pop();
            int id = u.id;
            double w = u.weight;
            int fau = fa[id];
            if(cnt[id] == 0) continue;
            if(fau != -1){
                c[fau] = double(c[fau]*cnt[fau] + c[id]*cnt[id])/(1.0*cnt[fau] + cnt[id]); //更新权重
                cnt[fau] = cnt[fau] + cnt[id];
                for(int i = 1; i <= G[id][0]; i++){ //更新图的结构
                    fa[G[id][i]] = fau;
                    G[fau][++G[fau][0]] = G[id][i];
                }
                q.push(node(c[fau], fau));
                path[nex[fau]] = id;          //记录最优的选择路径,nex[i]数组维护以i开头的路径的最后一个元素
                nex[fau] = getnext(id);
            }
            else {
                for(int i = 1; i <= G[id][0]; i++){
                    fa[G[id][i]] = -1;
                }
                for(int i = id; i != -1; i = path[i]){
                    ans += (++cc)*(int)(ww[i]+0.5);
                }
            }
            cnt[id] = 0;
        }
        printf("%d\n", ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值