题目链接:Click here

Solution:

设f[x]表示要使x向它的父亲写信需要花的最少的钱,per[x]为要使x向它的父亲写信最少要多少人

则\(f[x]=\sum_{i=1}^{per[x]}f[son[x]]\),此时的f数组是从小到大排过序的

那我们只需要把每个点的儿子放到multiset去维护就好了,最后输出f[0]

总时间复杂度\(O(n\,log\,n)\)

Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+1;
const int inf=2147483647;
int n,m,t,cnt,head[N];
int ans,a[N],per[N],cost[N],num[N];
struct Edge{int nxt,to;}edge[N];
struct cmp{bool operator()(const int& a,const int& b)const{return cost[a]<cost[b];}};
multiset<int,cmp> q[N];
multiset<int,cmp>::iterator it;
void ins(int x,int y){
edge[++cnt].nxt=head[x];
edge[cnt].to=y;head[x]=cnt;
}
void dfs(int x){
int flag=0;
for(int i=head[x];i;i=edge[i].nxt){
int y=edge[i].to;
dfs(y);q[x].insert(y);
flag=1;
}
if(!flag){cost[x]=a[x];return ;}
if(x){
int u=per[x];it=q[x].begin();
for(int i=1;i<=u;i++){
if(it==q[x].end()){
cost[x]=inf;
return ;
}cost[x]+=cost[*it],++it;
}
}else{int w=(m*num[x]+t-1)/t;
it=q[x].begin();
for(int i=1;i<=w;i++)
ans+=cost[*it],++it;
}
}
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
signed main(){
n=read(),t=read(),m=read();
for(int i=1;i<=n;i++){
int x=read(),y=read();
ins(x,i);a[i]=y;num[x]++;
}
for(int i=1;i<=n;i++) per[i]=(num[i]*a[i]+t-1)/t;
dfs(0);printf("%lld\n",ans);
return 0;
}

Luogu P3408 恋爱的更多相关文章

  1. 恋爱虽易,相处不易:当EntityFramework爱上AutoMapper

    剧情开始 为何相爱? 相处的问题? 女人的伟大? 剧情收尾? 有时候相识即是一种缘分,相爱也不需要太多的理由,一个眼神足矣,当EntityFramework遇上AutoMapper,就是如此,恋爱虽易 ...

  2. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  3. 高手总结的“恋爱法”学习Linux系统,效果更好。

    如果你恋爱了,那你一定非常喜欢她.了解她,知道她喜欢吃什么玩什么,知道她需要什么,在她生气的时候可以哄她开心,一切尽在你的手指中.那你想学好Linux吗?喜欢Linux吗?你懂她吗?你有喜欢Linux ...

  4. luogu p1268 树的重量——构造,真正考验编程能力

    题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...

  5. [luogu P2170] 选学霸(并查集+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...

  6. [luogu P2647] 最大收益(贪心+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...

  7. 【有趣~】SFOJ-1711 Obey的恋爱、NYOJ-739 笨蛋难题

    笨蛋难题四 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 这些日子笨蛋一直研究股票,经过调研,终于发现xxx公司股票规律,更可喜的是 笨蛋推算出这家公司每天的股价, ...

  8. Luogu 考前模拟Round. 1

    A.情书 题目:http://www.luogu.org/problem/show?pid=2264 赛中:sb题,直接暴力匹配就行了,注意一下读入和最后一句话的分句 赛后:卧槽 怎么只有40 B.小 ...

  9. UESTC_秋实大哥の恋爱物语 2015 UESTC Training for Search Algorithm & String<Problem K>

    K - 秋实大哥の恋爱物语 Time Limit: 5000/2000MS (Java/Others)     Memory Limit: 32000/32000KB (Java/Others) Su ...

随机推荐

  1. css3实现倾斜转动的转盘

    HTML代码: <div class="r-1">a</div> <div class="r-2">a</div> ...

  2. 深入理解java:2.3.4. 并发编程concurrent包 之容器ConcurrentLinkedQueue(非阻塞的并发队列---循环CAS)

    1.    引言 在并发编程中我们有时候需要使用线程安全的队列. 如果我们要实现一个线程安全的队列有两种实现方式:一种是使用阻塞算法,另一种是使用非阻塞算法. 使用阻塞算法的队列可以用一个锁(入队和出 ...

  3. [转帖] 国产x86-海光禅定 2018年营收过亿?

    中科曙光:全年业绩稳健,海光芯片营收过亿 X86服务器市场Intel占据绝对优势:X86处理器已经成为全球最广泛使用的处理器架构之一,尤其是在PC和服务器领域,其中在处理器市场的份额高达90%以上.中 ...

  4. MySQL总结(5)

    视图 SELECT cust_name,cust_contact FROM customers,orders,orderitems WHERE customers.cust_id=orders.cus ...

  5. Android 之 悬浮窗口

    1. 创建并设置  WindowManager  类 WindowManager mWindowManager; // 取得系统窗体 mWindowManager = (WindowManager) ...

  6. leetcode 980. Unique Paths III

    On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  There is e ...

  7. TCP的三次握手与四次挥手理解及面试题

    TCP的三次握手与四次挥手理解及面试题(很全面) 转载自:https://blog.csdn.net/qq_38950316/article/details/81087809 本文经过借鉴书籍资料.他 ...

  8. ms17010批量扫描备忘

    安装一些依赖: dpkg --add-architecture i386 && apt-get update && apt-get install wine32 rm ...

  9. Librepilot-Windows编译环境的搭建

      1.安装Msys2 下载Msys2,下载地址 https://msys2.github.io,注意根据笔记本的型号选择32bit或64bit. 2.添加LibrePilot MinGW库 在 /e ...

  10. 006-saltstack之远程执行

    1.目标 2.执行模块 3.返回 salt ‘*’ cmd.run ‘uptime’ 命令 目标 执行模块 执行模块参数 1.SlatStack远程执行–目标 执行目标:https://docs.sa ...