一、题意

  给定一个树状地图,每个树节点上有一只怪物,打死一只怪物的过程中将会消耗A点HP,打死之后将会获得B点HP。因为树状结构,所以每只怪物必须先打死父节点的怪兽之后在打死子节点的怪物。现在,给定每只怪兽的a,b和树状结构的联通关系,求初始HP最小的击败顺序。

  Little Q is fighting against scary monsters in the game ``Monster Hunter''. The battlefield consists of n intersections, labeled by 1,2,...,n, connected by n−1bidirectional roads. Little Q is now at the 1-th intersection, with X units of health point(HP).
There is a monster at each intersection except 1. When Little Q moves to the k-th intersection, he must battle with the monster at the k-th intersection. During the battle, he will lose ai units of HP. And when he finally beats the monster, he will be awarded bi units of HP. Note that when HP becomes negative(<0), the game will over, so never let this happen. There is no need to have a battle at the same intersection twice because monsters do not have extra life.
When all monsters are cleared, Little Q will win the game. Please write a program to compute the minimum initial HP that can lead to victory.

二、解题思路

  首先考虑在一般情况下<仅仅给出每只怪物的A,B的情况下>如何进行选择。于是有:

    1. 如果两只怪物的b都大于a,则优先打击a更小的那个怪物。
    2. 如果一只怪物a<b,另一只a>b则优先打击a<b的。
    3. 如果两只怪物的b都小于a,于是有,最小血量的击败方案为min(max(ai,ai+aj-bi),max(aj,aj+ai-bj)),题解提到上述计算式实际上等于比较bi和bj

  之后考虑,加入树边的情况。

  考虑使用优先队列维护最优解,则

    当某一个元素p为上述比较方案的最优解时,

    且p的树根如果被选择后,选择p一定是最优解。

  因此可以将p并入树根节点,后使p得所有子节点成为p的兄弟节点。——因为当p的父节点被选择后,p也已经默认被立刻选择了。

  最终,当所有节点都是根节点的子节点时,计算并比较最优解的具体状态。

  考虑一个细节:如何合并两个节点?
    设ai,aj,bi,bj为两个节点的相关变量则有:

    ak = min(max(ai,ai+aj-ai),max(aj,aj+ai-bj))

    bk = ak + bi + bj - ai - aj

#include<bits/stdc++.h>
using namespace std; #define ll long long
const ll MAXN=;
#define veci vector<int> int fa[MAXN];
int cha[MAXN]; class Node
{
public:
ll a,b,num,ver;
Node(){}
Node(const Node &n)
{
this->a = n.a;
this->b = n.b;
this->num = n.num;
this->ver = n.ver;
}
bool operator < (Node const n)const{
ll tmpa = this->b - this->a;
ll tmpb = n.b - n.a;
if(tmpa >= && tmpb >=)return this->a > n.a;
if(tmpa >= && tmpb < )return false;
if(tmpa < && tmpb >=)return true;
if(tmpa < && tmpb < )return this->b < n.b;
}
};
Node nodes[MAXN];
int n;
veci G[MAXN]; void dfs(int now,int father)
{
fa[now] = father;
int len = G[now].size();
for(int i=;i<len;++i)
{
int tar = G[now][i];
if(tar == father)continue;
dfs(tar,now);
}
} int find_set(int tar)
{
if(cha[tar] == tar)return tar;
else return cha[tar] = find_set(cha[tar]);
} void init()
{
cin>>n;
for(int i=;i<n+;++i)
{
G[i].clear();
cha[i] = i;
}
priority_queue<Node> pq;
for(int i=;i<=n;++i)
{
cin>>nodes[i].a>>nodes[i].b;
nodes[i].num = i;
nodes[i].ver = ;
pq.push(nodes[i]);
}
for(int i=;i<n;++i)
{
int a,b;
cin>>a>>b;
G[a].push_back(b);
G[b].push_back(a);
}dfs(,); ll ans = ;
ll money = ; while(!pq.empty())
{
Node node = pq.top();
pq.pop(); int now = node.num;
if(node.ver != nodes[now].ver)continue;
int father = fa[now];
father = find_set(father);
nodes[now].ver = -;
cha[now] = cha[father];
if(father == )
{
if(node.a > money)
{
ans += node.a-money;
money = node.b;
}else{
money -= node.a;
money += node.b;
}
continue;
} nodes[father].ver ++;
ll tmp = nodes[father].b +node.b - nodes[father].a-node.a;
nodes[father].a = max(nodes[father].a, nodes[father].a + node.a - nodes[father].b);
nodes[father].b = nodes[father].a + tmp;
Node newNode(nodes[father]);
pq.push(newNode);
} cout<<ans<<"\n";
} int main()
{
cin.sync_with_stdio(false);
int t;
cin>>t;
while(t--)init(); return ;
}

