bzoj 2809

2809: [Apio2012]dispatching

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

Description

在一個忍者的幫派裏,一些忍者們被選中派遣給顧客,而後依據本身的工做獲取報償。在這個幫派裏,有一名忍者被稱之爲 Master。除了 Master之外,每名忍者都有且僅有一個上級。爲保密,同時加強忍者們的領導力,全部與他們工做相關的指令老是由上級發送給他的直接下屬,而不容許經過其餘的方式發送。如今你要招募一批忍者,並把它們派遣給顧客。你須要爲每一個被派遣的忍者 支付必定的薪水,同時使得支付的薪水總額不超過你的預算。另外,爲了發送指令,你須要選擇一名忍者做爲管理者,要求這個管理者能夠向全部被派遣的忍者 發送指令,在發送指令時,任何忍者(無論是否被派遣)均可以做爲消息的傳遞 人。管理者本身能夠被派遣,也能夠不被派遣。固然,若是管理者沒有被排遣,就不須要支付管理者的薪水。你的目標是在預算內使顧客的滿意度最大。這裏定義顧客的滿意度爲派遣的忍者總數乘以管理者的領導力水平,其中每一個忍者的領導力水平也是必定的。寫一個程序,給定每個忍者  i的上級  B i ,薪水 Ci ,領導力 L i ,以及支付給忍者們的薪水總預算  M,輸出在預算內知足上述要求時顧客滿意度的最大值。


 

1  ≤ N ≤ 100,000 忍者的個數;
1  ≤ M ≤ 1,000,000,000 薪水總預算; 
 
0  ≤ B i < i  忍者的上級的編號;
1  ≤ C i ≤ M                     忍者的薪水;
1  ≤ L i ≤ 1,000,000,000             忍者的領導力水平。
 
 

 

Input

從標準輸入讀入數據。
 
第一行包含兩個整數  N M,其中  N表示忍者的個數, M表示薪水的總預算。
 
接下來  N行描述忍者們的上級、薪水以及領導力。其中的第  i 行包含三個整  B i , C i , L i 分別表示第 i 個忍者的上級,薪水以及領導力。 Master 知足 B i = 0 而且每個忍者的老闆的編號必定小於本身的編號  B i < 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      ,是能夠獲得的用戶滿意度的最大值。php

 

Source

代碼:node

//對於每個子樹顯然是選花費小的節點合算,維護一個大根堆,而且當堆的花費和大於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;
}
相關文章
相關標籤/搜索