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的更多相关文章
随机推荐
- 3星|《腾讯产业森林:AI时代的创业密码》:后半部分是较详细的创业指南,前面泛泛介绍腾讯、AI
腾讯产业森林:AI时代的创业密码 前半部分泛泛介绍腾讯对创业者的支持,腾讯支持的创业项目的案例.AI的一些基本介绍,后半部分是比较详细的写给创业者的各阶段行动与选择的指南. 总体评价3星,有一些参考价 ...
- (转) Hibernate检索方式概述
http://blog.csdn.net/yerenyuan_pku/article/details/70554816 Hibernate检索方式概述 我们在对数据库的操作中,最常用的是select, ...
- swift-延时加载函数
//延时加载函数 func delayLoad(){ let time: NSTimeInterval = 2.0 let delay = dispatch_time(DISPATCH_TIME_NO ...
- uva 227 Puzzle (UVA - 227)
感慨 这个题实在是一个大水题(虽然说是世界决赛真题),但是它给出的输入输出数据,标示着老子世界决赛真题虽然题目很水但是数据就能卡死你...一直pe pe直到今天上午AC...无比感慨...就是因为最后 ...
- pip/pip3国内源
Error 在使用pip3安装PySide2时出现ReadTimeoutError. $ pip3 install PySide2 Solution 使用国内源 例如: $ pip3 install ...
- Problem 28
Problem 28 https://projecteuler.net/problem=28 Starting with the number 1 and moving to the right in ...
- git 的简单使用(4)
多人协作的工作模式通常是这样: 首先,可以试图用git push origin <branch-name>推送自己的修改: 如果推送失败,则因为远程分支比你的本地更新,需要先用git pu ...
- 《hello-world》第八次团队作业:Alpha冲刺-Scrum Meeting 3
项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺 团队名称 <hello--worl ...
- noip模拟赛 黑骑士
题目描述江爷爷给你出了一道题:给你一个图,保证每个点最多属于一个简单环,每个点度数最多为3,求这个图有多少“眼镜图形个数”保证图联通哦~其中“眼镜图形个数”,定义为三元组(x,y,S),其中x和y表示 ...
- 使用applescript脚本方式以管理员权限运行
- (BOOL) runProcessAsAdministrator:(NSString*)scriptPath withArguments:(NSArray ...