bzoj 2809

2809: [Apio2012]dispatching

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 4519  Solved: 2329
[Submit][Status][Discuss]

Description

在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿。在这个帮派里,有一名忍者被称之为 Master。除了 Master以外,每名忍者都有且仅有一个上级。为保密,同时增强忍者们的领导力,所有与他们工作相关的指令总是由上级发送给他的直接下属,而不允许通过其他的方式发送。现在你要招募一批忍者,并把它们派遣给顾客。你需要为每个被派遣的忍者 支付一定的薪水,同时使得支付的薪水总额不超过你的预算。另外,为了发送指令,你需要选择一名忍者作为管理者,要求这个管理者可以向所有被派遣的忍者 发送指令,在发送指令时,任何忍者(不管是否被派遣)都可以作为消息的传递 人。管理者自己可以被派遣,也可以不被派遣。当然,如果管理者没有被排遣,就不需要支付管理者的薪水。你的目标是在预算内使顾客的满意度最大。这里定义顾客的满意度为派遣的忍者总数乘以管理者的领导力水平,其中每个忍者的领导力水平也是一定的。写一个程序,给定每一个忍者 i的上级 Bi,薪水Ci,领导力L i,以及支付给忍者们的薪水总预算 M,输出在预算内满足上述要求时顾客满意度的最大值。


 

1  ≤N ≤ 100,000 忍者的个数;
1  ≤M ≤ 1,000,000,000 薪水总预算; 
 
0  ≤Bi < i  忍者的上级的编号;
1  ≤Ci ≤ M                     忍者的薪水;
1  ≤Li ≤ 1,000,000,000             忍者的领导力水平。
 
 

 

Input

从标准输入读入数据。
 
第一行包含两个整数 N M,其中 N表示忍者的个数,M表示薪水的总预算。
 
接下来 N行描述忍者们的上级、薪水以及领导力。其中的第 i 行包含三个整 Bi , C i , L i分别表示第i个忍者的上级,薪水以及领导力。Master满足B i = 0并且每一个忍者的老板的编号一定小于自己的编号 Bi < i


 

 

Output

输出一个数,表示在预算内顾客的满意度的最大值。
 
 

Sample Input


5 4
0 3 3
1 3 5
2 2 2
1 2 4
2 3 1

Sample Output

6

HINT

 



如果我们选择编号为 1的忍者作为管理者并且派遣第三个和第四个忍者,薪水总和为 4,没有超过总预算                         4。因为派遣了                              2   个忍者并且管理者的领导力为      3,

用户的满意度为 2      ,是可以得到的用户满意度的最大值。

 

Source

代码:

//对于每一个子树显然是选花费小的节点合算,维护一个大根堆,并且当堆的花费和大于m时删掉堆根。
//斜堆。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int MAXN=100009;
int n,m,tot,cnt,head[MAXN],C[MAXN],L[MAXN],node[MAXN][2],root[MAXN],key[MAXN];
ll sum[MAXN],size[MAXN],ans;
struct Edge { int to,next; }edge[MAXN<<1];
void init()
{
    tot=cnt=0;
    ans=0;
    memset(head,-1,sizeof(head));
    memset(node,0,sizeof(node));
}
void addedge(int x,int y)
{
    edge[tot].to=y;edge[tot].next=head[x];
    head[x]=tot++;
}
int mmeg(int x,int y)
{
    if(x==0) return y;
    if(y==0) return x;
    if(key[x]<key[y]) swap(x,y);
    node[x][1]=mmeg(node[x][1],y);
    swap(node[x][0],node[x][1]);
    return x;
}
int ttop(int x) { return key[x]; }
int ppop(int x) { return mmeg(node[x][0],node[x][1]); }
void dfs(int x)
{
    root[x]=++cnt;
    size[x]=1;
    key[cnt]=sum[x]=C[x];
    for(int i=head[x];i!=-1;i=edge[i].next){
        int y=edge[i].to;
        dfs(y);
        size[x]+=size[y];
        sum[x]+=sum[y];
        root[x]=mmeg(root[x],root[y]);
    }
    while(sum[x]>m){
        sum[x]-=ttop(root[x]);
        root[x]=ppop(root[x]);
        size[x]--;
    }
    ans=max(ans,size[x]*L[x]);
}
int main()
{
    init();
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d%d%d",&x,&C[i],&L[i]);
        addedge(x,i);
    }
    dfs(1);
    printf("%lld\n",ans);
    return 0;
}
//左偏树。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int MAXN=100009;
int n,m,tot,cnt,head[MAXN],node[MAXN][2],key[MAXN],root[MAXN],d[MAXN],C[MAXN],L[MAXN];
ll size[MAXN],sum[MAXN],ans;
struct Edge { int to,next; }edge[MAXN<<1];
void init()
{
    tot=cnt=0;
    ans=0;
    memset(head,-1,sizeof(head));
    memset(node,0,sizeof(node));
    memset(d,-1,sizeof(d));
}
void addedge(int x,int y)
{
    edge[tot].to=y;edge[tot].next=head[x];
    head[x]=tot++;
}
int mmeg(int x,int y)
{
    if(x==0) return y;
    if(y==0) return x;
    if(key[x]<key[y]) swap(x,y);
    node[x][1]=mmeg(node[x][1],y);
    if(d[node[x][1]]>d[node[x][0]]) swap(node[x][1],node[x][0]);
    d[x]=d[node[x][1]]+1;
    return x;
}
int ttop(int x) { return key[x]; }
int ppop(int x) { return mmeg(node[x][1],node[x][0]); }
void dfs(int x)
{
    size[x]=1;
    root[x]=++cnt;
    d[cnt]=0;
    sum[x]=key[cnt]=C[x];
    for(int i=head[x];i!=-1;i=edge[i].next){
        int y=edge[i].to;
        dfs(y);
        size[x]+=size[y];
        sum[x]+=sum[y];
        root[x]=mmeg(root[x],root[y]);
    }
    while(sum[x]>m){
        sum[x]-=ttop(root[x]);
        root[x]=ppop(root[x]);
        size[x]--;
    }
    ans=max(ans,size[x]*L[x]);
}
int main()
{
    init();
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d%d%d",&x,&C[i],&L[i]);
        addedge(x,i);
    }
    dfs(1);
    printf("%lld\n",ans);
    return 0;
}

 

posted @ 2017-12-08 16:33  luckilzy  阅读(373)  评论(0编辑  收藏  举报