[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 ...
随机推荐
- asp.net推送
http://tech.it168.com/a2012/0210/1310/000001310252_all.shtml http://www.infoq.com/cn/news/2012/09/rc ...
- php PDO连接mysql以及字符乱码处理
<?php //mysql 的 PDO $dsn = "mysql:dbname=cqkx;host:localhost"; $username = "root&q ...
- UVA 11090 Going in Cycle!!
要求给定的图的中平均权值最小的环,注意处理自环的情况就能过了. 按照w1+w2+w3+….wn < n*ave的不等式,也就是(w1-ave) + (w2-ave) +…..(wn-ave) & ...
- edX Devstack 汉化(i18n)
操练了几日edx Devstack后,发现自己e文还是那么poor,如果和我一样,继续往下看,否则可以轻轻的飘过- 1.运行起 edx Devstack cd /devstack vagrant up ...
- glibc, eglibc和 glib的区别
http://blog.csdn.net/wind19/article/details/6082874
- RxJava开发精要2-为什么是Observables?
原文出自<RxJava Essentials> 原文作者 : Ivan Morgillo 译文出自 : 开发技术前线 www.devtf.cn 转载声明: 本译文已授权开发者头条享有独家转 ...
- velocity-1.7中vm文件的存放位置
velocity-1.7中关于vm文件存放 demo: public class App_example1 { public App_example1() { String propfile=&quo ...
- WCF - Architecture
WCF - Architecture WCF has a layered architecture that offers ample support for developing various d ...
- Lombok 安装、入门 - 消除冗长的 java 代码(转)
前言: 逛开源社区的时候无意发现的,用了一段时间,觉得还可以,特此推荐一下. lombok 提供了简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 java 代码.特别是相对于 ...
- 【JavaScript 开发规范】
Javascript 最佳实践http://sofish.de/1171http://sofish.de/1181 总是使用 ‘var’ √ 特性检测而非浏览器检测 √ 使用方括号记法 √ 使用&qu ...