HDU暑假多校第三场H.Monster Hunter的更多相关文章

  1. 2020牛客暑假多校训练营 第二场 H Happy Triangle set 线段树 分类讨论

    LINK:Happy Triangle 这道题很容易. 容易想到 a+b<x a<x<b x<a<b 其中等于的情况在第一个和第三个之中判一下即可. 前面两个容易想到se ...

  2. HDU暑假多校第八场G-Card Game

    一.题意 给出N个卡牌,卡牌的正反两面具有两个数字,取值范围为[1,2*n],给出若干个默认正面向上的卡牌,求最小反转多少张卡牌可以使得,每张卡牌朝上的面上都有一个不同的数字,同时满足最小反转次数的反 ...

  3. HDU暑假多校第八场J-Taotao Picks Apples

    一.题意 给定一个序列,之后给出若干个修改,修改的内容为在原序列的基础上,将某一位元素的值改成给定的值<每次修改相互独立,不保存修改后的结果>.之后询问,在选择第一位元素的情况下,最长递增 ...

  4. HDU暑假多校第六场K-werewolf

    一.题意 好人必然说真话,坏人不一定说真话,给定N个人的言论<每人一个发言.不谈及自己>,要求指出有多少个人一定是好人,有多少个人一定是坏人.#define 狼人 坏人#define 村民 ...

  5. HDU暑假多校第四场J-Let Sudoku Rotate

    一.题意 Sudoku is a logic-based, combinatorial number-placement puzzle, which is popular around the wor ...

  6. [题解]Magic Line-计算几何(2019牛客多校第三场H题)

    题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意: 给你偶数个点的坐标,找出一条直线将这n个点分成数量相等的两部分 并在这条直线上取不同的两个点,表示 ...

  7. 2019 牛客多校第三场 H Magic Line

    题目链接:https://ac.nowcoder.com/acm/contest/883/H 题目大意 给定 N 个不同的整数点,N 为偶数,求一条直线,这条直线能把这 N 个点对半分开,输出这条直线 ...

  8. 2019牛客多校第三场H Magic Line 思维

    Magic Line 题意 给出n(偶)个整点 整点范围1000,找出一条直线,把n个点分成均等的两部分 分析 因为都是整数,并且范围比较小,所以直接按x排序找到在中间那一部分,并且把中间那一部分的点 ...

  9. [题解] 2019牛客暑期多校第三场H题 Magic Line

    题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意:二维平面上有n个不同的点,构造一条直线把平面分成两个点数相同的部分. 题解:对这n个点以x为第一关键 ...

随机推荐

  1. python下的selenium安装

    安装python 打开 Python官网,找到“Download”, 在其下拉菜单中选择自己的平台(Windows/Mac),一般的Linux平台已经自带的Python,所以不需要安装,通过打开“终端 ...

  2. C# 调用 c++ 非托管dll时wchar类型参数的乱码处理

    [DllImport("user32.dll", CallingConvention = CallingConvention.WinApi)] public static exte ...

  3. python UI自动化实战记录一:测试需求与测试思路

    测试需求: 项目包含两个数据展示页面,数据均来自于四个数据源接口. 测试操作步骤: 选择5个大类型中的一个,每个大类型下有3个子类型,选择任一子类型,页面数据更新.需验证页面上的数据与数据源接口数据一 ...

  4. kahadb设计

    Kahadb设计思想   简介 hakadb是activemq的持久化数据库,作为消息队列的存储,每个消息有一个消息ID,提供了对消息的快速的查找,更新,以及消息的事物支持,以及意外磬机之后的恢复.丰 ...

  5. BZOJ4001:[TJOI2015]概率论(卡特兰数,概率期望)

    Description Input 输入一个正整数N,代表有根树的结点数 Output 输出这棵树期望的叶子节点数.要求误差小于1e-9 Sample Input 1 Sample Output 1. ...

  6. 深搜(DFS),回溯,Fire Net

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2 解题报告: 这里的深搜有一点不同,就是,在深搜每一个点时,都要深搜每 ...

  7. 并发队列 – 有界阻塞队列 ArrayBlockingQueue 原理探究

    一.ArrayBlockingQueue类图结构 如图ArrayBlockingQueue内部有个数组items用来存放队列元素,putindex下标标示入队元素下标,takeIndex是出队下标,c ...

  8. Graphics Card Notes | 烧卡日记(显卡常识笔记)

    [ a comparison of several popular Graphics cards ]

  9. shell命令查看某文件夹下的文件个数

    shell命令查看某文件夹下的文件个数 2010-06-25 17:05:15|  分类: shell |字号 订阅   1.查看某文件夹下文件的个数: ls -l |grep "^-&qu ...

  10. JQuery 禁用后退按钮

    jQuery(document).ready(function () { if (window.history && window.history.pushState) { $(win ...