LUOGU P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
解题思路
首先第一遍dfs预处理出每个点的子树的siz,然后可以处理出放在根节点的答案,然后递推可得其他答案,递推方程 sum[u]=sum[x]-(val[i]*siz[u])+(siz[1]-siz[u])*val[i]
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
const int MAXN = 100005;
typedef long long LL;
inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {f=ch=='-'?0:1;ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f?x:-x;
}
LL ans,sum[MAXN],all[MAXN];
int siz[MAXN],n,head[MAXN],cnt,to[MAXN<<1],nxt[MAXN<<1],val[MAXN<<1];
inline void add(int bg,int ed,int w){
to[++cnt]=ed,nxt[cnt]=head[bg],val[cnt]=w,head[bg]=cnt;
}
void dfs1(int x,int fa,int d){
all[x]=(LL)siz[x]*d;
for(register int i=head[x];i;i=nxt[i]){
int u=to[i];if(u==fa) continue;
dfs1(u,x,d+val[i]);
siz[x]+=siz[u];all[x]+=all[u];
}
}
void dfs2(int x,int fa){
for(register int i=head[x];i;i=nxt[i]){
int u=to[i];if(u==fa) continue;
sum[u]=sum[x]-((LL)siz[u]*val[i])+(LL)(siz[1]-siz[u])*val[i];
ans=min(ans,sum[u]);
dfs2(u,x);
}
}
int main(){
n=rd();int x,y,z;
for(int i=1;i<=n;i++) siz[i]=rd();
for(int i=1;i<n;i++){
x=rd(),y=rd(),z=rd();
add(x,y,z),add(y,x,z);
}
dfs1(1,0,0);ans=sum[1]=all[1];dfs2(1,0);
cout<<ans<<endl;
return 0;
}
LUOGU P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…的更多相关文章
- 【题解】Luogu p2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat 树型dp
题目描述 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 ...
- 洛谷 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 < ...
- [USACO10MAR]伟大的奶牛聚集Great Cow Gat…【树形dp】By cellur925
题目传送门 首先这道题是在树上进行的,然后求最小的不方便程度,比较符合dp的性质,那么我们就可以搞一搞树形dp. 设计状态:f[i]表示以i作为聚集地的最小不方便程度.那么我们还需要各点间的距离,但是 ...
- [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… ($dfs$,树的遍历)
题目链接 Solution 辣鸡题...因为一个函数名看了我贼久. 思路很简单,可以先随便指定一个根,然后考虑换根的变化. 每一次把根从 \(x\) 换成 \(x\) 的一个子节点 \(y\),记录一 ...
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集(树形动规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- P2986 [USACO10MAR]伟大的奶牛聚集(思维,dp)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
随机推荐
- 【转载】opencl中设备内存
地址空间限定符 一般的内核代码中,里面的内核参数或声明变量时,都会有地址空间限定符 地址空间限定符,地址空间限定符的主要作用是指出数据应该保存在哪个地方 地址空间限定符有4个: 全局内存: 限定符:_ ...
- 学习js和jQuery总结
数据类型的转换:1.字符串必须放在引号内2.字符串的转换有两个标签,string和tostring这两个函数.3.变量必须使用驼峰形式,把一个变量转换成字符串:console.log(isChild. ...
- CreateRemoteThread简单应用
要实现线程的远程注入必须使用Windows提供的CreateRemoteThread函数来创建一个远程线程 该函数的原型如下: HANDLE CreateRemoteThread( HAND ...
- Docker系列(五):Docker网络机制(上)
Linux路由机制打通网络 路由机制是效率最好的 docker128上修改Docker0的网络地址,与docker130不冲突 vi /usr/lib/systemd/system/docker.se ...
- JS if 判断
if条件判断 1.语法结构——只判断true,不判断false if(条件判断:结果只有两个true或false) { 条件为true,将执行该代码: } 说明: if是系统关键字,必须全小写. ...
- 1.RCC of STM32F103
开发板用的是8Mhz晶振,即HSE=8,000,000 f103最大支持72Mhz,即SYSCLK=PLLCLK=72Mhz,HSE=8M,所以PLLM=9即可. /* HCLK = SYSCLK * ...
- 万恶之源-python介绍
PATH OF PYTHON (生命短暂,我要学pythonヾ(◍°∇°◍)ノ゙) 一.Python介绍: 简史:Python诞生于1989年的圣诞节, 创始人为Guido van Rossum, 又 ...
- UMP系统功能 资源隔离
- Nand flash 芯片工作原理
Nand flash 芯片型号为 Samsung K9F1208U0B,数据存储容量为 64MB,采用块页式存储管理.8 个 I/O 引脚充当数据.地址.命令的复用端口. 芯片内部存储布局及存储操作特 ...
- 「题解」:[AHOI2013]作业
问题: 作业 时间限制: 10 Sec 内存限制: 512 MB 题面 题目描述 此时己是凌晨两点,刚刚做了Codeforces的小A掏出了英语试卷.英语作业其实不算多,一个小时刚好可以做完.然后是 ...