题目大意:给你一棵树,每个点有点权,边有边权,求一个点,使得其他所有点到这个点的距离和最短,输出这个距离

题解:树形$DP$,思路清晰,转移显然

卡点:

C++ Code:

#include <cstdio>
#include <algorithm>
#define maxn 100010
const long long inf = 0x3f3f3f3f3f3f3f3f;
int head[maxn], cnt;
struct Edge {
int to, nxt, w;
} e[maxn << 1];
inline void add(int a, int b, int c) {
e[++cnt] = (Edge) {b, head[a], c}; head[a] = cnt;
} int n, sum;
int w[maxn], sz[maxn];
long long f[maxn], ans;
void dfs(int u, int fa = 0) {
sz[u] = w[u];
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v != fa) {
dfs(v, u);
sz[u] += sz[v];
f[u] += f[v] + static_cast<long long> (sz[v]) * e[i].w;
}
}
}
void dfs1(int u, int fa = 0) {
ans = std::min(ans, f[u]);
// printf("%d: %lld\n", u, f[u]);
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v != fa) {
f[v] = f[u] - static_cast<long long> (sz[v]) * e[i].w + static_cast<long long> (sum - sz[v]) * e[i].w;
// printf("%d -> %d : %d %d %lld %lld\n", u, v, sz[v], e[i].w, f[u], f[v]);
dfs1(v, u);
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", w + i), sum += w[i];
for (int i = 1, a, b, c; i < n; i++) {
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
add(b, a, c);
}
dfs(1);
ans = f[1];
__builtin_memset(f, 0, sizeof f); f[1] = ans;
dfs1(1);
printf("%lld\n", ans);
return 0;
}

  

[洛谷P2986][USACO10MAR]伟大的奶牛聚集Great Cow Gat…的更多相关文章

  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]伟大的奶牛聚集(树形动规)

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

  3. BZOJ 1827 洛谷 2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gather

    [题解] 很容易想到暴力做法,枚举每个点,然后对于每个点O(N)遍历整棵树计算答案.这样整个效率是O(N^2)的,显然不行. 我们考虑如果已知当前某个点的答案,如何快速计算它的儿子的答案. 显然选择它 ...

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

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

  5. 【题解】Luogu p2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat 树型dp

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

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

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

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

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

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

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

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

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

随机推荐

  1. mybatis报错:sql中有条件语句时出现属性没有getter的异常

    Mybatis问题:在使用条件语句动态设置SQL语句时出现如下错误 Caused by: org.apache.ibatis.reflection.ReflectionException: There ...

  2. 微信小程序缓存

    购物车数据加入缓存,相同的商品值修改数量,然后再次加入缓存中 修改购物车的数据的时候同理,都是修改缓存数据然后加入到缓存中. 具体的使用方法看官方文档,我只是提供思路

  3. python集合、函数实例

    集合 1.list ==>允许重复的集合,可修改 2.tuple ==>允许重复的集合,不可修改 3.dict ==> 4.set ==>不允许重复的集合,相当于不可重复的列表 ...

  4. go执行外部应用

    go执行外部应用 最近想将原来用asp.net mvc写的一个公司内部网站改用beego来写,但发现其中有一个功能是,能将加密的sqlite文件进行解密,因为这个解密是不能公开的,但有些同事需要查看这 ...

  5. 面试-MySQL总结

    三范式 三范式定义(范式和反范式) 1NF:每个数据项都是最小单元,不可分割,确定行列之后只能对应一个数据. 2NF:每一个非主属性完全依赖于候选码(属性组的值能唯一的标识一个元组,但是其子集不可以) ...

  6. android去掉button默认的点击阴影

    查了资料,发现别人都是说加一个style属性. style="?android:attr/borderlessButtonStyle" 加上了确实管用,但是我绝不是不求甚解的人.追 ...

  7. springmvc+mybatis的两种配置和应用方式

    一.不用写dao层实现的方式 1.导入依赖包,我的pom.xml文件配置如下: <project xmlns="http://maven.apache.org/POM/4.0.0&qu ...

  8. AD-Powershell for Active Directory Administrators

    Table of Contents   Computer object commands Group object commands Organizational Unit (OU) commands ...

  9. jmeter添加自定义扩展函数之图片base64编码

    打开eclipse,新建maven工程,在pom中引入jmeter核心jar包: <!-- https://mvnrepository.com/artifact/org.apache.jmete ...

  10. 【APUE】Chapter5 Standard I/O Library

    5.1 Introduction 这章介绍的standard I/O都是ISOC标准的.用这些standard I/O可以不用考虑一些buffer allocation.I/O optimal-siz ...