洛谷 P2986 [USACO10MAR]Great Cow Gat…(树形dp+容斥原理)
P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目描述
Bessie is planning the annual Great Cow Gathering for cows all across the country and, of course, she would like to choose the most convenient location for the gathering to take place.
Each cow lives in one of N (1 <= N <= 100,000) different barns (conveniently numbered 1..N) which are connected by N-1 roads in such a way that it is possible to get from any barn to any other barn via the roads. Road i connects barns A_i and B_i (1 <= A_i <= N; 1 <= B_i <= N) and has length L_i (1 <= L_i <= 1,000). The Great Cow Gathering can be held at any one of these N barns. Moreover, barn i has C_i (0 <= C_i <= 1,000) cows living in it.
When choosing the barn in which to hold the Cow Gathering, Bessie wishes to maximize the convenience (which is to say minimize the inconvenience) of the chosen location. The inconvenience of choosing barn X for the gathering is the sum of the distances all of the cows need to travel to reach barn X (i.e., if the distance from barn i to barn X is 20, then the travel distance is C_i*20). Help Bessie choose the most convenient location for the Great Cow
Gathering.
Consider a country with five barns with [various capacities] connected by various roads of varying lengths. In this set of barns, neither barn 3 nor barn 4 houses any cows.
1 3 4 5
@--1--@--3--@--3--@[2]
[1] |
2 | @[1] 2 Bessie can hold the Gathering in any of five barns; here is the table of inconveniences calculated for each possible location:
Gather ----- Inconvenience ------
Location B1 B2 B3 B4 B5 Total
1 0 3 0 0 14 17
2 3 0 0 0 16 19
3 1 2 0 0 12 15
4 4 5 0 0 6 15
5 7 8 0 0 0 15
If Bessie holds the gathering in barn 1, then the inconveniences from each barn are:
Barn 1 0 -- no travel time there!
Barn 2 3 -- total travel distance is 2+1=3 x 1 cow = 3 Barn 3 0 -- no cows there!
Barn 4 0 -- no cows there!
Barn 5 14 -- total travel distance is 3+3+1=7 x 2 cows = 14 So the total inconvenience is 17.
The best possible convenience is 15, achievable at by holding the Gathering at barns 3, 4, or 5.
Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会。当然,她会选择最方便的地点来举办这次集会。
每个奶牛居住在 N(1<=N<=100,000) 个农场中的一个,这些农场由N-1条道路连接,并且从任意一个农场都能够到达另外一个农场。道路i连接农场A_i和B_i(1 <= A_i <=N; 1 <= B_i <= N),长度为L_i(1 <= L_i <= 1,000)。集会可以在N个农场中的任意一个举行。另外,每个牛棚中居住者C_i(0 <= C_i <= 1,000)只奶牛。
在选择集会的地点的时候,Bessie希望最大化方便的程度(也就是最小化不方便程度)。比如选择第X个农场作为集会地点,它的不方便程度是其它牛棚中每只奶牛去参加集会所走的路程之和,(比如,农场i到达农场X的距离是20,那么总路程就是C_i*20)。帮助Bessie找出最方便的地点来举行大集会。
输入输出格式
输入格式:
Line 1: A single integer: N
Lines 2..N+1: Line i+1 contains a single integer: C_i
- Lines N+2..2*N: Line i+N+1 contains three integers: A_i, B_i, and L_i
输出格式:
- Line 1: The minimum inconvenience possible
输入输出样例
5
1
1
0
0
2
1 3 1
2 3 2
3 4 3
4 5 3
15
/*
树形dp+容斥原理
f[i]表示i为关键点答案
dis[i]表示以i为根子树和,第一遍树形dp统计
num[i]表示点权和,tot为总点权和
容斥原理: f[v]=f[now]+(tot-num[v])*e[i].d-num[v]*e[i].d; 第二遍树形dp统计
由父亲节点转移到儿子节点 容斥比较难想一些,建议画图比照代码理解
一定明确要求Σ点权*边权最小,容斥的时候注意计算的是那个点的点权*哪条边的边权
*/
#include<iostream>
#include<cstdio>
#include<cstring> #define N 100007 using namespace std;
int head[N],w[N];
long long f[N],num[N],dis[N],tot;
int n,m,x,y,z,cnt;
struct edge
{
int u,to,pre,d;
}e[N<<]; inline void add(int u,int to,int d)
{
e[++cnt].to=to;e[cnt].d=d;e[cnt].pre=head[u];head[u]=cnt;
} void tree_dp1(int fa,int now)
{
num[now]=w[now];
for(int i=head[now];i;i=e[i].pre)
{
if(e[i].to==fa) continue;
tree_dp1(now,e[i].to);
num[now]+=num[e[i].to];
dis[now]+=dis[e[i].to]+num[e[i].to]*e[i].d;
}
} long long mn=999999999999999LL; void tree_dp2(int fa,int now)
{
mn=min(mn,f[now]);
for(int i=head[now];i;i=e[i].pre)
{
int v=e[i].to;
if(v==fa) continue;
f[v]=f[now]+(tot-num[v])*e[i].d-num[v]*e[i].d;
tree_dp2(now,v);
}
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&w[i]),tot+=w[i];
for(int i=;i<n;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);add(y,x,z);
}
tree_dp1(,);
f[]=dis[];
tree_dp2(,);
printf("%lld\n",mn);
return ;
}
洛谷 P2986 [USACO10MAR]Great Cow Gat…(树形dp+容斥原理)的更多相关文章
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…(树规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- [洛谷P2986][USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目大意:给你一棵树,每个点有点权,边有边权,求一个点,使得其他所有点到这个点的距离和最短,输出这个距离 题解:树形$DP$,思路清晰,转移显然 卡点:无 C++ Code: #include < ...
- 洛谷 P2015 二叉苹果树(codevs5565) 树形dp入门
dp这一方面的题我都不是很会,所以来练(xue)习(xi),大概把这题弄懂了. 树形dp就是在原本线性上dp改成了在 '树' 这个数据结构上dp. 一般来说,树形dp利用dfs在回溯时进行更新,使用儿 ...
- 洛谷 P3267 - [JLOI2016/SHOI2016]侦察守卫(树形 dp)
洛谷题面传送门 经典题一道,下次就称这种"覆盖距离不超过 xxx 的树形 dp"为<侦察守卫模型> 我们考虑树形 \(dp\),设 \(f_{x,j}\) 表示钦定了 ...
- 洛谷 P7163 - [COCI2020-2021#2] Svjetlo(树形 dp)
洛谷题面传送门 神仙级别的树形 dp. u1s1 这种代码很短但巨难理解的题简直是我的梦魇 首先这种题目一看就非常可以 DP 的样子,但直接一维状态的 DP 显然无法表示所有情况.注意到对于这类统计一 ...
- 洛谷P1352 没有上司的舞会——树形DP
第一次自己写树形DP的题,发个博客纪念`- 题目来源:P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结 ...
- 洛谷 P1453 城市环路 ( 基环树树形dp )
题目链接 题目背景 一座城市,往往会被人们划分为几个区域,例如住宅区.商业区.工业区等等.B市就被分为了以下的两个区域--城市中心和城市郊区.在着这两个区域的中间是一条围绕B市的环路,环路之内便是B市 ...
- C++ 洛谷 P2458 [SDOI2006]保安站岗 from_树形DP
P2458 [SDOI2006]保安站岗 没学树形DP的,看一下. 题目大意:一棵树有N个节点,现在需要将所有节点都看守住,如果我们选择了节点i,那么节点i本身,节点i的父亲和儿子都会被看守住. 每个 ...
- 洛谷P2279 消防局的设立【树形dp】
题目:https://www.luogu.org/problemnew/show/P2279 题意:一棵树.在节点处建消防站,可以覆盖与他距离在2之内的节点.问最少要建多少个消防站,可以覆盖所有的节点 ...
随机推荐
- 用Grunt进行CSS文件压缩
假设你的项目的CSS文件全部放在项目目录下名为css的文件夹中,现在将它压缩合并成一个名为main-min.css的文件,放在css-min文件夹下. (1)首先保证机器安装了node.js. (2) ...
- (转) Hibernate框架基础——操纵持久化对象的方法(Session中)
http://blog.csdn.net/yerenyuan_pku/article/details/52761021 上一篇文章中我们学习了Hibernate中java对象的状态以及对象的状态之间如 ...
- (转)Hibernate关联映射——一对多(多对一)
http://blog.csdn.net/yerenyuan_pku/article/details/70152173 Hibernate关联映射——一对多(多对一) 我们以客户(Customer)与 ...
- linux搭建mysql服务器及可视化工具
环境: ubutnu 18.4 mysql 5.7 参考: 安装 https://www.cnblogs.com/opsprobe/p/9126864.html 配置用户权限 https://baij ...
- c#中动态创建textbox并且从数据库中获取表中数据添加到textbox中
private void FormLugOther_Load(object sender, EventArgs e) { foreach (string str in FormLug.FieldLis ...
- strcmp 与 _tcscmp
strcmp 用来比较ANSI字符串,而_tcscmp用来比较UNICODE(宽字符)的字符串.ANSI字符串中,1个英文字母为1个字节,1个中文字符为2个字节,遇到0字符表示字符串结束.而在UNIC ...
- Spring Boot 集成 JWT 实现单点登录授权
使用步骤如下:1. 添加Gradle依赖: dependencies { implementation 'com.auth0:java-jwt:3.3.0' implementation('org.s ...
- linux常用命令--ubuntu
linux 操作系统 一.linux 操作系统概述 二.安装linux系统 三.linux系统环境 ubuntu,默认有6个命令交互通道和一个图形界面交互通道,默认进入到的是图形界面通道 命令交互模式 ...
- SqlServer Function
set quoted_identifier on; set ansi_nulls on; go create function [dbo].[Get_StrArrayStrOfIndex] ( @st ...
- copy the content of a file muliptle times and save as ordered files:
input: transient.case outputs: transient_1.case, transient_2.case,...transient_101.case ********** n ...