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 ...
随机推荐
- Java接口自动化测试之集成MyBatis和MySQL (五)
pom.xml新增dependency <dependency> <groupId>org.mybatis</groupId> <artifactId> ...
- python requests的content和text方法的区别
requests对象的get和post方法都会返回一个Response对象,这个对象里面存的是服务器返回的所有信息,包括响应头,响应状态码等.其中返回的网页部分会存在.content和.text两个对 ...
- B: Ocean的游戏(前缀和)
B: Ocean的游戏 Time Limit: 1 s Memory Limit: 128 MB Submit My Status Problem Description 给定一个字符串s, ...
- Python数据分析几个比较常用的方法
1,表头或是excel的索引如果是中文的话,输出会出错 解决方法:python的版本问题!换成python3就自动解决了!当然也有其他的方法,这里就不再深究 2,如果有很多列,如何输出指定的列? ...
- Django主线
Django怎么学: 参考地址:https://www.zhihu.com/question/26235428 需要了解的知识点: Django Url请求流程 首要操作 Django的安装 pip3 ...
- pycaffe简明文档
pycaffe简明文档 by ChrisZZ, imzhuo@foxmail.com 2018年01月18日19:00:56 说明 caffe的python接口没有官方说明文档,例如查看一个函数的用法 ...
- SqlServer 四大排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)简介
CREATE TABLE [dbo].[Order]( [ID] [int] IDENTITY(1,1) NOT NULL, [UserId] [int] NOT NULL, [TotalPrice] ...
- WPF编程之找不到资源mainWindow.xaml
原文: WPF编程之找不到资源“window1.xaml”之谜 因为将启动窗口移动到了一个新建的文件夹中,启动调试时报找不到资源mainWindow.xaml,原来是App.xaml里面设置的启动窗口 ...
- explicit specialization 显式指定
//explicit specialization 显式指定 #include "stdafx.h" #include <iostream> #include < ...
- [转] linux学习第四十四篇:Nginx安装,Nginx默认虚拟主机,Nginx域名重定向
Nginx安装 进入存放源码包的目录: cd /usr/local/src 下载源码包: wget http://nginx.org/download/nginx-1.12.1.tar.gz 解压: ...