[USACO10MAR]伟大的奶牛聚集Great Cow Gat…【树形dp】By cellur925
首先这道题是在树上进行的,然后求最小的不方便程度,比较符合dp的性质,那么我们就可以搞一搞树形dp。
设计状态:f[i]表示以i作为聚集地的最小不方便程度。那么我们还需要各点间的距离,但是由于本题数据加强到1e5,开二维数组显然是不现实的,我们可以换一种思路,求d[i]表示其他所有奶牛到 i点的距离和,这样就成功转化过来惹==。
d[u]+=d[v]+size[v]*edge[i].val
然后再预处理size[i]数组表示以i为根的子树上奶牛的数量。这些都是一遍dfs就能搞定的,然后本题其实开始是无根树,这里默认1为根,看做有根树惹==。
转移:我们易知本题的转移是从当前状态转移到未来状态的。冷静分析可得
f[v]=f[u]-size[v]*edge[i].val+(cnt-size[v])*edge[i].val
然后本题中出现几次的转化思想,如上述方程中的cnt在这题中也有体现。
Code
#include<cstdio>
#include<algorithm> using namespace std;
typedef long long ll; int n,tot;
ll ans=1e40,cnt,size[],d[],f[];
ll val[];
ll head[];
struct node{
ll to,next,val;
}edge[*]; ll lmin(ll a,ll b)
{
if(a<b) return a;
else return b;
} void add(ll x,ll y,ll z)
{
edge[++tot].to=y;
edge[tot].val=z;
edge[tot].next=head[x];
head[x]=tot;
} void dfs_pre(int u,int fa)
{
size[u]=val[u];
for(ll i=head[u];i;i=edge[i].next)
{
ll v=edge[i].to;
if(v==fa) continue;
dfs_pre(v,u);
size[u]+=size[v];
d[u]+=d[v]+1ll*size[v]*edge[i].val;
}
} void TreeDP(int u,int fa)
{
for(ll i=head[u];i;i=edge[i].next)
{
ll v=edge[i].to;
if(v==fa) continue;
f[v]=1ll*f[u]-1ll*size[v]*edge[i].val+1ll*(cnt-size[v])*edge[i].val;
ans=lmin(ans,f[v]);
TreeDP(v,u);
}
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%lld",&val[i]),cnt+=val[i];
for(int i=;i<=n-;i++)
{
ll x=,y=,z=;
scanf("%lld%lld%lld",&x,&y,&z);
add(x,y,z),add(y,x,z);
}
// for(int i=1;i<=tot;i++)
// printf("%d %d %lld\n",edge[i].to,edge[i].next,edge[i].val);
// return 0;
dfs_pre(,);
// for(int i=1;i<=n;i++)
// printf("%lld ",d[i]);
f[]=d[];
ans=lmin(ans,f[]);
TreeDP(,);
// for(int i=1;i<=n;i++)
// printf("%lld ",f[i]);
// for(int i=1;i<=n;i++)
// ans=lmin(ans,f[i]);
printf("%lld",ans);
return ;
}
细节:本题开long long是毋庸置疑的,我开始把ans初值赋成了0x3f3f3f3f,在一般情况下本来够用,但是因为本题数据较大,所以在这种环境下就显得偏小了,可开到1e40.
[USACO10MAR]伟大的奶牛聚集Great Cow Gat…【树形dp】By cellur925的更多相关文章
- 洛谷 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]伟大的奶牛聚集Great Cow Gat…
题目描述 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 ...
- [洛谷P2986][USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目大意:给你一棵树,每个点有点权,边有边权,求一个点,使得其他所有点到这个点的距离和最短,输出这个距离 题解:树形$DP$,思路清晰,转移显然 卡点:无 C++ Code: #include < ...
- [USACO10MAR]伟大的奶牛聚集Great Cow Gat… ($dfs$,树的遍历)
题目链接 Solution 辣鸡题...因为一个函数名看了我贼久. 思路很简单,可以先随便指定一个根,然后考虑换根的变化. 每一次把根从 \(x\) 换成 \(x\) 的一个子节点 \(y\),记录一 ...
- LUOGU P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
传送门 解题思路 首先第一遍dfs预处理出每个点的子树的siz,然后可以处理出放在根节点的答案,然后递推可得其他答案,递推方程 sum[u]=sum[x]-(val[i]*siz[u])+(siz[1 ...
- 洛谷 P2986 [USACO10MAR]Great Cow Gat…(树形dp+容斥原理)
P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat… 题目描述 Bessie is planning the annual Great Cow Gathering for c ...
- BZOJ 1827 洛谷 2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gather
[题解] 很容易想到暴力做法,枚举每个点,然后对于每个点O(N)遍历整棵树计算答案.这样整个效率是O(N^2)的,显然不行. 我们考虑如果已知当前某个点的答案,如何快速计算它的儿子的答案. 显然选择它 ...
随机推荐
- java zip压缩文件和文件夹
public class FileUtil { /** * 压缩文件-File * @param out zip流 * @param srcFiles 要压缩的文件 * @param path 相对路 ...
- 笨鸟不乖 是这么设计Android项目架构的
项目地址:https://github.com/benniaobuguai/android-project-wo2b部分效果图 项目结构当前项目只是其中一个例子,wo2b-common- ...
- 全国省市区三级联动js
function Dsy(){ this.Items = {}; } Dsy.prototype.add = function(id,iArray){ this.Items[id] = iArray; ...
- 微信小程序项目实例
目前为止最全的微信小程序项目实例 2018年03月20日 11:38:28 Happy王子乐 阅读数:4188 wx-gesture-lock 微信小程序的手势密码 WXCustomSwitch ...
- cocos2d-x 3.0 touch事件官方解释
官方解释 http://www.cocos2d-x.org/docs/manual/framework/native/input/event-dispatcher/zh#_1
- Nova镜像使用方法
Nova中的虚拟机可以从镜像.卷.卷快照等启动,一般装完OpenStack时,环境中既没有镜像也没有卷,这时候往往 需要上传一些已有的镜像,或者上传ISO文件来安装虚拟机系统.这个文档主要描述如何上传 ...
- mac上pydev
转自:http://m.blog.csdn.net/blog/yangfu132/23689823 本来网上有教程,但是往往又一些不周到的地方,让人走了不少弯路. 使用 PyDev 进行调试 第一步: ...
- delphi中的HOOK [转贴]
按事件分类,有如下的几种常用类型的钩子: 1)键盘钩子可以监视各种键盘消息. 2)鼠标钩子可以监视各种鼠标消息. 3)外壳钩子可以监视各种Shell事件消息. 4)日志钩子可以记录从系统消息队列中取出 ...
- https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/connections.py
# Python implementation of the MySQL client-server protocol # http://dev.mysql.com/doc/internals/en/ ...
- Android开发之开机自动启动应用
package com.raycloud.wolf.autostart; import android.content.BroadcastReceiver; import android.conten ...