【题解】

  很容易想到暴力做法,枚举每个点,然后对于每个点O(N)遍历整棵树计算答案。这样整个效率是O(N^2)的,显然不行。

  我们考虑如果已知当前某个点的答案,如何快速计算它的儿子的答案。

  显然选择它的儿子作为集合点,它的儿子的子树内的奶牛可以少走当前点到儿子节点的距离dis,不在它儿子的子树内的奶牛要多走dis. 那么我们维护每个节点的子树内的奶牛总数(即点权和),就可以快速进行计算了。效率O(N).

  

  1. #include<cstdio>
  2. #include<algorithm>
  3. #define N 200010
  4. #define rg register
  5. #define LL long long
  6. using namespace std;
  7. int n,tot,last[N],v[N];
  8. LL ans,sum,size[N],dis[N];
  9. struct edge{
  10. int to,pre,dis;
  11. }e[N];
  12. inline int read(){
  13. int k=,f=; char c=getchar();
  14. while(c<''||c>'')c=='-'&&(f=-),c=getchar();
  15. while(''<=c&&c<='')k=k*+c-'',c=getchar();
  16. return k*f;
  17. }
  18. void dfs(int x,int fa){
  19. size[x]=v[x];
  20. for(rg int i=last[x],to;i;i=e[i].pre)if((to=e[i].to)!=fa){
  21. dis[to]=dis[x]+e[i].dis;
  22. dfs(to,x); size[x]+=size[to];
  23. }
  24. }
  25. void solve(int x,int fa,LL now){
  26. ans=min(ans,now);
  27. for(rg int i=last[x],to;i;i=e[i].pre)if((to=e[i].to)!=fa){
  28. LL tmp=now-size[to]*e[i].dis+(sum-size[to])*e[i].dis;
  29. solve(to,x,tmp);
  30. }
  31. }
  32. int main(){
  33. n=read();
  34. for(rg int i=;i<=n;i++) v[i]=read(),sum+=v[i];
  35. for(rg int i=;i<n;i++){
  36. int u=read(),v=read(),w=read();
  37. e[++tot]=(edge){v,last[u],w}; last[u]=tot;
  38. e[++tot]=(edge){u,last[v],w}; last[v]=tot;
  39. }
  40. dfs(,);
  41. for(rg int i=;i<=n;i++) ans+=dis[i]*v[i];
  42. solve(,,ans);
  43. printf("%lld\n",ans);
  44. return ;
  45. }

BZOJ 1827 洛谷 2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gather的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. 【luoguP2986】[USACO10MAR]伟大的奶牛聚集Great Cow Gathering

    题目链接 先把\(1\)作为根求每个子树的\(size\),算出把\(1\)作为集会点的代价,不难发现把集会点移动到\(u\)的儿子\(v\)上后的代价为原代价-\(v\)的\(size\)*边权+( ...

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

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

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

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

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

    传送门 解题思路 首先第一遍dfs预处理出每个点的子树的siz,然后可以处理出放在根节点的答案,然后递推可得其他答案,递推方程 sum[u]=sum[x]-(val[i]*siz[u])+(siz[1 ...

随机推荐

  1. matlab采用mex编译多个cpp文件

    最近在看matlab code时,由于本人使用的是64系统,而code中的mex文件时在32位系统上编译的,所以需要重新自己编译maxflowmex.cpp,但是直接mex maxflowmex.cp ...

  2. java笔记线程两种方式模拟电影院卖票

    public class SellTicketDemo { public static void main(String[] args) { // 创建三个线程对象 SellTicket st1 = ...

  3. (博弈论)51NOD 1072 威佐夫游戏

    有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆中取任意个或从2堆中取相同数量的石子,但不可不取.拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出2堆石子的数量, ...

  4. tomcat 参数调优

    JAVA_OPTS="-Xms2g -Xmx2g  -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath= ...

  5. linux vi 块操作、多窗口

    vim 块选择v:字符选择或者行选择[ctrl]+v: 块选择y:将反白的地方复制d:将反白的地方删除 多窗口:sp {filename} 打开一个新的窗口[ctrl]+w+j或者[ctrl]+w+向 ...

  6. Python中re操作正则表达式

    在python中使用正则表达式 1.转义符 正则表达式中的转义: '\('表示匹配小括号 [()+*/?&.] 在字符组中一些特殊的字符会现出原形 所有的\s\d\w\S\D\W\n\t都表示 ...

  7. Deepfakes教程及各个换脸软件下载

    源:https://blog.csdn.net/koest/article/details/80720078 Deepfakes目前用于深度换脸的程序基本都是用python编程语言基于tensorfl ...

  8. Android 性能优化(16)线程优化:Creating a Manager for Multiple Threads 如何创建一个线程池管理类

    Creating a Manager for Multiple Threads 1.You should also read Processes and Threads The previous le ...

  9. 407 Trapping Rain Water II 接雨水 II

    给定一个m x n的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水.说明:m 和 n 都是小于110的整数.每一个单位的高度都大于0 且小于 20000. ...

  10. Spring Boot (28) actuator与spring-boot-admin

    在上一篇中,通过restful api的方式查看信息过于繁琐,也不直观,效率低下.当服务过多的时候看起来就过于麻烦,每个服务都需要调用不同的接口来查看监控信息. SBA SBA全称spring boo ...