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. (转)分布式文件存储FastDFS(五)FastDFS常用命令总结

    http://blog.csdn.net/xingjiarong/article/details/50561471 1.启动FastDFS tracker: /usr/local/bin/fdfs_t ...

  2. codeforces_724C_Ray Tracing

    C. Ray Tracing time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  3. Android Binder机制(一) Binder的设计和框架

    这是关于Android中Binder机制的一系列纯技术贴.花了一个多礼拜的时间,才终于将其整理完毕.行文于此,以做记录:也是将自己所得与大家分享.和以往一样,介绍Binder时,先讲解框架,然后再从设 ...

  4. c# 图片资料

  5. C++入职学习篇--新员工入职(持续更新)

    C++入职学习篇--新员工入职(持续更新) 本人菜鸟一枚,刚刚结束学业生涯,入职C++软件开发岗位,之前对C++一窍不通,刚刚入职,亚历山大,但为祖国和平发展,本人励志为中华崛起而奋斗,学不好C++誓 ...

  6. opcache的配置

    ; Enable Zend OPcache extension module zend_extension=opcache.so ; Determines if Zend OPCache is ena ...

  7. hdu2010 水仙花数【C++】

    水仙花数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. Entity Framework Connection String不保留密码的方法

    添加Entity Data Model的时候,到最后一步,有两个radio box: 如果选择include sensitive data,虽然很方便,但是在web.config或者app.confi ...

  9. poj 1273最大流dinic算法模板

    #include<stdio.h> #include<string.h> #define N 300 #define inf 0x7fffffff #include<qu ...

  10. kendo grid结合ajax功能

    我感觉使用ajax结合表格绑定效率更好一些,可以灵活的控制点击前后的事件,现在grid前后的事件我不能控制