[USACO10MAR]伟大的奶牛聚集
[USACO10MAR]伟大的奶牛聚集
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找出最方便的地点来举行大集会。 ——by洛谷(感谢洛谷少有的良心翻译)
http://daniu.luogu.org/problem/show?pid=2986
建图,然后把她当做以任意点为根的树,然后很容易想用树DP。我们发现a与其父节点b;a为集合点的路径有两类:
- 直接到a;(我们把到a路径符合此类的点集记为A);
- 先到b;(我们把到a路径符合此类的点集记为B);
于是当我们知道f[b]时,f[a]即为在f[b]的基础上A中点不必走a->b,B中点要再走b->a,而A即是a的子树点集;
得方程:
f[a]=f[fa[a]]-tree[a]*dis(a->b)+(tree[root]-tree[a])*dis(a->b);
(想象所有点先聚集于b,再全走到a,其中a的子树上节点多走了,故减去)
代码如下:
#include<cstdio>
using namespace std;
int n;
int c[];
long long f1[],f[];
struct ss
{
int next,to,dis;
}x[];
int first[],num;
long long all;
void build(int f,int t,int d)
{
x[++num].next=first[f];
x[num].to=t;
x[num].dis=d;
first[f]=num;
}
long long dfs(int ,int );
void dp(int ,int ,int ); int main()
{
scanf("%d",&n);
int i,j,k,l;
for(i=;i<=n;i++)
scanf("%d",&c[i]),all+=c[i];
for(i=;i<=n-;i++)
{
scanf("%d%d%d",&j,&k,&l);
build(j,k,l);
build(k,j,l);
}
f[]=dfs(,-);
dp(,,);
all=;
for(i=;i<=n;i++)
if(f[i]<all)
all=f[i];
printf("%lld",all);
return ;
} long long dfs(int fa,int last)
{
int j;
long long sum=;
j=first[fa];
f1[fa]=c[fa];
while(j)
{
if(x[j].to!=last)
{
sum+=dfs(x[j].to,fa)+x[j].dis*f1[x[j].to];
f1[fa]+=f1[x[j].to];
}
j=x[j].next;
}
return sum;
}
void dp(int fa,int last,int di)
{
int j;
f[fa]=f[last]-f1[fa]*di+(all-f1[fa])*di;
j=first[fa];
while(j)
{
if(x[j].to!=last)
dp(x[j].to,fa,x[j].dis);
j=x[j].next;
}
}
祝AC哟;
[USACO10MAR]伟大的奶牛聚集的更多相关文章
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集(树形动规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- 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…(树规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- [USACO10MAR]伟大的奶牛聚集 BZOJ 1827 树形dp+dfs
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- [USACO10MAR]伟大的奶牛聚集Great Cow Gat…【树形dp】By cellur925
题目传送门 首先这道题是在树上进行的,然后求最小的不方便程度,比较符合dp的性质,那么我们就可以搞一搞树形dp. 设计状态:f[i]表示以i作为聚集地的最小不方便程度.那么我们还需要各点间的距离,但是 ...
- BZOJ 1827 洛谷 2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gather
[题解] 很容易想到暴力做法,枚举每个点,然后对于每个点O(N)遍历整棵树计算答案.这样整个效率是O(N^2)的,显然不行. 我们考虑如果已知当前某个点的答案,如何快速计算它的儿子的答案. 显然选择它 ...
- [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- P2986 [USACO10MAR]伟大的奶牛聚集(思维,dp)
题目描述 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 ...
随机推荐
- CentOS PPTP配置LNMP+PPTP+FreeRADIUS+DaloRADIUS+流量控制
折腾了好几天,查阅了很多资料,终于搞定了,泪牛满面,下面记录详细操作过程!注:测试环境为CENTOS5.8 x86 安装PPTP 直接使用赵蓉的PPTP一键安装包即可 wget http://dl.z ...
- jQuery滑动导航菜单
js: $(function(){ $("ul.sub").parent().append("<span></span>"); $(&q ...
- 关于对js的this的几点理解
四种不同模式小调用的指向 1.函数调用模式的时候,this指向window 2.方法调用模式的时候,this指向方法所在的对象 3.构造函数模式的时候,this指向新生成的实例 4.apply/cal ...
- MongoDB实战指南(一):大数据与云计算
1.1 什么大数据 具体来说,大数据技术涉及到数据的创造,存储,获取和分析,大数据的主要特点有下面几个: 数据量大.一个典型的PC机载2000年前后其存储空间可能有10GB,今天facebook一天增 ...
- 156. Binary Tree Upside Down
题目: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node ...
- Layout Resource官方教程(3)在layout中用include嵌入其它layout
简介 <include>Includes a layout file into this layout. 类似 #include ,把layout展开在include处 attribute ...
- ActionBar官方教程(5)ActionBar的分裂模式(底部tab样式),隐藏标题,隐藏图标
Using split action bar Split action bar provides a separate bar at the bottom of the screen to displ ...
- ipconfig命令
C:\Windows\System32>ipconfig -all Windows IP 配置 主机名 . . . . . . . . . . . . . : LuJunTao 主 DNS 后缀 ...
- 屯题50AC纪念
从2.1起开始屯题,一直弄到现在才完成了一发50题的目标,实在太弱 (当然之间事比较多,还是挺不容易的) 不过总算是完成了一个小的目标了 接下来两周要进行小高考最后冲刺了,所以我大概不会再怎么刷题了 ...
- NOI2008假面舞会
1064: [Noi2008]假面舞会 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 883 Solved: 462[Submit][Status] ...