题目传送门

首先这道题是在树上进行的,然后求最小的不方便程度,比较符合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. 键值对集合Dictionary<K,V>根据索引提取数据

    Dictionary<K,V>中ToList方法返回 List<KeyValuePair<K,V>>定义可设置检索的键/值对

  2. 多线程-----Thread类与Runnable接口的区别

    第一个继承Thread类来实现多线程,其实是相当于拿出三件事即三个卖早餐10份的任务分别分给三个窗口,他们各做各的事各卖各的早餐各完成各的任务,因为MyThread继承Thread类,所以在newMy ...

  3. adb4robotium跨进程框架抛出InputStream cannot be null的异常的解决方案

    转自:http://blog.csdn.net/qingchunjun/article/details/43448371 之前我写的关于利用adb框架来进行robotium跨进程操作的文章中,有些朋友 ...

  4. HDU 6078 Wavel Sequence 树状数组优化DP

    Wavel Sequence Problem Description Have you ever seen the wave? It's a wonderful view of nature. Lit ...

  5. 备忘录模式-Memento

    备忘录模式:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可以将该对象恢复到原先保存的状态. 备忘录模式结构图: 何时使用备忘录模式: Memento模式比适合 ...

  6. 使用Android Studio查看API文档

    在使用Android Studio编码时,若要查看某个类或函数的释义, 只需将光标移动至要查看释义的代码处,然后按下Ctrl+Q,便会弹出文档描述. 然而,有时候会出现如下状况: 因为默认查看的是在线 ...

  7. jQuery整理笔记九----功能性表格开发

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/hai_cheng001/article/details/27536965 演示样例中用到的一些图片. ...

  8. vue中导出Excel表格

    项目中我们可能会碰到导出Excel文件的需求,一般后台管理系统中居多,将table中展示的数据导出保存到本地.当然我们也可以通过一些处理来修改要导出的数据格式,具体需求具体对待. 1.首先我们需要安装 ...

  9. POJ3436 ACM Computer Factory —— 最大流

    题目链接:https://vjudge.net/problem/POJ-3436 ACM Computer Factory Time Limit: 1000MS   Memory Limit: 655 ...

  10. How to create a List of ValueTuple?

    ValueTuple需要通过NuGet安装System.ValueTuple https://docs.microsoft.com/en-us/dotnet/csharp/tuples?view=ne ...