[洛谷P2986][USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目大意:给你一棵树,每个点有点权,边有边权,求一个点,使得其他所有点到这个点的距离和最短,输出这个距离
题解:树形$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…的更多相关文章
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…(树规)
题目描述 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 ...
- BZOJ 1827 洛谷 2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gather
[题解] 很容易想到暴力做法,枚举每个点,然后对于每个点O(N)遍历整棵树计算答案.这样整个效率是O(N^2)的,显然不行. 我们考虑如果已知当前某个点的答案,如何快速计算它的儿子的答案. 显然选择它 ...
- P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- 【题解】Luogu p2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat 树型dp
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- LUOGU P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
传送门 解题思路 首先第一遍dfs预处理出每个点的子树的siz,然后可以处理出放在根节点的答案,然后递推可得其他答案,递推方程 sum[u]=sum[x]-(val[i]*siz[u])+(siz[1 ...
- [USACO10MAR]伟大的奶牛聚集Great Cow Gat…【树形dp】By cellur925
题目传送门 首先这道题是在树上进行的,然后求最小的不方便程度,比较符合dp的性质,那么我们就可以搞一搞树形dp. 设计状态:f[i]表示以i作为聚集地的最小不方便程度.那么我们还需要各点间的距离,但是 ...
- [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- [USACO10MAR]伟大的奶牛聚集Great Cow Gat… ($dfs$,树的遍历)
题目链接 Solution 辣鸡题...因为一个函数名看了我贼久. 思路很简单,可以先随便指定一个根,然后考虑换根的变化. 每一次把根从 \(x\) 换成 \(x\) 的一个子节点 \(y\),记录一 ...
随机推荐
- 神级编辑器 sublime text 和 神级插件 emmet
h1{foo}和a[href=#] 生成如下代码 <h1>foo</h1> <a href="#"></a> 嵌套的使用 > ...
- vm 中 centOS 7 固定ip设置
虚拟机中,centOS通过NAT连接,设置固定IP上网. 本地主机 VMware Network Adapter VMnet8 状态信息: 描述: VMware Virtual Ethernet A ...
- Leecode刷题之旅-C语言/python-13.罗马数字转整数
/* * @lc app=leetcode.cn id=13 lang=c * * [13] 罗马数字转整数 * * https://leetcode-cn.com/problems/roman-to ...
- AWS安装CDH5.3-CentOS6.4中关键操作步骤
1.在AWS masternode 上下载cloudera-manager-installer.bin安装包 [root@ip-172-21-42-114 ~]# wget http://archiv ...
- [转]URL传中文参数导致乱码的解决方案之encodeURI
通过URL传中文参数时,在服务端后台获取到的值往往会出现乱码.解决方案有很多种.本文介绍如何通过encodeURI来解决中文乱码问题. 首先,在前端页面准备参数的时候,需要对中文参数进行encode处 ...
- Hibernate-ORM:16.Hibernate中的二级缓存Ehcache的配置
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲述Hibernate中的二级缓存的配置,作者将使用的是ehcache缓存 一,目录 1.二级缓存的具 ...
- django序列化时间
具体代码: import json,time,datetime lis ={'time':datetime.date.today(),"username":"zhilei ...
- 使用FPGA开发板驱动VGA显示器
1. 本次使用的是cyclone4开发板,先看下原理图,因为右边的RGB应该是模拟信号量,但是本次例程只接了3根线,那就是说颜色只有8种. 2. 代码,输出信号有R,G,B三色,就是上图右边的,行同步 ...
- 《剑指offer》题解
有段时间准备找工作,囫囵吞枣地做了<剑指offer>提供的编程习题,下面是题解收集. 当初没写目录真是个坏习惯(-_-)||,自己写的东西都要到处找. 提交的源码可以在此repo中找到:h ...
- kindeditor 限制上传图片大小及宽高
进入:/kindeditor/plugins/image/image.js,替换其中的 self.plugin.imageDialog = function (options)方法,代码为: self ...