有点权的重心,拆掉点dfs不就是了吗

//#include <iostream>
#include <cstdio>
#include <cstring>
//#include <algorithm>
//#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long #define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; const int N = 100007; struct Edge{
int nxt, pre, w;
}e[N << 1];
int head[N], cntEdge;
inline void add(int u, int v, int w){
e[++cntEdge] = (Edge){ head[u], v, w}, head[u] = cntEdge;
} int C[N], totSize;
int siz[N];
long long f[N];
inline void DFS_1(int u, int fa){
siz[u] = C[u];
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(v == fa) continue;
DFS_1(v, u);
siz[u] += siz[v];
f[u] += f[v] + 1ll * siz[v] * e[i].w;
}
}
long long ans = 9223372036854775807;
inline void DFS_2(int u, int fa){
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(v == fa) continue;
f[v] = f[u] - 1ll * siz[v] * e[i].w + 1ll * (totSize - siz[v]) * e[i].w;
ans = Min(ans, f[v]);
DFS_2(v, u);
}
}
int main(){
//FileOpen();
int n;
io >> n;
R(i,1,n){
io >> C[i];
totSize += C[i];
}
R(i,2,n){
int u, v, w;
io >> u >> v >> w;
add(u, v, w);
add(v, u, w);
} // DFS_First(1, 0);
// DFS_Second(1, 1);
//
// long long ans = 9223372036854775807;
// R(i,1,n){
// long long sum = 0;
// R(j,1,n){
// if(i == j) continue;
// sum += 1ll * (dis[i] - (dis[LCA(i, j)] << 1) + dis[j]) * C[j];
// }
// ans = Min(ans, sum);
// } DFS_1(1, 0);
for(register int i = 2; i <= n; i += 3) f[i] = f[i + 1] = f[i + 2] = 0;
DFS_2(1, 0); printf("%lld", ans); return 0;
}

Luogu2986 [USACO10MAR]伟大的奶牛聚集 (树形DP)的更多相关文章

  1. [USACO10MAR] 伟大的奶牛聚集 - 树形dp

    每个点有重数,求到所有点距离最小的点 就是魔改的重心了 #include <bits/stdc++.h> using namespace std; #define int long lon ...

  2. [USACO10MAR]伟大的奶牛聚集

    [USACO10MAR]伟大的奶牛聚集 Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会. 每个奶牛居住在 N(1<=N& ...

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

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

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

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

  5. 【BZOJ】2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛(树形dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2060 裸的树形dp d[x][1]表示访问x的数量,d[x][0]表示不访问x的数量 d[x][1] ...

  6. [bzoj2060][Usaco2010 Nov]Visiting Cows 拜访奶牛_树形dp

    Visiting Cows 拜访奶牛 bzoj-2060 Usaco-2010 Nov 题目大意:题目链接. 注释:略. 想法:看起来像支配集. 只是看起来像而已. 状态:dp[pos][flag]表 ...

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

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

  8. [USACO10MAR]伟大的奶牛聚集 BZOJ 1827 树形dp+dfs

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

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

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

随机推荐

  1. 制造企业信息化时代,SaaS系统下沉,移动端上升

    这个时代,我们是不是有很多岗位一定是要在电脑前面完成?如果我们让部分岗位的办公室人员离开电脑,让他们通过移动端来完成工作,这又会产生出一个什么样的变化?是否意味着可以有更多的时间在一线生产制造现场,从 ...

  2. 【原创】项目四Tr0ll-1

    实战流程 1.nmap枚举 nmap -sP 192.168.186.0/24 nmap -p- 192.168.186.142 nmap 192.168.186.142 -p- -sS -sV -A ...

  3. 深入探究MinimalApi是如何在Swagger中展示的

    前言 之前看到技术群里有同学讨论说对于MinimalApi能接入到Swagger中感到很神奇,加上Swagger的数据本身是支持OpenApi2.0和OpenApi3.0使得swagger.json成 ...

  4. CentOS8设置国内镜像源(阿里云镜像)

    CentOS8设置国内镜像源(阿里云) 1.备份原有配置 [root@localhost ~]# mkdir /etc/yum.repos.d.bak [root@localhost ~]# mv / ...

  5. 使用 content-visibility 优化渲染性能

    最近在业务中实际使用 content-visibility 进了一些渲染性能的优化. 这是一个比较新且有强大功能的属性.本文将带领大家深入理解一番. 何为 content-visibility? co ...

  6. 一次 MySQL 误操作导致的事故,「高可用」都顶不住了!

    这是悟空的第 152 篇原创文章 官网:www.passjava.cn 你好,我是悟空. 上次我们项目不是把 MySQL 高可用部署好了么,MySQL 双主模式 + Keepalived,来保证高可用 ...

  7. 一个紧张刺激的聊天器,要不要进来看看(Python UDP网络模型)

    先来哔哔两句:(https://jq.qq.com/?_wv=1027&k=QgGWqAVF) 互联网的本质是什么?其实就是信息的交换.那么如何将自己的信息发送到其他人的电脑上呢?那就需要借助 ...

  8. 论HashMap、Hashtable、TreeMap、LinkedHashMap的内部排序

    参考文章 论HashMap.Hashtable.TreeMap.LinkedHashMap的内部排序

  9. NC15979 小q的数列

    NC15979 小q的数列 题目 题目描述 小q最近迷上了各种好玩的数列,这天,他发现了一个有趣的数列,其递推公式如下: \[f[i] = \left\{ \begin{array}{l} 0 ,&a ...

  10. Grammarly for Chrome-语法、用词自动检查

    从语法和拼写到风格和语气,Grammarly帮助你消除写作错误,找到完美的词语来表达自己.当你在Gmail.Twitter.LinkedIn和几乎任何你发现自己在写作的地方写作时,你都会从Gramma ...