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 ...
随机推荐
- Two Sum【LeetCode】
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- caffe入门-人脸检测1
最近刚入门caffe,跟着视频做了一个简单人脸检测.包括人脸二分类模型+方框框出人脸. 人脸二分类模型 1. 收集数据 我用的是lfw数据集,总共有13233张人脸图片.非人脸数据有两种选择.1. 用 ...
- https://www.cnblogs.com/zoro-robin/p/6110188.html
https://www.cnblogs.com/zoro-robin/p/6110188.html https://blog.csdn.net/kongxx/article/details/65435 ...
- [转] Webpack-CommonsChunkPlugin
当前项目结构 项目结构 其中 Greeter.js 引用了 config.json main.js 和 second.js 都引用了 Greeter.js main.js 还引用了 onlyfor ...
- LibreOJ β Round #2
题解: 都是不错的技巧题目 t1暴力就不说了 t2dp是比较显然的 然后发现都是0,1用bitset优化 代码非常短 t3容易发现这个东西在不断合并 于是我们想到启发式合并 存疑:splay启发式合并 ...
- mysql 5.6.25编译安装详细步骤
简略步骤: mysql5.6.25编译安装步骤: 下载mysql准备用户和组yum安装依赖解压mysqlcmake编译mysqlmake && make install ----时间约 ...
- net core体系-web应用程序-4asp.net core2.0 项目实战(1)-8项目加密解密方案
本文目录1. 摘要2. MD5加密封装3. AES的加密.解密4. DES加密/解密5. 总结 1. 摘要 C#中常用的一些加密和解密方案,如:md5加密.RSA加密与解密和DES加密等,Asp.N ...
- Java中文字符所占的字节数
Java语言中,中文字符所占的字节数取决于字符的编码方式,一般情况下,采用ISO8859-1编码方式时,一个中文字符与一个英文字符一样只占1个字节:采用GB2312或GBK编码方式时,一个中文字符占2 ...
- 配置maven从自己的私服下载jar包nexus、maven私服仓库(二)
配置maven项目从私服下载jar包 pom文件配置从maven私服下载jar包 settings文件配置从maven私服下载jar包 (方便自己关键字搜索,所以多写了几行o(* ̄︶ ̄*)o) 今天自 ...
- Python交互图表可视化Bokeh:2. 辅助参数
图表辅助参数设置 辅助标注.注释.矢量箭头 参考官方文档:https://bokeh.pydata.org/en/latest/docs/user_guide/annotations.html#col ...