LA4788
贪心
这个贪心不太懂啊 dfs返回子树需要的最小值,然后按需要减消耗排序,然后贪心选取即可。
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = ;
struct Node {
int a, g, c;
} node[N];
vector<int> G[N];
int cost[N], mx[N];
int n, ans, tot, kase;
bool cp(int i, int j)
{
return cost[i] == cost[j] ? mx[i] > mx[j] : cost[i] < cost[j];
}
bool cp1(int i, int j)
{
return mx[i] - cost[i] > mx[j] - cost[j];
}
int dfs(int u, int last)
{
int ret = ;
vector<PII> vt;
vt.clear();
for(int i = ; i < G[u].size(); ++i)
{
int v = G[u][i];
if(v == last) continue;
int need = dfs(v, u);
cost[u] += cost[v];
vt.push_back(make_pair(need - cost[v], need));
}
sort(vt.begin(), vt.end());
int pprev = ;
for(int i = vt.size() - ; i >= ; --i)
{
PII x = vt[i];
ret += max(x.second - pprev, );
pprev = max(pprev, x.second);
pprev -= x.second - x.first;
}
if(ret + node[u].g >= node[u].a) ret += node[u].g;
else ret = node[u].a;
cost[u] += node[u].g;
return ret;
}
int main()
{
while(scanf("%d", &n))
{
if(!n) break;
for(int i = ; i <= n; ++i)
{
scanf("%d%d%d", &node[i].a, &node[i].g, &node[i].c);
node[i].g += node[i].c;
}
for(int i = ; i <= n; ++i) G[i].clear();
for(int i = ; i < n; ++i)
{
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
ans = << ;
for(int i = ; i <= n; ++i)
{
memset(mx, , sizeof(mx));
memset(cost, , sizeof(cost));
ans = min(ans, dfs(i, ));
}
printf("Case %d: %d\n", ++kase, ans);
}
return ;
}
LA4788的更多相关文章
随机推荐
- Nginx 重新加载日志配置
最近在写一个nginx日志的切割脚本,切割完后,发现可以不重启服务,而直接重新加载日志配置文件的命令 [ kill -USR1 $nginx.pid ],但是不知道 -USR1这个参数是什么意 ...
- 【技术累积】【点】【java】【27】@JSONField
@JSONField 该注解隶属于阿里fastjson,方便fastjson处理对象时的一些操作 源码 @Retention(RetentionPolicy.RUNTIME) @Target({ El ...
- Nginx 监控
编译安装加上http_stub_status_module 模块 location /status { stub_status on; # access_log /usr/local/nginx/lo ...
- 洛谷——P3833 [SHOI2012]魔法树
P3833 [SHOI2012]魔法树 题目背景 SHOI2012 D2T3 题目描述 Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的 ...
- CentOS 7安装JDK 1.8
1. 首先查看当前Linux系统是否安装Java ``` rpm -qa | grep java ``` 2. 如果列表显示有,则使用命令将其卸载 rpm -e --nodeps 要卸载的软件名 或 ...
- 实验十二 团队作业8:软件测试与Alpha冲刺 第四天
项目 内容 这个作业属于哪个课程 老师链接 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺 团队名称 Always Run! 作业学习目标 (1)掌握软件测试基础技术 (2)学习 ...
- Python之路【第一篇】:Python基础1
本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...
- 洛谷——P2639 [USACO09OCT]Bessie的体重问题Bessie's We…
https://www.luogu.org/problem/show?pid=2639 题目描述 Bessie像她的诸多姊妹一样,因为从Farmer John的草地吃了太多美味的草而长出了太多的赘肉. ...
- LucaCanali --DTRACE AND SYSTEMTAP 脚本工具
https://github.com/LucaCanali http://cern.ch/canali/
- [转]十五天精通WCF——第二天 告别烦恼的config配置
经常搞wcf的基友们肯定会知道,当你的应用程序有很多的“服务引用”的时候,是不是有一种疯狂的感觉...从一个环境迁移到另外一个环境,你需要改变的 endpoint会超级tmd的多,简直就是搞死了人.. ...