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 ...
随机推荐
- (转)运维老鸟教你安装centos6.5如何选择安装包
运维老鸟教你安装centos6.5如何选择安装包 原文:http://blog.51cto.com/oldboy/1564620 近来发现越来越多的运维小伙伴们都有最小化安装系统的洁癖,因此,找老男孩 ...
- Python Class __init__ __del__ 构造,析构过程解析【转】
转载自: http://blog.csdn.net/bbdxf/article/details/25774763 最近学习<Python参考手册>即<Learning Python& ...
- 利用Python进行数据分析 2017 第二版 项目代码
最近在学习<利用Python进行数据分析>,找到了github项目的地址, 英文版本,中文版本 (非常感谢翻译中文的作者). mark一下,方便后边学习查找.
- Java 内存模型(零)
经典老图: 方法区和堆:由所有线程共享的数据区 虚拟机栈和本地方法栈和程序计数器:线程隔离的数据区 后面一步步讲这个图里面所涉及到的内容,待续...
- <ganglia+nagios>rhel6.5
由于linux下的office和win下有所区别,我只能把linux下的.dot文件打包成pdf,粘贴发送标出来,但有些图片还是没办法发表,要是有朋友感兴趣的话,可加我qq 215687833具体的文 ...
- A promise tomorrow is worth a lot less than trying today.
A promise tomorrow is worth a lot less than trying today.明日的承诺远不及今日的行动.
- IIS发布网站出错解决方案
1.第一类错误(Web服务器被配置为不列出此目录的内容) 问题所在没有为请求的URL设置默认文档,在IIS“默认文档”添加一个你要访问的默认文档名字,如:Default.aspx. 2.第二类错误(请 ...
- SpringBoot集成mybatis和mybatis generator
利用搭建的基本的spring boot框架,集成 mybatis + generator 1.设置 maven 的相关配置: File - setting - maven 设置 Maven home ...
- PHP函数:method_exists和function_exists
method_exists 检查类的方法是否存在 bool method_exists ( mixed $object , string $method_name ) 检查类的方法是否存在于指定的ob ...
- JS案例练习:图片切换+切换模式
先附图: CSS样式部分: <style> *{;} body{font-family:'Microsoft YaHei';} .menu{margin:20px auto 0; widt ...