BZOJ2491 : Quelling Blade
首先将合成树展开,得到一棵不超过$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的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- TODO:Laravel 使用blade标签布局页面
TODO:Laravel 使用blade标签布局页面 本文主要介绍Laravel的标签使用,统一布局页面.主要用到到标签有@yield,@ stack,@extends,@section,@stop, ...
- 【blade利刃出鞘】一起进入移动端webapp开发吧
前言 在移动浪潮袭来的时候,小钗有幸进入框架组做webapp框架开发,过程中遇到了移动端的各种坑,也产生了各种激情,就我们公司的发展历程来说 第一阶段:使用传统方式开发移动站点,少量引入HTML5元素 ...
- 【blade的UI设计】理解前端MVC与分层思想
前言 最近校招要来了,很多大三的同学一定按捺不住心中的焦躁,其中有期待也有彷徨,或许更多的是些许担忧,最近在开始疯狂的复习了吧 这里小钗有几点建议给各位: ① 不要看得太重,关心则乱,太紧张反而表现不 ...
- Traveling in Blade & Soul
Traveling in Blade & Soul Walking is too simple. Having trained their physics and spirits for ye ...
- 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 ...
- blade and soul Group Combos
Group Combos A martial artist always make friends along their way. They learn how to work and fight ...
- blade and soul Personal Combos
Personal Combos Since Blade and Soul is mainly based on skills, the game is more interesting after y ...
- How to Develop blade and soul Skills
How to Develop Skills Each skill can be improved for variation effects. Some will boost more strengt ...
随机推荐
- 论文阅读笔记二十五:Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition(SPPNet CVPR2014)
论文源址:https://arxiv.org/abs/1406.4729 tensorflow相关代码:https://github.com/peace195/sppnet 摘要 深度卷积网络需要输入 ...
- hexo+github page +markdown问题汇总
1.没有权限提交 解决办法:把git版本由2.x改为1.9 未完待续
- jexus linux x64 [专业版] 安装和配置https
一.环境 操作系统:centOs7-x64 二.准备工作 购买SSL/TLS证书 三.部署 1.首先查看“/lib”或“/usr/lib”等系统库文件夹中是否有SSL库文件的名字,该文件名应该是“li ...
- WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable Exception in thread "main" java.io.IOException: No FileSystem for sc F
1.执行脚本程序报如下所示的错误: [hadoop@slaver1 script_hadoop]$ hadoop jar web_click_mr_hive.jar com.bie.hive.mr.C ...
- 为tomcat8安装Native library
安装依赖包 yum install -y cmake gcc expat-devel perl wget 安装apr wget http://mirrors.hust.edu.cn/apache//a ...
- JSP基础知识➣Cookie和Session(五)
JSP Cookie 处理 Cookie是存储在客户机的文本文件,它们保存了大量轨迹信息.在servlet技术基础上,JSP显然能够提供对HTTP cookie的支持.JSP脚本通过request对象 ...
- ubuntu ibus pinyin输入法异常
http://blog.csdn.net/granvillegao/article/details/41115211 命令行运行 ibus-setup 在常规页面,取消了“在应用程序窗口中启用内嵌编辑 ...
- [转]scp、sftp命令使用
http://wangxuedong.com/index.php/archives/182/ 前言 有时候想上传文件到服务器或者从服务器下载一个文件到本地,但是服务器还没有配置ftp等环境,这时候可以 ...
- BZOJ1096 [ZJOI2007]仓库建设 动态规划 斜率优化
原文链接http://www.cnblogs.com/zhouzhendong/p/8696410.html 题目传送门 - BZOJ1096 题意 给定两个序列$a,b,X$,现在划分$a$序列. ...
- OpenSSL-Win64创建IdentityServer证书
d:cd xx C:\OpenSSL-Win64\bin\openssl req -newkey rsa:2048 -nodes -keyout identity.key -x509 -days 36 ...