Day3-A-Problem H. Monster Hunter HDU6326
There is a monster at each intersection except 11. When Little Q moves to the kk-th intersection, he must battle with the monster at the kk-th intersection. During the battle, he will lose aiai units of HP. And when he finally beats the monster, he will be awarded bibi units of HP. Note that when HP becomes negative(<0<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.
InputThe first line of the input contains an integer T(1≤T≤2000)T(1≤T≤2000), denoting the number of test cases.
In each test case, there is one integer n(2≤n≤100000)n(2≤n≤100000) in the first line, denoting the number of intersections.
For the next n−1n−1 lines, each line contains two integers ai,bi(0≤ai,bi≤109)ai,bi(0≤ai,bi≤109), describing monsters at the 2,3,...,n2,3,...,n-th intersection.
For the next n−1n−1 lines, each line contains two integers uu and vv, denoting a bidirectional road between the uu-th intersection and the vv-th intersection.
It is guaranteed that ∑n≤106∑n≤106.
OutputFor each test case, print a single line containing an integer, denoting the minimum initial HP.
Sample Input
1
4
2 6
5 4
6 2
1 2
2 3
3 4
Sample Output
3 思路:直接贴一个官方题解
代码如下:(不懂的可以再问我):
typedef long long LL;
const int maxm = ;
struct Node {
int id, change;
LL a, b;
bool operator < (const Node &x) const {
if(a >= b && x.a < x.b ) return true; // b > a 的优先
if(a < b && x.a >= x.b ) return false;
if(a < b && x.a < x.b ) return a>x.a;//a < b,按照a从小到大
if(a >= b && x.a >= x.b) return b<x.b;//a >= b,按照b从大到小
}
void operator += (const Node &n) { // A means the minimum HP to kill monster
LL A = a, B = b;
if(b < n.a) {
A = a + n.a - b;
B = n.b;
} else {
B = b - n.a + n.b;
}
a = A, b = B;
}
} buf[maxm];
int fa[maxm], vis[maxm], cnt, n;
vector<int> G[maxm];
void build(int u, int father) {
fa[u] = father;
for (int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(v != father)
build(v, u);
}
}
int Find(int u) {
if(vis[fa[u]])
return fa[u] = Find(fa[u]);
else
return fa[u];
}
void init() {
cnt = ;
buf[].a = buf[].b = buf[].change = ;
buf[].id = ;
memset(vis, , sizeof(vis)), memset(fa, , sizeof(fa));
for (int i = ; i <= n; ++i)
G[i].clear();
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
init();
priority_queue<Node> q;
for (int i = ; i <= n; ++i) {
scanf("%lld%lld", &buf[i].a, &buf[i].b);
buf[i].id = i, buf[i].change = ;
q.push(buf[i]);
}
for (int i = ; i < n - ; ++i) {
int t1, t2;
scanf("%d%d", &t1, &t2);
G[t1].push_back(t2), G[t2].push_back(t1);
}
build(, );
while(!q.empty()) {
Node tmp = q.top();
q.pop();
int u = tmp.id;
if(vis[u] || tmp.change != buf[u].change)
continue;
vis[u] = ;
int f = Find(u);
buf[f] += buf[u];
if(f > ) {
buf[f].change = ++cnt;
q.push(buf[f]);
}
}
printf("%lld\n", buf[].a);
}
return ;
}
tmp.change != buf[u].change: 堆中有修改前的u和修改后的u
Day3-A-Problem H. Monster Hunter HDU6326的更多相关文章
- HDU 6326 Problem H Monster Hunter
\(\mathtt{Problem H}\) \(\mathtt{Monster}\) \(\mathtt{Hunter}\) \(\mathcal{Description}\) 题目 给定一棵 \( ...
- HDU 6326.Problem H. Monster Hunter-贪心(优先队列)+流水线排序+路径压缩、节点合并(并查集) (2018 Multi-University Training Contest 3 1008)
6326.Problem H. Monster Hunter 题意就是打怪兽,给定一棵 n 个点的树,除 1 外每个点有一只怪兽,打败它需要先消耗 ai点 HP,再恢复 bi点 HP.求从 1 号点出 ...
- HDU暑假多校第三场H.Monster Hunter
一.题意 给定一个树状地图,每个树节点上有一只怪物,打死一只怪物的过程中将会消耗A点HP,打死之后将会获得B点HP.因为树状结构,所以每只怪物必须先打死父节点的怪兽之后在打死子节点的怪物.现在,给定每 ...
- 实验12:Problem H: 整型数组运算符重载
Home Web Board ProblemSet Standing Status Statistics Problem H: 整型数组运算符重载 Problem H: 整型数组运算符重载 Tim ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem H
Problem H High bridge, low bridge Q: There are one high bridge and one low bridge across the river. ...
- Gym 100531H Problem H. Hiking in the Hills 二分
Problem H. Hiking in the Hills 题目连接: http://codeforces.com/gym/100531/attachments Description Helen ...
- Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞
Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...
- Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉
Problem H. Hard TestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...
- 清北学堂入学测试P4751 H’s problem(h)
P4751 H’s problem(h) 时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试 描述 小H是一个喜欢逛街的女孩子,但是由于上了大学 ...
随机推荐
- Beego Learning Notes
Beego框架学习 1.1软件框架 一个公司是由公司中的各部部门来组成的,每一个部门拥有特定的职能,部门与部门之间通过相互的配合来完成让公司运转起来. 一个软件框架是由其中各个软件模块组成的,每一个模 ...
- 802.11r mixed mode
* 802.11r mixed mode support – Untill this code(8.0), if you enable 802.11r fast secure roaming, onl ...
- IO课堂测试
一,用户需求 英语的26 个字母的频率在一本小说中是如何分布的?某类型文章中常出现的单词是什么?某作家最常用的词汇是什么?<哈利波特与魔法石> 中最常用的短语是什么,等等. (1)要求1: ...
- kafka connector
Kafka Connect 是一种用于在 Kafka 和其他系统之间可扩展的.可靠的的流式传输数据的工具.它使得能偶快速定义将大量数据集合移入和移除 kafka 连接器变得简单. kafka conn ...
- Codeforces Global Round 5E(构造,思维)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int main(){ ios::sync_w ...
- 洛谷 P1119 灾后重建(Floyd)
嗯... 题目链接:https://www.luogu.org/problem/P1119 这道题是一个Floyd的很好的题目,在Floyd的基础上加一点优化: 中转点k在这里不能暴力枚举,否则会超时 ...
- Java入门笔记 02-数组
介绍: Java的数组既可以存储基本类型的数据,也可以存储引用类型的数据,但是要求所有的数组元素具有相同的数据类型.另外,Java数组也是一种数据类型,其本身就是一种引用类型. 一.数组的定义: 数据 ...
- MRCP接口MRCPRecog 简介
功能:开始一个语音识别,一边讲话,一边识别,需要ASR服务器. 原型:MRCPRecog (grammar, options) grammar ---- 语法文件,可以是一个xml文件 options ...
- Vulnhub_DC1 记录
目录 Vulnhub_DC1 记录 经验 & 总结 步骤流水 1. 建立立足点 2. 提权 3. 主机信息搜集 4. 继续提权 5. 消失的flag Vulnhub_DC1 记录 参考walk ...
- where、having区别
where << group by << having where筛选是在分组之前筛选,筛选完之后再group by having是分组之后再筛选,筛选完之前先g ...
