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 ...
随机推荐
- hadoop map 个数 源码分析
本文转自http://ronxin999.blog.163.com/blog/static/42217920201279112163/
- vs2017通过snippet代码片断进行标准化注释
我们在进行团队开发时,类的注释已经可以做到自定义了,详细看上篇文章<vs2017通过模块文件添加自定义注释>,而对于方法的注释,我们也需要完善一下,这里我们用到了“代码片断”插件,VS里有 ...
- PHP的Undefined variable错误怎么解决?
在调试程序时,有可能会出现Undefined variable错误,一般情况下php是不需要定义变量的,但如果服务器什么都报错的,就会出现错误,我们经常接收表单POST过来的数据时报Undefined ...
- Visual Studio 使用
目录结构 solution_dir Debug: 存放Debug版本信息的.exe Release: Release的.exe .sln: visual studio 项目文件 project_dir ...
- 前端-页面性能调试:Hiper
前端-页面性能调试:Hiper 我们写单页面应用,想看页面修改后性能变更其实挺繁琐的.有时想知道是「正优化」还是「负优化」只能靠手动刷新查看network.而Hiper很好解决了这一痛点(其实Hi ...
- IO多路复用丶基于IO多路复用+socket实现并发请求丶协程
一丶IO多路复用 IO多路复用指:通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作 IO多路复用作用: 检测多个socket是否已经发生变 ...
- JavaScript中函数声明和函数表达式的区别
声明一个函数: var x=1; foo(); function foo() { console.log(x);//1 } myfun();//报错 定义一个函数表达式: var myfun=myfo ...
- (转载)C#中的lock关键字
lock 关键字可以用来确保代码块完成运行,而不会被其他线程中断.这是通过在代码块运行期间为给定对象获取互斥锁来实现的. 先来看看执行过程,代码示例如下: 假设线程A先执行,线程B稍微慢一点.线程A执 ...
- Bugzilla-5.0.3 (OpenLogic CentOS 7.2)
平台: CentOS 类型: 虚拟机镜像 软件包: apache2.4.6 bugzilla5.0.3 mariadb5.5.47 perl5.16.3 apache bug tracking bug ...
- ubuntu常见错误
ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解决 ubuntu常见错误--Could not get lock /var/lib/dpkg/loc ...