有点权的重心,拆掉点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. 负载均衡之LVS的三种模式

    模式一:D-NAT模式 原理:此模式类似NAT网络中,所以此网络内主机发到互联网上的数据包的源目的IP都是NAT路由的IP,在NAT路由上做了IP替换. 把客户端发来的数据的IP头的目的地址在负载均衡 ...

  2. 解决python 导入selenium 库后自动化运行成功但是报错问题

    本章节开始进入自动化的基础教学了,首先我们要对我们的工具有一定的熟练使用程度,做自动化常用的工具一个是搭建 RobotFramework自动化框架,另外一个便是我们最常用的python 工作原理是比较 ...

  3. Git使用 - 忽略特定文件 - gitignore

    1. 背景 2. 创建.gitignore 文件 3. 文件内容样式 4. exclude文件 5. gitignore 文件模板 6. 参考文档 1. 背景 前提知识:在工作目录下的每一个文件都不外 ...

  4. ClickHouse(02)ClickHouse架构设计介绍概述与ClickHouse数据分片设计

    ClickHouse核心架构设计是怎么样的?ClickHouse核心架构模块分为两个部分:ClickHouse执行过程架构和ClickHouse数据存储架构,下面分别详细介绍. ClickHouse执 ...

  5. 「非软文」零基础学习TypeScript(源码开源)

    今天,这篇文章篇幅很短,主要开放我最近学习整理TypeScript源码. 源码地址 https://github.com/maomincoding/typeScript_study 更多内容请见原文, ...

  6. 贝壳自动化测试平台sosotest 学习记录

    手工测试VS自动化测试 用例执行: 手动执行 自动执行 是否需要些脚本: 需要 不需要 测试报告生成: 手动 自动 常见的测试技术 关键字驱动的测试框架 RobotFRamework 单元测试框架 自 ...

  7. RS485通信电路

    RS485由RS232和RS422发展而来,弥补了抗干扰能力差.通信距离短.速率低的缺点,增加了多点.双向通信能力,即允许多个发送器连接在同一条主线上,同时增加了发送器的驱动能力和冲突保护特性,扩展了 ...

  8. NIO.2中Path、 Paths、Files类的使用

  9. labview从入门到出家6(进阶篇)--移位寄存器的使用

    前面介绍了如何熟悉和使用Labview自带的库函数以及调试方式,大家后期基本可以凭借这两个方式从入门到出家了,哈哈,后面就靠各位同仁99%的努力了.这篇为啥要讲移位寄存器呢,主要是之前做的项目和经验告 ...

  10. 选择结构-单if语句和标准if else语句

    判断语句1--if if语句第一种格式: if if(关系表达式){ 语句体; } 执行流程 首先判断关系表达式看其结果是true还是false 如果是true就执行语句体 如果是false就不执行语 ...