BZOJ 1827 洛谷 2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gather
【题解】
很容易想到暴力做法,枚举每个点,然后对于每个点O(N)遍历整棵树计算答案。这样整个效率是O(N^2)的,显然不行。
我们考虑如果已知当前某个点的答案,如何快速计算它的儿子的答案。
显然选择它的儿子作为集合点,它的儿子的子树内的奶牛可以少走当前点到儿子节点的距离dis,不在它儿子的子树内的奶牛要多走dis. 那么我们维护每个节点的子树内的奶牛总数(即点权和),就可以快速进行计算了。效率O(N).
#include<cstdio>
#include<algorithm>
#define N 200010
#define rg register
#define LL long long
using namespace std;
int n,tot,last[N],v[N];
LL ans,sum,size[N],dis[N];
struct edge{
int to,pre,dis;
}e[N];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
void dfs(int x,int fa){
size[x]=v[x];
for(rg int i=last[x],to;i;i=e[i].pre)if((to=e[i].to)!=fa){
dis[to]=dis[x]+e[i].dis;
dfs(to,x); size[x]+=size[to];
}
}
void solve(int x,int fa,LL now){
ans=min(ans,now);
for(rg int i=last[x],to;i;i=e[i].pre)if((to=e[i].to)!=fa){
LL tmp=now-size[to]*e[i].dis+(sum-size[to])*e[i].dis;
solve(to,x,tmp);
}
}
int main(){
n=read();
for(rg int i=;i<=n;i++) v[i]=read(),sum+=v[i];
for(rg int i=;i<n;i++){
int u=read(),v=read(),w=read();
e[++tot]=(edge){v,last[u],w}; last[u]=tot;
e[++tot]=(edge){u,last[v],w}; last[v]=tot;
}
dfs(,);
for(rg int i=;i<=n;i++) ans+=dis[i]*v[i];
solve(,,ans);
printf("%lld\n",ans);
return ;
}
BZOJ 1827 洛谷 2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gather的更多相关文章
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…(树规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- [洛谷P2986][USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目大意:给你一棵树,每个点有点权,边有边权,求一个点,使得其他所有点到这个点的距离和最短,输出这个距离 题解:树形$DP$,思路清晰,转移显然 卡点:无 C++ Code: #include < ...
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集(树形动规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- [USACO10MAR]伟大的奶牛聚集Great Cow Gat…【树形dp】By cellur925
题目传送门 首先这道题是在树上进行的,然后求最小的不方便程度,比较符合dp的性质,那么我们就可以搞一搞树形dp. 设计状态:f[i]表示以i作为聚集地的最小不方便程度.那么我们还需要各点间的距离,但是 ...
- 【luoguP2986】[USACO10MAR]伟大的奶牛聚集Great Cow Gathering
题目链接 先把\(1\)作为根求每个子树的\(size\),算出把\(1\)作为集会点的代价,不难发现把集会点移动到\(u\)的儿子\(v\)上后的代价为原代价-\(v\)的\(size\)*边权+( ...
- [USACO10MAR]伟大的奶牛聚集Great Cow Gat… ($dfs$,树的遍历)
题目链接 Solution 辣鸡题...因为一个函数名看了我贼久. 思路很简单,可以先随便指定一个根,然后考虑换根的变化. 每一次把根从 \(x\) 换成 \(x\) 的一个子节点 \(y\),记录一 ...
- [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- LUOGU P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
传送门 解题思路 首先第一遍dfs预处理出每个点的子树的siz,然后可以处理出放在根节点的答案,然后递推可得其他答案,递推方程 sum[u]=sum[x]-(val[i]*siz[u])+(siz[1 ...
随机推荐
- bzoj2440 [中山市选2011]完全平方数——莫比乌斯+容斥
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2440 莫比乌斯...被难倒... 看TJ:http://hzwer.com/4827.htm ...
- MySQL:MySQL安装
ylbtech-MySQL:MySQL安装 mysql5.5.27_win64_zol.msi 1.返回顶部 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14 ...
- Rails5 layout 和 template
layout是布局,比如页面的头(head), 脚(foot), 内容(body) template是布局的一部分的内容 这两货实在太像了,写了这些我也是一脸懵逼. 换个说法,layout和tem ...
- 在 Vue 项目中(vue-cli2,vue-cli3)使用 pug 简化 HTML 的编写
使用 pug 的原因: 使得 HTML 写起了来更加清晰和快捷 用法: Vue 的用法没有变化: <template lang="pug"> transition(na ...
- php insteadof 作用
PHP5的另一个新成员是instdnceof关键字.使用这个关键字可以确定一个对象是类的实例.类的子类,还是实现了某个特定接口,并进行相应的操作.在某些情况下,我们希望确定某个类是否特定的类型,或者是 ...
- 推荐一波 瀑布流的RecylceView
推荐博客:http://www.bubuko.com/infodetail-999014.html
- 如何调试ajax 和php
###ex11_1_main.php <html><head><meta http-equiv="Content-Type" content=&quo ...
- pycharm但多行注释快捷键
pycharm中同时注释多行代码快捷键: 代码选中的条件下,同时按住 Ctrl+/,被选中行被注释,再次按下Ctrl+/,注释被取消
- JDK API文档下载
java SE 8 API文档:http://www.oracle.com/technetwork/java/javase/documentation/jdk8-doc-downloads-21331 ...
- Spring框架学习-Spring和IOC概述
一:什么是Spring框架? spring是一个分层的javase/EEfull-stack(一站式)轻量级的java开源框架.是为了解决企业开发的复杂性而创建的.框架的主要优势是分层架构,Sprin ...