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的更多相关文章
随机推荐
- html5——3D转换
角度旋转 rotateX:默认以center绕x轴旋转 rotateY:默认以center绕y轴旋转 rotateZ:默认以cente绕z轴r旋转 //rotateX原点为center==>正值 ...
- How an SSL connection is established
An SSL connection between a client and server is set up by a handshake, the goals of which are: To s ...
- UIResponder详解
UIResponder Class Reference Managing the Responder Chain 1.- (UIResponder *)nextResponder 返回接收者的下一个相 ...
- PHP 之递归遍历目录与删除
/** * @Description: 递归查询目录文件 * @Author: Yang * @param $path * @param int $level * @return array */ f ...
- WPF在win7运行时报'Initialization of 'System.Windows.Setter' threw an exception.'
写的一个WPF程序,在win10运行好好的,在win7就报'Initialization of 'System.Windows.Setter' threw an exception.' 原来是xaml ...
- js-2018-11-01 关于break和continue语句
1.label语句 语法:label: statement 加标签语句一般都要与for语句等循环语句配合使用. 2.break语句 立即退出循环,强制执行循环后面的语句. 3.continue语句 立 ...
- JAVA学习总结-常用数据结构
java中集合框架其实就是数据结构的实现的封装; 参考资料:任小龙教学视频 1,什么是数据结构? 数据结构是计算机存储,组织数据的方式; 数据结构是指相互之间存在一种或多种特定关系的数据元素的集合; ...
- 使用Python的Flask框架,结合Highchart,动态渲染图表(Ajax 请求数据接口)
参考链接:https://www.highcharts.com.cn/docs/ajax 参考链接中的示例代码是使用php写的,这里改用python写. 需要注意的地方: 1.接口返回的数据格式,这个 ...
- LINQ简记(1):基本语法
关于LINQ(语言集成查询)是.NET 3.5和Visual Studio 2008以上版本中引入的一种有趣的全新概念,语言版本有VB和C#,由于C#与.NET平台结合最为紧密,也是MS当初首推的语言 ...
- ansible - playbook(剧组)
目录 ansible - playbook(剧组) 常用命令 五种传参方式 常用元素详解 tags handlers template when 循环 嵌套循环 ansible - playbook( ...