[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 ...
随机推荐
- 使用js为html元素动态添加class
<ul id="root"> <li>1</li> <li>2</li> <li>3</li> ...
- 学习lamda表达式
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.L ...
- Hbase 0.96 比 hbase 0.94的改变
转载:http://blog.csdn.net/hxpjava1/article/details/20043703 环境: hadoop:hadoop-2.2.0 hbase:hbase-0.96.0 ...
- ExtJS4.2学习(15)树形表格(转)
鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-27/185.html --------------- ...
- linux 查看各服务状态chkconfig
使用chkconfig 查看服务状态启动状态chkconfig --list 查看服务状态chkconfig --del <service name> 删除掉某项服务.在Fedora14中 ...
- x86, x86-64, i386, IA32, IA64...
转自x86, x86-64, i386, IA32, IA64... x86:Intel从16位微处理器8086开始的整个CPU芯片系列,系列中的每种型号都保持与以前的各种型号兼容,主要有8086,8 ...
- POJ 3321 Apple Tree(树状数组)
点我看题目 题意 : 大概是说一颗树有n个分岔,然后给你n-1对关系,标明分岔u和分岔v是有边连着的,然后给你两个指令,让你在Q出现的时候按照要求输出. 思路 :典型的树状数组.但是因为没有弄好数组 ...
- 转发:maven打包时始终出现以下提示:-source 1.3 中不支持泛型(请使用 -source 5 或更高版本以启用泛型)
maven打包时始终出现以下提示: 1.-source 1.3 中不支持泛型(请使用 -source 5 或更高版本以启用泛型)List<User> userList= new Array ...
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-002- 在xml中引用Java配置文件,声明DispatcherServlet、ContextLoaderListener
一.所有声明都用xml 1. <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...
- IOS开发中单例模式使用详解
第一.基本概念 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问. 第二.在IOS中使用单例模式的情 ...