当前位置:网站首页>【集训DAY3】石油储备计划【树形DP】
【集训DAY3】石油储备计划【树形DP】
2022-08-09 22:35:00 【VL——MOESR】
思路:
设f[i][j]表示当前i节点的子树,有j个是ave + 1的最小代价
转移方程就是 f [ x ] [ j ] = m i n ( f [ x ] [ j − k ] + f [ y ] [ k ] + a b s ( s [ y ] − ( s i z e [ y ] − k ) ∗ v a l − k ∗ ( v a l + 1 l l ) ) ∗ b [ i ] . w ) f[x][j] = min(f[x][j - k] + f[y][k] + abs(s[y] - (size[y] - k) * val - k * (val + 1ll)) * b[i].w) f[x][j]=min(f[x][j−k]+f[y][k]+abs(s[y]−(size[y]−k)∗val−k∗(val+1ll))∗b[i].w)
其中y是儿子,x是当前节点,s[y]是儿子子树的油桶数的数量,size[y]是儿子子树的大小,val是平均值,k是给儿子多少个val + 1
c o d e code code
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define ll long long
using namespace std;
ll t, val, SIZE;
ll n, sum, size[110], s[110];
ll a[110], f[110][110];
ll tot, head[110], g[110];
struct node {
ll to, next, w;
}b[10010];
void add(ll x, ll y, ll z) {
b[++ tot] = (node) {
y, head[x], z };
head[x] = tot;
}
ll min(ll x, ll y) {
return x < y ? x : y; }
void dfs(ll x, ll fa) {
size[x] = 1, s[x] = a[x];
f[x][0] = f[x][1] = 0;
for(ll i = head[x]; i; i = b[i].next) {
ll y = b[i].to;
if(fa == y) continue;
dfs(y, x);
}
for(ll i = head[x]; i; i = b[i].next) {
ll y = b[i].to;
if(y == fa) continue;
memset(g, 0x7f7f7f7f, sizeof(g));
s[x] += s[y], size[x] += size[y];
for(ll j = 0; j <= size[x] && j <= SIZE; j ++)
for(ll k = 0; k <= j && k <= size[y]; k ++)
g[j] = min(g[j], f[x][j - k] + f[y][k] + abs(s[y] - (size[y] - k) * val - k * (val + 1ll)) * b[i].w);
for(ll j = 0; j <= SIZE && j <= size[x]; j ++)
f[x][j] = g[j];
}
}
int main() {
scanf("%lld", &t);
while(t --) {
scanf("%lld", &n);
for(ll i = 1; i <= n; i ++) {
scanf("%lld", &a[i]);
sum += a[i];
}
val = sum / n, SIZE = sum % n;
for(ll i = 1; i < n; i ++) {
ll x, y, z;
scanf("%lld%lld%lld", &x, &y, &z);
add(x, y, z), add(y, x, z);
}
memset(f, 0x7f7f7f7f, sizeof(f));
dfs(1, 0);
printf("%lld\n", f[1][SIZE]);
memset(head, 0, sizeof(head));
memset(s, 0, sizeof(s));
memset(size, 0, sizeof(size));
sum = 0;
}
return 0;
}
边栏推荐
- 函数习题(下)
- matplotlib散点图颜色分组图例
- YGG 经理人杯总决赛已圆满结束,来看看这份文字版总结!
- Gumbel distribution of discrete choice model
- 【Burning】It's time to show your true strength!Understand the technical highlights of the 2022 Huawei Developer Competition in one article
- 直播app开发搭建,flutter 实现自适应、自动换行、相对布局
- A Shanghai technology company was fined 220,000 for brushing orders, exposing the gray industry chain of online brushing
- 2021年国内外五大BI厂商——优秀的商业智能工具推荐
- && 不是此版本的有效语句分隔符
- 70. 爬楼梯进阶版
猜你喜欢
32 JZOF 】 【 print down on binary tree
首席信息官如何将可持续性和技术结合起来
你的手机曾经被监控过吗?
ElasticSearcch集群
Explore the TiDB Lightning source code to solve the found bugs
【JZOF】32从上往下打印二叉树
巴比特 | 元宇宙每日必读:国内首个数字人产业专项支持政策发布,2025年北京数字人产业规模将破500亿元...
【Burning】It's time to show your true strength!Understand the technical highlights of the 2022 Huawei Developer Competition in one article
kubesphere
2022-08-09 mysql/stonedb-subquery performance improvement-introduction
随机推荐
为什么刀具数据库无法打开?
Filament - Material basic graphics drawing
harbor配置远程仓库
2021年国内外五大BI厂商——优秀的商业智能工具推荐
【Burning】It's time to show your true strength!Understand the technical highlights of the 2022 Huawei Developer Competition in one article
How to match garbled characters regularly?
tiup cluster template
Gartner全球集成系统市场数据追踪,超融合市场增速第一
Leetcode 701. 二叉搜索树中的插入操作
Has your phone ever been monitored?
JS--hashchange事件--使用/教程
ElasticSearcch集群
首席信息官如何将可持续性和技术结合起来
IT传奇人物菲尔德的转型经验教训及给CIO的建议
[Interface Test] Decoding the request body string of the requests library
安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育
直播间搭建,按钮左滑出现删除等操作按钮
Redis集群
生成NC文件时,报错“未定义机床”
tiup cluster stop