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 ...
随机推荐
- ubuntu网络已禁用解决办法
1.首先停掉network-manager这个服务 sudo service network-manager stop 2.删除掉NetworkManager.state这个文件 sudo rm /v ...
- leetcoe--47. Permutations II
1.问题描述 Given a collection of numbers that might contain duplicates, return all possible unique permu ...
- Eventlet Greenlet
Eventlet是一个用来处理和网络相关的python网络库,而且可以通过协程来实现并发,在eventlet里,把“协程”叫做greenthread. 所谓并发,就是开启了多个greenthread, ...
- MyEclipse8.5配置struts等框架
开发环境:MyEclipse8.5+Tomcat6.5+MySql5.5配置环境:Struts2+Spring2+Hibernate3.1 1.首先创建Java-Web Project工程,选择J2E ...
- Java排序算法(一)
Java排序算法(一) 排序的基本概念和分类 1.1排序的定义 在<大话数据结构>中,排序定义为,假设含有n个记录的序列为{r1,r2,...,rn},其相应的关键字{k1,k2,..., ...
- BZOJ3798: 特殊的质数(分段打表)
题意 题目链接 Sol 分块打表,非常好的思想. 对于这种求$[A, B]$区间内xxx的数的个数,然后$B$又不算是特别大的题,考虑分段打表 每个块的大小为$10^5$,打$3 * 10^3$个.然 ...
- Google常用拓展插件
1.web前端助手(FEhelper)提供一些实用的前端小工具,功能十分贴心 2.bookMarks Manager 一个书签管理工具 3.Clear Cache 清除浏览器的缓存,有很多供选择的条目 ...
- Java Web中web.xml文件简单介绍
参考博客: https://www.cnblogs.com/Y-oung/p/8401549.html 1.XML 声明和根元素 <?xml version="1.0" en ...
- linux中BASH_SOURCE[0](转)
转自:http://www.cnblogs.com/sunfie/p/5943979.html 在C/C++中,__FUNCTION__常量记录当前函数的名称.有时候,在日志输出的时候包含这些信息是非 ...
- Vultr VPS建站攻略 – 一键安装LNMP无面板高性能WEB环境
在"Vultr VPS建站攻略 - 一键安装宝塔面板架设LNMP/LAMP Web环境"文章中,VULTR中文网分享到我们常用的可视化面板宝塔面板安装在VULTR VPS主机中建站 ...