JZOJ 5776. 【NOIP2008模拟】小x游世界树
5776. 【NOIP2008模拟】小x游世界树
(File IO): input:yggdrasil.in output:yggdrasil.out
Description
Input
第二行n个数ai,表示每个平台上的加速器的性能。
第三至n+1行,每行三个数bi,ci,di分别表示这条无向边的起点,终点与耗费的能量值
Output
第二行一个数,表示最小耗费的体力值。
Sample Input
4
2 1 3 3
1 2 3
1 3 4
2 4 6
Sample Output
1
9
Data Constraint
对于40%的数据:n<=1000
对于60%的数据:n<=8000
对于80%的数据:n<=100000
对于100%的数据:0<n<=700000;ai<=1000;1<=bi,ci<=n;di<=1000。
数据保证一个点的加速器性能绝对小于等于它的所有的边所耗费的能量,保证所有节点都可以到达,保证没有数据与样例相同。
Hint
样例解释:
如果以第一个点为根,则需要耗费0(到1)+1(到2)+2(到3)+6(到4)=9的能量值。
如果以第二个点为根,则需要耗费2(到1)+0(到2)+4(到3)+5(到4)=11的能量值。
如果以第三个点为根,则需要耗费1(到1)+2(到2)+0(到3)+7(到4)=10的能量值。
如果以第四个点为根,则需要耗费5(到1)+3(到2)+7(到3)+0(到4)=15的能量值。
很明显以第一个点为根是最优的。
#include <iostream>
#include <cstring>
#include <cstdio>
#define LL long long
#define N 1000007
using namespace std;
struct edge
{
LL to, next, w;
}e[N * ];
LL zs[N], n, a[N], ls[N * ], ans, root, site, cnt;
bool v[N]; void add(LL x, LL y, LL z)
{
e[++cnt].to = y;
e[cnt].next = ls[x];
e[cnt].w = z;
ls[x] = cnt;
} void dfs(LL x, LL sum)
{
root += sum;
for (int i = ls[x]; i; i = e[i].next)
{
if (v[e[i].to]) continue;
v[e[i].to] = ;
dfs(e[i].to, sum + (e[i].w - a[x]));
zs[x] += zs[e[i].to];
}
zs[x]++;
} void find(LL x, LL sum)
{
if (sum < ans)
{
ans = sum;
site = x;
}
else if (sum == ans) site = min(site, x);
for (int i = ls[x]; i; i = e[i].next)
{
if (v[e[i].to]) continue;
v[e[i].to] = ;
find(e[i].to, sum + (n - zs[e[i].to]) * (e[i].w - a[e[i].to])- zs[e[i].to] * (e[i].w - a[x]));
}
} int main()
{
freopen("yggdrasil.in", "r", stdin);
freopen("yggdrasil.out", "w", stdout);
scanf("%lld", &n);
for (int i = ; i <= n; i++)
scanf("%lld", &a[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);
}
v[] = ;
dfs(, );
ans = root;
site = ;
memset(v, , sizeof(v));
v[] = ;
find(, root);
printf("%lld\n", site);
printf("%lld", ans);
}
JZOJ 5776. 【NOIP2008模拟】小x游世界树的更多相关文章
- JZOJ5776. 【NOIP2008模拟】小x游世界树
题目:[NOIP2008模拟]小x游世界树: 题目的附加题解给的很清楚,这里只给一个代码: #include<iostream> #include<cstdio> #inclu ...
- [题解](树形dp/换根)小x游世界树
2. 小x游世界树 (yggdrasi.pas/c/cpp) [问题描述] 小x得到了一个(不可靠的)小道消息,传说中的神岛阿瓦隆在格陵兰海的某处,据说那里埋藏着亚瑟王的宝藏,这引起了小x的好奇,但当 ...
- [jzoj 5776]【NOIP2008模拟】小x游世界树 (树形dp)
传送门 Description 小x得到了一个(不可靠的)小道消息,传说中的神岛阿瓦隆在格陵兰海的某处,据说那里埋藏着亚瑟王的宝藏,这引起了小x的好奇,但当他想前往阿瓦隆时发现那里只有圣诞节时才能到达 ...
- JZOJ 5777. 【NOIP2008模拟】小x玩游戏
5777. [NOIP2008模拟]小x玩游戏 (File IO): input:game.in output:game.out Time Limits: 1000 ms Memory Limits ...
- JZOJ 5793. 【NOIP2008模拟】小S练跑步
5793. [NOIP2008模拟]小S练跑步 (File IO): input:run.in output:run.out Time Limits: 2000 ms Memory Limits: ...
- JZOJ 5775. 【NOIP2008模拟】农夫约的假期
5775. [NOIP2008模拟]农夫约的假期 (File IO): input:shuru.in output:shuru.out Time Limits: 1000 ms Memory Lim ...
- JZOJ 5773. 【NOIP2008模拟】简单数学题
5773. [NOIP2008模拟]简单数学题 (File IO): input:math.in output:math.out Time Limits: 1000 ms Memory Limits ...
- JZOJ 5809. 【NOIP2008模拟】数羊
5809. [NOIP2008模拟]数羊 (File IO): input:sheep.in output:sheep.out Time Limits: 1000 ms Memory Limits: ...
- JZOJ 5791. 【NOIP2008模拟】阶乘
5791. [NOIP2008模拟]阶乘 (File IO): input:factorial.in output:factorial.out Time Limits: 1000 ms Memory ...
随机推荐
- Shell编程中的条件判断(条件测试)
Shell中的条件判断(测试)类型: 1) 整数测试 2) 字符测试 3) 文件测试 条件测试的表达式: (注: expression 与 [] 之间空格不能省略) [ expressi ...
- web 单例 多例
单例多例需要搞明白两个问题:1. 什么是单例多例:2. 如何产生单例多例:3. 为什么要用单例多例4. 什么时候用单例,什么时候用多例:1. 什么是单例多例:所谓单例就是所有的请求都用一个对象来处理, ...
- xmanger图形化登陆远程服务器
由于网上的资料比较杂,经过本人整理实际操作验证,保证ok 本人的服务器系统为centos5.8 下面的都是centos服务器上的操作,需要简单的配置下: win客户端使用xmanger软件:首先是服 ...
- Ionic2集成DevExtreme
安装Install DevExtreme Angular npm install --save devextreme devextreme-angular 或者在package.json 文件中增加依 ...
- jQuery知识重构
jQuery知识重构 目录: 一.入口函数 1 $(document).ready(function(){}); 2 $(function(){}); jQuery ...
- 【extjs6学习笔记】0.4 准备: 书籍与文档
Ext JS 6 By Example Ext JS Essentials Learning Ext JS - Fourth Edition Ext JS 6: Getting Started htt ...
- System Center Configuration Manager 2016 域准备篇(Part2)
对于" 服务器角色",请选择" Active Directory域服务",当系统提示您添加Active Directory域服务所需的功能时,请选择" ...
- html常用的小技能
在html中有很多常用小技能,记下来,下次直接看自己的,就不用四处找啦! 1.<li>标签去掉点号:list-style-type:none; 去掉前: 去掉后: 2.<li> ...
- VMware-Ubuntu16.04LTS-安装ssh
1,检查是否安装ssh 2,安装ssh
- HDU3308 线段树区间合并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 ,简单的线段树区间合并. 线段树的区间合并:一般是要求求最长连续区间,在PushUp()函数中实 ...