传送门

解题思路

首先第一遍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…的更多相关文章

  1. 【题解】Luogu p2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat 树型dp

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  2. P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  3. 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…(树规)

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  4. [洛谷P2986][USACO10MAR]伟大的奶牛聚集Great Cow Gat…

    题目大意:给你一棵树,每个点有点权,边有边权,求一个点,使得其他所有点到这个点的距离和最短,输出这个距离 题解:树形$DP$,思路清晰,转移显然 卡点:无 C++ Code: #include < ...

  5. [USACO10MAR]伟大的奶牛聚集Great Cow Gat…【树形dp】By cellur925

    题目传送门 首先这道题是在树上进行的,然后求最小的不方便程度,比较符合dp的性质,那么我们就可以搞一搞树形dp. 设计状态:f[i]表示以i作为聚集地的最小不方便程度.那么我们还需要各点间的距离,但是 ...

  6. [USACO10MAR]伟大的奶牛聚集Great Cow Gat…

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  7. [USACO10MAR]伟大的奶牛聚集Great Cow Gat… ($dfs$,树的遍历)

    题目链接 Solution 辣鸡题...因为一个函数名看了我贼久. 思路很简单,可以先随便指定一个根,然后考虑换根的变化. 每一次把根从 \(x\) 换成 \(x\) 的一个子节点 \(y\),记录一 ...

  8. 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集(树形动规)

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  9. P2986 [USACO10MAR]伟大的奶牛聚集(思维,dp)

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

随机推荐

  1. 期望dp——zoj3640

    /* dp[i]表示力量为i时的期望 dp[i]=sum{tj}/n+sum{dp[i+cj]+1}/n //前一项是cj<i的和,后一项是cj>=i的和 初始状态dp[m] */ #in ...

  2. 牛客多校第五场 A digits 2 签到

    题意: 给定一个n,输出一个数,要求这个数所有位之和整除n,并且这个数也整除n,并且位数不许多于1e4 题解: 把这个数n输出n遍. #include<iostream> using na ...

  3. php算法题---连续子数组的最大和

    php算法题---连续子数组的最大和 一.总结 一句话总结: 重要:一定要本机调试过了再提交代码,因为很容易出现考虑不周的情况,或者修改了之后没有考虑修改的这部分 利用空间来换时间,或者利用空间来换算 ...

  4. ul列表元素在float:right后li元素倒转

    发现对li元素进行float:right后,虽然成功右浮动,但是的元素是倒转的 解决方案: 对ul进行右浮动,然后对li左浮动 结果

  5. iOS开发之SceneKit框架--SCNLight.h

    1.SCNLight简介 用于添加光源,连接到一个节点照亮现场,可以给场景添加不同的灯光,模拟逼真的环境. 2.四种灯光的简介 添加一个box立方体.一个tube圆柱管道和一个地板floor,没有灯光 ...

  6. wrapClass

    参考文章   https://blog.csdn.net/wyk304443164/article/details/68484305

  7. 2019-8-31-dotnet-core-发布只带必要的依赖文件

    title author date CreateTime categories dotnet core 发布只带必要的依赖文件 lindexi 2019-08-31 16:55:58 +0800 20 ...

  8. redis笔记_源码_内存分配

    文件:zmoalloc.h zmoalloc.c 1.求两个整数的余数 eg: 求_n对sizeof(long)的余数(_n&(sizeof(long)-1)), 性能提升为50%-100% ...

  9. Python遇到的第一个问题

    1.运行如下代码: 输入成绩80之后报错: 2.问题分析:字符串跟整型不能比 3.查看score的类型 print(type(score)), 由此看出score是string类型的,因为input接 ...

  10. 廖雪峰Java11多线程编程-2线程同步-2synchronized方法

    1.Java使用synchronized对一个方法进行加锁 class Counter{ int count = 0; public synchronized void add(int n){ cou ...