Poj 2054 贪心

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.

题意:给一个树染色,每个节点有权值Ci,每次染的消耗是Ci*Fi,Fi是染该节点的次序(前面染了Fi-1个),子节点必须后于父节点染色,求最小消耗和。

做法:贪心。
先不讨论子节点后于父节点的次序问题,显然大数在前。
现在要满足大数优先和树的层次顺序两者。
若一个节点有比他大的子节点,显然它的优先级提高了,评判每点顺序的KEY值为以它为开头的集合的平均值的最大值。每次找出该平均值X最大的节点,更新它父节点所在集合的根节点。用并查集维护集合关系。更新n次。
之后做一个类似层次遍历的遍历。用优先队列保证每次是X最大的节点出队即可。
WA了一万年,是因为我以为只要看当前节点父节点是否更新过即可,实际上要看父节点所在集合是否已更新完毕才行。或者不管如何,直接更新父节点根节点即可,因为更新完毕时父节点的根节点就是整个树的根节点了,随便更新都无所谓了。

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;

struct node {
    int id;
    int c;
    int t;
    int p;
    double c1;
    friend bool operator<(node a,node b)
    {
        return a.c1<b.c1;
    }
}rec[1050];

bool vis[1050];
int child[1050][1050];
int childc[1050];
int n,root;
int u[1050];

int findp(int x)
{
    if(x==u[x])return x;
    else return u[x]=findp(u[x]);
}

int find()
{
    int ans=-1;
    double MM=-1;
    for(int i=1;i<=n;i++)
    {
        if(!vis[i]&&MM<rec[i].c1){MM=rec[i].c1;ans=i;}
    }
    return ans;
}

int main()
{
    while(scanf("%d%d",&n,&root))
    {
        if(n==0&&root==0)break;
        for(int i=1;i<=n;i++)u[i]=i;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&rec[i].c);
            rec[i].c1=rec[i].c;
            rec[i].t=1;
            rec[i].id=i;
            rec[i].p=-1;
        }
        memset(child,-1,sizeof(child));
        memset(childc,0,sizeof(childc));
        for(int i=1;i<n;i++)
        {
            int f,c;
            scanf("%d%d",&f,&c);
            child[f][++childc[f]]=c;
            rec[c].p=f;
        }
        memset(vis,false,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            int M=find();
            vis[M]=true;
            int MP=rec[M].p;
            if(MP!=-1)
            {
                MP=findp(MP);
                u[M]=MP;
                rec[MP].c1=(rec[MP].c1*rec[MP].t+rec[M].c1*rec[M].t)/(rec[MP].t+rec[M].t);
                rec[MP].t+=rec[M].t;
            }
        }
        int ans=0;
        int cnt=0;
        priority_queue<node>pq;
        pq.push(rec[root]);
        while(!pq.empty())
        {
            cnt++;
            node tmp=pq.top();
            pq.pop();
            ans+=cnt*tmp.c;
            for(int i=1;i<=childc[tmp.id];i++)
            {
                pq.push(rec[child[tmp.id][i]]);
            }
        }
        //for(int i=1;i<=n;i++)
        //  printf("%lf %d %d\n",rec[i].c1,rec[i].t,u[i]);
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值