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

输入输出样例

输入样例#1:

5
1
1
0
0
2
1 3 1
2 3 2
3 4 3
4 5 3
输出样例#1:

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+容斥原理)的更多相关文章

  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]伟大的奶牛聚集Great Cow Gat…

    题目大意:给你一棵树,每个点有点权,边有边权,求一个点,使得其他所有点到这个点的距离和最短,输出这个距离 题解:树形$DP$,思路清晰,转移显然 卡点:无 C++ Code: #include < ...

  3. 洛谷 P2015 二叉苹果树(codevs5565) 树形dp入门

    dp这一方面的题我都不是很会,所以来练(xue)习(xi),大概把这题弄懂了. 树形dp就是在原本线性上dp改成了在 '树' 这个数据结构上dp. 一般来说,树形dp利用dfs在回溯时进行更新,使用儿 ...

  4. 洛谷 P3267 - [JLOI2016/SHOI2016]侦察守卫(树形 dp)

    洛谷题面传送门 经典题一道,下次就称这种"覆盖距离不超过 xxx 的树形 dp"为<侦察守卫模型> 我们考虑树形 \(dp\),设 \(f_{x,j}\) 表示钦定了 ...

  5. 洛谷 P7163 - [COCI2020-2021#2] Svjetlo(树形 dp)

    洛谷题面传送门 神仙级别的树形 dp. u1s1 这种代码很短但巨难理解的题简直是我的梦魇 首先这种题目一看就非常可以 DP 的样子,但直接一维状态的 DP 显然无法表示所有情况.注意到对于这类统计一 ...

  6. 洛谷P1352 没有上司的舞会——树形DP

    第一次自己写树形DP的题,发个博客纪念`- 题目来源:P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结 ...

  7. 洛谷 P1453 城市环路 ( 基环树树形dp )

    题目链接 题目背景 一座城市,往往会被人们划分为几个区域,例如住宅区.商业区.工业区等等.B市就被分为了以下的两个区域--城市中心和城市郊区.在着这两个区域的中间是一条围绕B市的环路,环路之内便是B市 ...

  8. C++ 洛谷 P2458 [SDOI2006]保安站岗 from_树形DP

    P2458 [SDOI2006]保安站岗 没学树形DP的,看一下. 题目大意:一棵树有N个节点,现在需要将所有节点都看守住,如果我们选择了节点i,那么节点i本身,节点i的父亲和儿子都会被看守住. 每个 ...

  9. 洛谷P2279 消防局的设立【树形dp】

    题目:https://www.luogu.org/problemnew/show/P2279 题意:一棵树.在节点处建消防站,可以覆盖与他距离在2之内的节点.问最少要建多少个消防站,可以覆盖所有的节点 ...

随机推荐

  1. JavaScript的基础数据类型和表达式

    Java Script的基础数据类型和表达式 基本的数据类型: number(数值)类型:可分为整数和浮点数 string(字符)类型:是用单引号“'”或者双引号“"”来说明的. boole ...

  2. (转)学习淘淘商城第二十二课(KindEditor富文本编辑器的使用)

    http://blog.csdn.net/u012453843/article/details/70184155 上节课我们一起学习了怎样解决KindEditor富文本编辑器上传图片的浏览器兼容性问题 ...

  3. MFC TAB控件顺序

    在MFC中添加控件后,按Ctrl+d可以改变控件TAB顺序,怕自己忘了,一个神奇的东西,记下. 关于改变Tab顺序的方法有以下几种: 方法一:在动态创建控件的时候STYLE设置成为WS_CHILD|W ...

  4. centOS Linux下用yum安装mysql

    centOS Linux下用yum安装mysql      第一篇:安装和配置MySQL   第一步:安装MySQL   [root@192 local]# yum -y install mysql- ...

  5. 惊了!!! 小白零基础学java (月薪过万是你的梦想嘛) 手把手教学 就怕你不动手【二十五】第二章【初识MySQL】

    初识MySQL1. 了解主流的数据库和数据库分类1.1 数据库概念数据库:按照数据结构来组织.存储和管理数据的一种建立在计算机存储设备上的仓库. 数据库的优势: 1. 可以持久化存储大量的数据.方便我 ...

  6. 哈夫曼树(Huffman Tree)

    Date:2019-06-21 14:42:04 做题时更多的是用到哈夫曼树的构造思想,即按照问题规模从小到大,依次解决问题,可以得到最优解 Description: 在一个果园里,多多已经将所有的果 ...

  7. js的StringBuffer类

    function StringBuffer(str){ var arr = []; str = str || ""; arr.push(str); this.append = fu ...

  8. vue-router的基本使用和配置

    1.在main.js文件中引入相关模块以及组件及实例化vue对象配置选项路由及渲染App组件 默认设置如下: import Vue from 'vue' import App from './App' ...

  9. 洛谷——P2504 [HAOI2006]聪明的猴子

    P2504 [HAOI2006]聪明的猴子 题目描述 在一个热带雨林中生存着一群猴子,它们以树上的果子为生.昨天下了一场大雨,现在雨过天晴,但整个雨林的地表还是被大水淹没着,部分植物的树冠露在水面上. ...

  10. 名字竞技场 V3.0

    更新内容 1.加入新boss,更高的难度. 2.支持组队模式勒! 3.针对大家反应的人物属性算法进行了修改,现在人物属性更多的取决于名字而不是随机数 4.用户界面优化 INF.代码拿走赞留下,不然你赢 ...