当前位置: 首页 > news >正文

FarmCraft[POI2014]

题目描述

In a village called Byteville, there are   houses connected with N-1 roads. For each pair of houses, there is a unique way to get from one to another. The houses are numbered from 1 to  . The house no. 1 belongs to the village administrator Byteasar. As part of enabling modern technologies for rural areas framework,   computers have been delivered to Byteasar"s house. Every house is to be supplied with a computer, and it is Byteasar"s task to distribute them. The citizens of Byteville have already agreed to play the most recent version of FarmCraft (the game) as soon as they have their computers.
Byteasar has loaded all the computers on his pickup truck and is about to set out to deliver the goods. He has just the right amount of gasoline to drive each road twice. In each house, Byteasar leaves one computer, and immediately continues on his route. In each house, as soon as house dwellers get their computer, they turn it on and install FarmCraft. The time it takes to install and set up the game very much depends on one"s tech savviness, which is fortunately known for each household. After he delivers all the computers, Byteasar will come back to his house and install the game on his computer. The travel time along each road linking two houses is exactly 1 minute, and (due to citizens" eagerness to play) the time to unload a computer is negligible.
Help Byteasar in determining a delivery order that allows all Byteville"s citizens (including Byteasar) to start playing together as soon as possible. In other words, find an order that minimizes the time when everyone has FarmCraft installed.
 
中文接地气版:
mhy住在一棵有n个点的树的1号结点上,每个结点上都有一个妹子。
mhy从自己家出发,去给每一个妹子都送一台电脑,每个妹子拿到电脑后就会开始安装zhx牌杀毒软件,第i个妹子安装时间为Ci。
树上的每条边mhy能且仅能走两次,每次耗费1单位时间。mhy送完所有电脑后会回自己家里然后开始装zhx牌杀毒软件。
卸货和装电脑是不需要时间的。
求所有妹子和mhy都装好zhx牌杀毒软件的最短时间。
 

输入

The first line of the standard input contains a single integer N(2<=N<=5 00 000)  that gives the number of houses in Byteville. The second line contains N integers C1,C2…Cn(1<=Ci<=10^9), separated by single spaces; Ci is the installation time (in minutes) for the dwellers of house no. i.
The next N-1  lines specify the roads linking the houses. Each such line contains two positive integers a and b(1<=a<b<=N) , separated by a single space. These indicate that there is a direct road between the houses no. a and b.
 

输出

The first and only line of the standard output should contain a single integer: the (minimum) number of minutes after which all citizens will be able to play FarmCraft together.
 

样例输入

6
1 8 9 6 3 2
1 3
2 3
3 4
4 5
4 6

样例输出

11

提示

Explanation: Byteasar should deliver the computers to the houses in the following order: 3, 2, 4, 5, 6, and 1. The game will be installed after 11, 10, 10, 10, 8, and 9 minutes respectively, in the house number order. Thus everyone can play after 11 minutes.
 
题解
       论我今天为什么打挂了几乎所有题:昨天接连被ryf、byb、zsq三个dalao膜,RP变为-0x7fffffff,我不炸谁炸啊……
       另外给出的那个参考翻译明显是有道自动翻译吧,能把No.1翻译成“不1”我也是服,找zzh翻也不至于翻得这么奇怪啊;题目里那个翻译倒是没有什么语法错误,但是总觉得怪怪的……一开始当网络流来做,想了半个小时怎么建图,最后感觉这就不是个网络流的题,想到树中路径是唯一的,放下网络流开始树归。其实也不能算是严格的树归,只不过是用子节点推父亲罢了。遍历顺序肯定是需要贪心的,但是拿节点权值贪心总觉得心里不太安稳,果然如此,但是水过了75分~
       遍历的时间是一定的,即边数*2。关键是在遍历结束之后,还有可能一些点没有完成安装。用p[i]表示在遍历完i点及其子树后,仍需要的安装时间,则最后的结果为max(c[1],p[1])+边数*2。1是特别的,要遍历完成后才开始安装;叶节点p值即为c值,由子节点递推父节点p值的公式为:p[father]=max(c[father]-f[father],p[son]-(f[father]-w)),其中f为遍历本树所需步数,w为遍历到这个儿子已走过的步数。当每个子节点的p值在递归后已经确定,按p递减的顺序决定遍历顺序来进一步确定父节点p值(刚开始的时候这里一直没想懂,总觉得自己要拿些根本不知道的东西来确定遍历顺序,其实可以在一次递归里解决)。w值只包括父节点与子节点之间已走过的边,因为子节点的子树已经在子节点的p值被除去过了。c[i]-f[i]可能为负,但这种情况直接当做0来处理。注意到这些细节,一次dfs就可以处理完成了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int sj=500010;
