题目:【NOIP2008模拟】小x游世界树;

题目的附加题解给的很清楚,这里只给一个代码;

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ms(a,x) memset(a,x,sizeof(a))
#define ll long long
using namespace std;
const int MAXN=;
struct node{
int y,nxt;ll z;
}edge[MAXN*];int indx=,n,head[MAXN];
ll a[MAXN],ans=0x7fffffffff,rt=,siz[MAXN],f[MAXN];
void add(int x,int y,ll z){
edge[++indx].y=y;edge[indx].z=z;
edge[indx].nxt=head[x];head[x]=indx;
}
void dp(int x,int fa){
siz[x]=;
for(int i=head[x],y;~i;i=edge[i].nxt){
if((y=edge[i].y)==fa) continue;
dp(y,x);f[x]+=1LL*edge[i].z*siz[y];
siz[x]+=siz[y];f[x]+=f[y];
} return ;
}
void dfs(int x,int fa,ll la,ll v){
la-=v*siz[x];
for(int i=head[x];~i;i=edge[i].nxt){
if(edge[i].y!=fa) continue;
la+=edge[i].z*(n-siz[x]);break;
} if(la<ans) ans=la,rt=x;
else if(la==ans) rt=min((int)x,(int)rt);
for(int i=head[x],y;~i;i=edge[i].nxt){
if((y=edge[i].y)==fa) continue;
dfs(y,x,la,edge[i].z);
} return ;
}
int main(){
// freopen("yggdrasil.in","r",stdin);
// freopen("yggdrasil.out","w",stdout);
scanf("%d",&n);ms(head,-);
for(int i=;i<=n;i++) scanf("%lld",&a[i]);
for(int i=,x,y;i<n;i++){
ll z;scanf("%d%d%lld",&x,&y,&z);
add(x,y,a[x]>?z-a[x]:z);
add(y,x,a[y]>?z-a[y]:z);
} dp(,-);ans=min(ans,f[rt]);
for(int i=head[];~i;i=edge[i].nxt)
dfs(edge[i].y,,f[],edge[i].z);
printf("%lld\n%lld\n",rt,ans);
return ;
}

树形DP与换根

JZOJ5776. 【NOIP2008模拟】小x游世界树的更多相关文章

  1. JZOJ 5776. 【NOIP2008模拟】小x游世界树

    5776. [NOIP2008模拟]小x游世界树 (File IO): input:yggdrasil.in output:yggdrasil.out Time Limits: 1500 ms  Me ...

  2. [题解](树形dp/换根)小x游世界树

    2. 小x游世界树 (yggdrasi.pas/c/cpp) [问题描述] 小x得到了一个(不可靠的)小道消息,传说中的神岛阿瓦隆在格陵兰海的某处,据说那里埋藏着亚瑟王的宝藏,这引起了小x的好奇,但当 ...

  3. [jzoj 5776]【NOIP2008模拟】小x游世界树 (树形dp)

    传送门 Description 小x得到了一个(不可靠的)小道消息,传说中的神岛阿瓦隆在格陵兰海的某处,据说那里埋藏着亚瑟王的宝藏,这引起了小x的好奇,但当他想前往阿瓦隆时发现那里只有圣诞节时才能到达 ...

  4. JZOJ 5777. 【NOIP2008模拟】小x玩游戏

    5777. [NOIP2008模拟]小x玩游戏 (File IO): input:game.in output:game.out Time Limits: 1000 ms  Memory Limits ...

  5. JZOJ 5793. 【NOIP2008模拟】小S练跑步

    5793. [NOIP2008模拟]小S练跑步 (File IO): input:run.in output:run.out Time Limits: 2000 ms  Memory Limits:  ...

  6. JZOJ 5775. 【NOIP2008模拟】农夫约的假期

    5775. [NOIP2008模拟]农夫约的假期 (File IO): input:shuru.in output:shuru.out Time Limits: 1000 ms  Memory Lim ...

  7. JZOJ 5773. 【NOIP2008模拟】简单数学题

    5773. [NOIP2008模拟]简单数学题 (File IO): input:math.in output:math.out Time Limits: 1000 ms  Memory Limits ...

  8. JZOJ 5809. 【NOIP2008模拟】数羊

    5809. [NOIP2008模拟]数羊 (File IO): input:sheep.in output:sheep.out Time Limits: 1000 ms  Memory Limits: ...

  9. JZOJ 5791. 【NOIP2008模拟】阶乘

    5791. [NOIP2008模拟]阶乘 (File IO): input:factorial.in output:factorial.out Time Limits: 1000 ms  Memory ...

随机推荐

  1. org.springframework.web.struts.ContextLoaderPlugIn 和 org.springframework.web.context.ContextLoaderListener

    org.springframework.web.struts.ContextLoaderPlugIn 和 org.springframework.web.context.ContextLoaderLi ...

  2. linux修改用户主目录的方法 (转载)

    转自:http://xiaomaimai.blog.51cto.com/1182965/274002 第一:修改/etc/passwd文件第二:usermod命令 详细说明如下:第一种方法:vi /e ...

  3. 洛谷P3239 [HNOI2015]亚瑟王(期望dp)

    传送门 stdcall大佬好强 期望的姿势不是很高……据大佬说期望有一个线性性质,也就是说可以把每一张牌的期望伤害算出来然后再加起来就是总的期望伤害 因为每一张牌只能用一次,我们设$dp[i]$表示第 ...

  4. 安卓小程序的一次bug调试,报错:java.lang.NullPointerException,logcat学习

    做实验的时候,调试了很久后模拟器执行后,app还是会崩溃并停止运行,错误如下. 因为初学,所以也不知道怎么使用调试工具,也不懂看日志,经过学习后尝试这查看了LogCat日志上面有这样的提示: 其中引起 ...

  5. AOP日志框架实现

    AOP日志框架实现 JDK动态代理实现日志框架 首先,在项目包com.ay.test 下创建业务接口类BusinessClassService,具体代码如下: BusinessC lassServic ...

  6. Windows中句柄和ID的区别

    写在前面:这里介绍句柄 对于“句柄”,在下一直停留在一知半解的认识层面,近日在下学习Windows编程,决定趁此机会将句柄彻底搞清楚.查阅了一些网络上的资料,发现网络上的讲解大概可以分为两类:一种是以 ...

  7. BZOJ2006 超级钢琴

    Description ​ 给定一个长度为n的区间,询问前k大的区间和,区间长度\(\in [L, R]\). $ n, k <= 500000$ Solution ​ 首先求前缀和.把一个区间 ...

  8. _bzoj2243 [SDOI2011]染色【树链剖分】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2243 裸的树链剖分,最开始我保存一个线段树节点的color值时(若有多种颜色则为-1),不小 ...

  9. Android Dialogs(4)Dialog事件处理

    Passing Events Back to the Dialog's Host When the user touches one of the dialog's action buttons or ...

  10. java实现九九乘法表

    public class Demo { public static void main(String[] args) { for (int i = 1; i < 10; i++) {      ...