贪心

这个贪心不太懂啊 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的更多相关文章

随机推荐

  1. CSS——轮播图中的箭头

    注意事项: 1.定位中left权重比right高,top权重比bottom高 2.两个span标签嵌套在一个盒子中,将来显示隐藏只需要控制父盒子就行了 <!DOCTYPE html> &l ...

  2. ASLR(Address space layout randomization)地址空间布局随机化

    /*********************************************************************  * Author  : Samson  * Date   ...

  3. includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。

    注意:对象数组不能使用includes方法来检测. JavaScript Demo: Array.includes() var array1 = [1, 2, 3]; console.log(arra ...

  4. 在引入的css或者js文件后面加参数的作用

    有时候可能会遇到js或者css文件引用后传递参数: css和js带参数(形如.css?v=与.js?v=) <script type=”text/javascript” src=”jb51.js ...

  5. docker安装kong和kong-dashboard

    1:docker安装遵循官方手册 2:安装kong 参考文档:https://getkong.org/install/docker/ 安装过程基本和文档一致,文档十分简单清晰. 但应注意,为了最新版k ...

  6. isset在php5.6-和php7.0+的一些差异

    今天在公司实现一个模块功能时写了如下代码: class ProductCategory { const TYPES = [ 1 => 'type1', 2 => 'type2', ]; p ...

  7. Problem 28

    Problem 28 https://projecteuler.net/problem=28 Starting with the number 1 and moving to the right in ...

  8. PAT 1113 Integer Set Partition

    Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint set ...

  9. Django——3 模板路径 模板变量 常用过滤器 静态文件的使用

    Django 模板路径 模板变量 过滤器 静态文件的加载 模板的路径,有两种方法来使用 设置一个总的templates在大项目外面,然后在sittings的TEMPLATES中声明 在每一个APP中创 ...

  10. 【codeforces 508A】Pasha and Pixels

    [题目链接]:http://codeforces.com/contest/508/problem/A [题意] 让你在一个n*m的方格上给方格染色; 顺序给出染色的k个格子 如果在某一时刻 有一个2* ...