[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 ...
随机推荐
- python 读取机器信息
本人最近新学python ,用到关于机器的相关信息,经过一番研究,从网上查找资料,经过测试,总结了一下相关的方法. # -*- coding: UTF8 -*- import os import wi ...
- vs2013运行c语言出现:无法查找或打开 PDB 文件。
vs2013运行c语言出现:无法查找或打开 PDB 文件. “ConsoleApplication1.exe”(Win32): 已加载“C:\Users\hp\Documents\Visual ...
- 上下切换js
<div class="wview"> <span class="prevs" id="prevs-j"></ ...
- javascript 冒泡
http://www.cnblogs.com/hh54188/archive/2012/02/08/2343357.html http://blog.csdn.net/xuefeng0707/arti ...
- 如何成为python高手(转)
http://www.cnblogs.com/xupeizhi/p/3207976.html#2896469 如何成为python高手 本文是从 How to become a proficient ...
- MySQL 性能调优的10个方法
Mysql的优化方面,一般我们很少去考虑它,即使想到优化一般也更多是程序级别的,比如不要写过于消耗资源的SQL语句,但是除此以外,在整个系统上其实仍然有很多可以优化的地方. 1. 选择合适的存储引擎: ...
- 更换手机号或者更换手机后QQ设备锁的设置问题
更换手机号 一步到位,更改密保手机号,OK了 更换手机 老卡插入 登录QQ,OK了 更换手机号和手机 老卡插入新手机 登录QQ 新卡插入新手机 更改密保手机号,OK了
- 我的第一个Struts程序
1.程序结构 2.各种文件 LoginAction.java package com.tfj.action; public class LoginAction { private String use ...
- BASE64编码规则及C#实现
一.编码规则 Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码.它将需要编码的数据拆分成字节数组.以3个字节为一组.按顺序排列24位数据,再把这24位数据分成4组 ...
- Linux kernel 内存泄露本地信息泄露漏洞
漏洞名称: Linux kernel 内存泄露本地信息泄露漏洞 CNNVD编号: CNNVD-201311-467 发布时间: 2013-12-06 更新时间: 2013-12-06 危害等级: ...