Luogu2986 [USACO10MAR]伟大的奶牛聚集 (树形DP)
有点权的重心,拆掉点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)的更多相关文章
- [USACO10MAR] 伟大的奶牛聚集 - 树形dp
每个点有重数,求到所有点距离最小的点 就是魔改的重心了 #include <bits/stdc++.h> using namespace std; #define int long lon ...
- [USACO10MAR]伟大的奶牛聚集
[USACO10MAR]伟大的奶牛聚集 Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会. 每个奶牛居住在 N(1<=N& ...
- [USACO10MAR]伟大的奶牛聚集Great Cow Gat…【树形dp】By cellur925
题目传送门 首先这道题是在树上进行的,然后求最小的不方便程度,比较符合dp的性质,那么我们就可以搞一搞树形dp. 设计状态:f[i]表示以i作为聚集地的最小不方便程度.那么我们还需要各点间的距离,但是 ...
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…(树规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- 【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] ...
- [bzoj2060][Usaco2010 Nov]Visiting Cows 拜访奶牛_树形dp
Visiting Cows 拜访奶牛 bzoj-2060 Usaco-2010 Nov 题目大意:题目链接. 注释:略. 想法:看起来像支配集. 只是看起来像而已. 状态:dp[pos][flag]表 ...
- [洛谷P2986][USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目大意:给你一棵树,每个点有点权,边有边权,求一个点,使得其他所有点到这个点的距离和最短,输出这个距离 题解:树形$DP$,思路清晰,转移显然 卡点:无 C++ Code: #include < ...
- [USACO10MAR]伟大的奶牛聚集 BZOJ 1827 树形dp+dfs
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集(树形动规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
随机推荐
- 动态调试JS脚本文件:(JS源映射 - sourceURL)与 debugger
我们在进行js调试时经常会对js进行调试,chrome 对js提示对支持非常友好,只需要F12就可以打开chrome的调试器 在sources里面就是页面请求后加载的一些资源文件,我们可以找到我们的j ...
- 行列式&矩阵树定理
行列式: 参考 oi-wiki 定义 对于一个\(n*n\)的矩阵A行列式取值(标量) \(det(A)=|A|=\sum\limits_p(-1)^{\tau(p)}\prod\limits_{i= ...
- 差分优化建边(Tax)
[Luogu P6822PA2012]Tax] (http://www.luogu.com.cn/problem/P6822") All right. Let's go! 题目描述 给出一个 ...
- Flink整合面向用户的数据流SDKs/API(Flink关于弃用Dataset API的论述)
动机 Flink提供了三种主要的sdk/API来编写程序:Table API/SQL.DataStream API和DataSet API.我们认为这个API太多了,建议弃用DataSet API,而 ...
- 2021.03.20【NOIP提高B组】模拟 总结
区间 DP 专场:愉快爆炸 T1 题目大意 有 \(n\) 个有颜色的块,连续 \(k\) 个相同颜色的就可以消掉 现在可以在任意位置插入任意颜色的方块,问最少插入多少个可以全部抵消 题解 先把连续的 ...
- Windows系统开启显示文件名后缀
更新记录 2022年4月16日:本文迁移自Panda666原博客,原发布时间:2021年8月26日. 通常Windows系统根据文件名称的后缀来确定文件的类型.经常让朋友出现软件方面的问题,让其修改一 ...
- C++ 炼气期之算术运算符
1. 前言 编写程序时,数据确定后,就需要为数据提供相应的处理逻辑(方案或算法).所谓逻辑有 2 种存在形态: 抽象形态:存在于意识形态,强调思考过程,与具体的编程语言无关. 具体形态:通过代码来实现 ...
- Java 泛型中的通配符
本文内容如下: 1. 什么是类型擦除 2.常用的 ?, T, E, K, V, N的含义 3.上界通配符 < ?extends E> 4.下界通配符 < ?super E> 5 ...
- mysql-安装(windows版本)与登录
安装mysql 1.MySQL版本 mysql-5.6.35-winx64.zip 2.首先解压到安装目录 3.修改配置文件 复制my-default.ini 重命名为my.ini 然后修改mysql ...
- python小题目练习(十一)
题目:大乐透号码生成器 需求:使用Random模块模拟大乐透号码生成器,选号规则为:前区在1 ~ 35的范围内随机产生不重复 的5个号码,后区在1~ 12的范围内随机产生不重复的2个号码.效果如图8. ...