int n,c[sj],h[sj],e,a1,a2,f[sj],jg;
int p[sj],fa[sj];
struct B
{
     int ne,v;
}b[sj*2];
void add(int x,int y)
{
     b[e].v=y;
     b[e].ne=h[x];
     h[x]=e++;
}
int bj(int x,int y)
{
    return x>y?x:y;
}
struct D
{
    int num,vl;
}d[sj];
int comp(const D&a,const D&b)
{
    return a.vl>b.vl;
}
void dfs(int x,int ge)
{
     int temp=ge,ww=1;
     for(int i=h[x];i!=-1;i=b[i].ne)
       if(b[i].v!=fa[x])
       {
          fa[b[i].v]=x;
          dfs(b[i].v,temp);
          f[x]+=f[b[i].v]+2;
          d[temp].vl=p[b[i].v];
          d[temp].num=b[i].v;
          temp++;
       }
     p[x]=c[x]-f[x];
     if(p[x]<0) p[x]=0;
     sort(d+ge,d+temp,comp);
     for(int i=ge;i<temp;i++)
     {
         ww+=f[d[i].num];
         p[x]=bj(p[x],p[d[i].num]-(f[x]-ww));
         ww+=2;
     }   
}
void init()
{
     scanf("%d",&n);
     memset(h,-1,sizeof(h));
     for(int i=1;i<=n;i++) scanf("%d",&c[i]);
     for(int i=1;i<n;i++)
     {
        scanf("%d%d",&a1,&a2);
        add(a1,a2);
        add(a2,a1);
     }
     dfs(1,1);
}
int main()
{
    init();
    jg=bj(c[1],p[1])+2*n-2;
    printf("%d",jg);
    return 0;
}
farmcraft

你应该超越自己,走得更远,登得更高,直至群星已在你脚下

转载于:https://www.cnblogs.com/moyiii-/p/7270855.html

相关文章:

  • Google知道你多少秘密
  • 《Java语言导学(原书第6版)》一一1.1 关于Java技术
  • 机器学习系列(8)_读《Nature》论文,看AlphaGo养成
  • TCP负载均衡地址转换
  • 新Tegra将问世:8核ARM再配CUDA GPU
  • Intent的使用
  • 《设计原本—计算机科学巨匠Frederick P. Brooks的反思》一一2.3 理性模型有哪些长处...
  • 沟通在一个运维服务项目中的重要性
  • 一个APP从启动到主页面显示经历了哪些过程?
  • 超级简单:如何更快的将数据导入Excel
  • springboot同mybatis整合
  • 博达路由器常见功能教学1
  • 阿特斯副总裁邢国强:2017年底光伏组件成本2元以下
  • python实战===使用随机的163账号发送邮件
  • CSS
  • IE9 : DOM Exception: INVALID_CHARACTER_ERR (5)
  • __proto__ 和 prototype的关系
  • Android系统模拟器绘制实现概述
  • Apache Pulsar 2.1 重磅发布
  • CEF与代理
  • Flannel解读
  • Java知识点总结(JavaIO-打印流)
  • magento 货币换算
  • 安卓应用性能调试和优化经验分享
  • 近期前端发展计划
  • 少走弯路,给Java 1~5 年程序员的建议
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 实现菜单下拉伸展折叠效果demo
  • 写代码的正确姿势
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • (1)虚拟机的安装与使用,linux系统安装
  • (2)STM32单片机上位机
  • (3)nginx 配置(nginx.conf)
  • (6)添加vue-cookie
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (层次遍历)104. 二叉树的最大深度
  • (二)PySpark3:SparkSQL编程
  • (附源码)php投票系统 毕业设计 121500
  • (附源码)springboot金融新闻信息服务系统 毕业设计651450
  • (更新)A股上市公司华证ESG评级得分稳健性校验ESG得分年均值中位数(2009-2023年.12)
  • (论文阅读32/100)Flowing convnets for human pose estimation in videos
  • (全注解开发)学习Spring-MVC的第三天
  • (一)UDP基本编程步骤
  • (一)插入排序
  • (原創) 如何動態建立二維陣列(多維陣列)? (.NET) (C#)
  • (转)程序员疫苗:代码注入
  • ***汇编语言 实验16 编写包含多个功能子程序的中断例程
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .NET MVC之AOP
  • .net/c# memcached 获取所有缓存键(keys)
  • .net操作Excel出错解决
  • .net程序集学习心得
  • .NET牛人应该知道些什么(2):中级.NET开发人员
  • .NET学习全景图
  • //解决validator验证插件多个name相同只验证第一的问题