题目传送门

首先这道题是在树上进行的,然后求最小的不方便程度,比较符合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的更多相关文章

  1. 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…(树规)

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  2. P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  3. [USACO10MAR]伟大的奶牛聚集Great Cow Gat…

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  4. 【题解】Luogu p2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat 树型dp

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  5. [洛谷P2986][USACO10MAR]伟大的奶牛聚集Great Cow Gat…

    题目大意:给你一棵树,每个点有点权,边有边权,求一个点,使得其他所有点到这个点的距离和最短,输出这个距离 题解:树形$DP$,思路清晰,转移显然 卡点:无 C++ Code: #include < ...

  6. [USACO10MAR]伟大的奶牛聚集Great Cow Gat… ($dfs$,树的遍历)

    题目链接 Solution 辣鸡题...因为一个函数名看了我贼久. 思路很简单,可以先随便指定一个根,然后考虑换根的变化. 每一次把根从 \(x\) 换成 \(x\) 的一个子节点 \(y\),记录一 ...

  7. LUOGU P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…

    传送门 解题思路 首先第一遍dfs预处理出每个点的子树的siz,然后可以处理出放在根节点的答案,然后递推可得其他答案,递推方程 sum[u]=sum[x]-(val[i]*siz[u])+(siz[1 ...

  8. 洛谷 P2986 [USACO10MAR]Great Cow Gat…(树形dp+容斥原理)

    P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat… 题目描述 Bessie is planning the annual Great Cow Gathering for c ...

  9. BZOJ 1827 洛谷 2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gather

    [题解] 很容易想到暴力做法,枚举每个点,然后对于每个点O(N)遍历整棵树计算答案.这样整个效率是O(N^2)的,显然不行. 我们考虑如果已知当前某个点的答案,如何快速计算它的儿子的答案. 显然选择它 ...

随机推荐

  1. MySQL搭建系列之多实例

    所谓多实例.就是在一台server上搭建.执行多个MySQL实例,每一个实例使用不同的服务port.通过不同的socket监听:物理上,每一个实例拥有独立的參数配置文件及数据库. 通常情况下.一台se ...

  2. nyoj 题目10 skiing —— 南阳oj

    题目信息例如以下: skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪, 由于滑雪的确非常刺激.但是为了获得速度,滑的区 ...

  3. 小程序多级下拉菜单demo

    小程序多级下拉菜单demo - CSDN博客 https://blog.csdn.net/github_39371177/article/details/80251211

  4. install build tools 25.0.2 and sync the project

    install build tools 25.0.2 and sync the project in android studio bundle.gradle,将buildToolsVersion修改 ...

  5. 恢复MySQL数据库删除的数据

    在日常运维工作中,对于数据库的备份是至关重要的!数据库对于网站的重要性使得我们对 MySQL 数据库的管理不容有失!然而是人总难免会犯错误,说不定哪天大脑短路了,误操作把数据库给删除了,怎么办? 下面 ...

  6. (linux)platform_driver_probe与platform_driver_register的区别

      [驱动注册]platform_driver_register()与platform_device_register()          设备与驱动的两种绑定方式:在设备注册时进行绑定及在驱动注册 ...

  7. vscode——配置终端集成bash和cmd

    前言 配置后bash和cmd是集成的,输入bash回车则进入bash,输入cmd回车则进入cmd 步骤 首先肯定是需要打开我们的vscode咯~ 进入终端设置 配置shell路径 根据自己的系统来复制 ...

  8. SpringMVC配置环境

    一,lib目录下加入spring一般所需的jar包 二,配置web.xml <?xml version="1.0" encoding="UTF-8"?&g ...

  9. html5--6-19 CSS3中的文字与字体

    html5--6-19 CSS3中的文字与字体 学习要点 掌握文字与字体的设置 颜色值查询方法: 百度查询,很多网站有提供 下载相关手册等需要时查表 运用绘图工具中的拾色器 CSS中常用的字体属性设置 ...

  10. MyEclipse注释配置

    MyEclipse注释配置 配置路径 1.1.      JAVA 打开MyEclipse,选择Window>Preferences>Java>Code Style>Code ...