有点权的重心,拆掉点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. mybatis 转义符号

    < <= > >= & ' " < <= > >= & &apos; "

  2. 『忘了再学』Shell基础 — 29、AWK内置变量

    目录 1.AWK内置变量 2.练习说明 (1)$n变量练习 (2)FS变量练习 (3)NF变量和NR变量练习 3.总结: 1.AWK内置变量 AWK内置变量如下表: awk内置变量 作用 $0 代表目 ...

  3. 【2022-06-16】Python解释器的下载安装与使用

    一.Python解释器介绍 什么是Python解释器? Python是一门解释型语言,解释器是Python运行必不可少的一种工具.所以,我们搭建Python环境,本质上就是对Python进行配置和定制 ...

  4. flink处理延迟

    flink处理延迟 flink主要是处理实时数据的,在处理实时数据的过程中,难免会遇到乱序的存在.以事件时间举例,先发生的事件后到处理算子.flink针对乱序数据的处理主要有三种方式: 拨慢水位线的生 ...

  5. tensorflow版本的bert模型 GPU的占用率为100%而其利用率为0%

    Notice: 本方法只是解决问题的一种可能,不一定百分百适用,出现这个问题还有很多其他原因,这个可以作为解决的一种尝试!!! 经过检查发现,是由于激活环境的原因 使用 conda activate ...

  6. JavaGUI——Java图形用户界面

    1.Java GUI 概述 GUI(Graphical User Interface,简称 GUI,图形用户界面)是指采用图形方式显示的计算机操作用户界面,与早期计算机使用的命令行界面相比,图形界面对 ...

  7. ABAP CDS - Annotations 注解

    Syntax ... annotation[.annotation1[.annotation2]][:value]  ... Effect Annotation that can be specifi ...

  8. mysql-安装(windows版本)与登录

    安装mysql 1.MySQL版本 mysql-5.6.35-winx64.zip 2.首先解压到安装目录 3.修改配置文件 复制my-default.ini 重命名为my.ini 然后修改mysql ...

  9. Nginx通过bat文件快速启动停止

    新建文本文件NginxRun.bat.(名字无所谓,后缀名得是bat) 将以下代码复制到bat文件中即可. @echo off ::进入D盘 d: ::进入nginx目录 这里是自己的nginx目录 ...

  10. W10修改被改的默认打开文本方式

    今天不小心给默认方式打开点错了,身为处女座的我有洁癖非要非恢复过来,这是找到的 原文操作出处:win10系统如何还原文件默认打开方式?win10设置文件默认打开方式的方法 -Win7系统之家 (win ...