首先将合成树展开,得到一棵不超过$m(m\leq 10^6)$的有根树。

问题等价于,不休息地访问所有点,访问每个点需要时间$t_i$,价值为$v_i$。

设$vis_i$为访问$i$点的时间(不含$t_i$),最大化$\sum t_i\times v_i$。

根据排序不等式可得,需要按照$\frac{v_i}{t_i}$从小到大的顺序访问最优。

用堆维护所有非根节点,每次取出最优节点,将其与父亲合并,用并查集维护每个点的父亲,直到只剩根节点为止。

时间复杂度$O(m\log m)$。

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
typedef pair<double,P>PI;
const int N=1010,M=1000010;
int T,C,n,m,i,k,x,y,g[N],v[N],w[N],ed,nxt[N],vis[M],f[M];ll ans,st[M],sv[M];bool del[M];
struct E{int v,t;}a[N];
priority_queue<PI,vector<PI>,greater<PI> >q;
inline void add(int x,int y,int z){v[++ed]=y;w[ed]=z;nxt[ed]=g[x];g[x]=ed;}
void dfs(int x,int y){
int o=++m;
f[o]=y,vis[o]=o,del[o]=0;
sv[o]=a[x].v,st[o]=a[x].t;
if(o>1)q.push(PI(1.0*sv[o]/st[o],P(o,o)));
for(int i=g[x];i;i=nxt[i])for(int j=1;j<=w[i];j++)dfs(v[i],o);
}
int F(int x){return del[f[x]]?f[x]=F(f[x]):f[x];}
int main(){
scanf("%d",&T);
for(C=1;C<=T;C++){
scanf("%d",&n);
for(ans=m=ed=0,i=1;i<=n;i++)g[i]=0;
for(i=1;i<=n;i++){
scanf("%d%d%d",&a[i].v,&a[i].t,&k);
while(k--)scanf("%d%d",&x,&y),add(i,x,y);
}
dfs(1,0);
while(!q.empty()){
PI t=q.top();q.pop();
if(del[x=t.second.first])continue;
if(vis[x]!=t.second.second)continue;
del[x]=1;
y=F(x);
ans+=st[y]*sv[x];
st[y]+=st[x],sv[y]+=sv[x];
if(y>1)q.push(PI(1.0*sv[y]/st[y],P(y,vis[y]=++m)));
}
printf("Case #%d: %lld\n",C,ans);
}
return 0;
}

  

BZOJ2491 : Quelling Blade的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. TODO:Laravel 使用blade标签布局页面

    TODO:Laravel 使用blade标签布局页面 本文主要介绍Laravel的标签使用,统一布局页面.主要用到到标签有@yield,@ stack,@extends,@section,@stop, ...

  3. 【blade利刃出鞘】一起进入移动端webapp开发吧

    前言 在移动浪潮袭来的时候,小钗有幸进入框架组做webapp框架开发,过程中遇到了移动端的各种坑,也产生了各种激情,就我们公司的发展历程来说 第一阶段:使用传统方式开发移动站点,少量引入HTML5元素 ...

  4. 【blade的UI设计】理解前端MVC与分层思想

    前言 最近校招要来了,很多大三的同学一定按捺不住心中的焦躁,其中有期待也有彷徨,或许更多的是些许担忧,最近在开始疯狂的复习了吧 这里小钗有几点建议给各位: ① 不要看得太重,关心则乱,太紧张反而表现不 ...

  5. Traveling in Blade & Soul

    Traveling in Blade & Soul Walking is too simple. Having trained their physics and spirits for ye ...

  6. how to get soul shields in blade and soul

    These soul shields can either be obtained by E.Fleet Supply Chain or Blackram Supply Chain (4-man or ...

  7. blade and soul Group Combos

    Group Combos A martial artist always make friends along their way. They learn how to work and fight ...

  8. blade and soul Personal Combos

    Personal Combos Since Blade and Soul is mainly based on skills, the game is more interesting after y ...

  9. How to Develop blade and soul Skills

    How to Develop Skills Each skill can be improved for variation effects. Some will boost more strengt ...

随机推荐

  1. linux:安装并使用mongo

    1.下载mongo:  curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz 2.解压: tar -zxvf ...

  2. 区分TCP包的顺序

    确认TCP包的顺序: 使用抓包工具抓包之后,通常按照时间先后排序的,而不是数据的内容逻辑先后.查找内容的先后的关键在于查看TCP中的Sequence number和Acknowledgment num ...

  3. 几种stl的应用

    1.set(特点:插入后元素自动从小到大排序) set< int > ::iterator it;//迭代器,可以指向同类型的集合 q.find(k);//其中一个元素k的地址 q.cou ...

  4. SHELL打印两个日期之间的日期

    SHELL打印两个日期之间的日期 [root@umout shell]# cat date_to_date.sh THIS_PATH=$(cd `dirname $0`;) cd $THIS_PATH ...

  5. Ueditor设置默认字体、字号、行间距,添加字体种类(转)

    Ueditor默认字体.字号.行间距的修改: ueditor默认字号是16号,默认字体为sans-serif,默认行间距为5px,如下图所示: 首先,修改ueditor.all.js文件中如上图红框中 ...

  6. Debug.Assert vs Exception Throwing(转载)

    来源 Q: I've read plenty of articles (and a couple of other similar questions that were posted on Stac ...

  7. WMSYS.WM_CONCAT返回CLOB类型的解决办法

    https://blog.csdn.net/cnm_csdn_wt/article/details/80047878

  8. Processing3 1.随机行走

    class Walker { int x; int y; Walker() { x = width/2; y = height/2; } void display() { stroke(0); poi ...

  9. ASP.NET Core Middleware管道介绍

    public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.Use(async (context, ne ...

  10. [转] js对象监听实现

    前言 随着前端交互复杂度的提升,各类框架如angular,react,vue等也层出不穷,这些框架一个比较重要的技术点就是数据绑定.数据的监听有较多的实现方案,本文将粗略的描述一番,并对其中一个兼容性 ...